Data from multiple models on same chart

This example demonstrates data from two different models MonthlyWeatherByCity and MonthlyWeatherSeattle on the same chart and on the same x-axis.

Points to note:

  • The month in terms for seattle data is written as {'month_seattle': 'month'} instead of as just 'month' because in the latter case it would overwrite the 'month' term from the other model.
  • Notice that the Seattle weather data in the database does not have any data for August (8) and September (9). Chartit gracefully skips them and plots the rest of the data points aligned correctly.

code

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

cht = Chart(
        datasource = ds, 
        series_options = 
          [{'options':{
              'type': 'line',
              'stacking': False},
            'terms':{
              'month': [
                'boston_temp',
                'houston_temp'],
              'month_seattle': [
                'seattle_temp']
              }}],
        chart_options = 
          {'title': {
               'text': 'Weather by Month (from 2 different tables)'},
           'xAxis': {
                'title': {
                   'text': 'Month number'}}})