Custom names for x-axis values

This example demonstrates how to use the sortf_mapf_mts parameter to map the x-axis. The database only has month numbers (1-12) but not the month names. To display the month names in the graph, we create the monthname function and pass it to the Chart as the mapping funtion (mapf).

Points to note:

  • mts is False because we want to sort by month numbers and map to the month names after they are sorted in order of month numbers. Setting it to True would sort after mapping giving an incorrect sort order like Apr, Aug, Dec, ....

code

ds = DataPool(
       series=
        [{'options': {
            'source': MonthlyWeatherByCity.objects.all()},
          'terms': [
            'month',
            'houston_temp', 
            'boston_temp']}
         ])

def monthname(month_num):
    names ={1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
            7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
    return names[month_num]

cht = Chart(
        datasource = ds, 
        series_options = 
          [{'options':{
              'type': 'line',
              'stacking': False},
            'terms':{
              'month': [
                'boston_temp',
                'houston_temp']
              }}],
        chart_options = 
          {'title': {
               'text': 'Weather Data of Boston and Houston'},
           'xAxis': {
                'title': {
                   'text': 'Month'}}},
        x_sortf_mapf_mts = (None, monthname, False))