Pivot Chart on multiple axes

Pivot Chart with multiple terms on multiple axes.

Points to note:

  • Note that the term avg-price is passed as a dict (instead of as a django aggregate to allow us to override the default legend_by option. When passed as a dict, the aggregate function needs to be passed to the func key.

  • Alternatively this could be written as

    series= [
       {'options':{
          'source': SalesHistory.objects.all(),
          'categories': [
            'bookstore__city__state',
            'bookstore__city__city'],
          'legend_by': 'book__genre__name'},
        'terms': {
          'tot_sales':Sum('sale_qty')}},
    
       {'options':{
          'source': SalesHistory.objects.all(),
          'categories': [
            'bookstore__city__state',
            'bookstore__city__city']},
        'terms': {
          'avg_price':Avg('price')}}
          ]
    

    but the one used in the code is more succint and has less duplication.

code

ds = PivotDataPool(
      series= [
       {'options':{
          'source': SalesHistory.objects.all(),
          'categories': [
            'bookstore__city__state',
            'bookstore__city__city'],
          'legend_by': 'book__genre__name'},
        'terms': {
          'tot_sales':Sum('sale_qty'),
          'avg_price':{
            'func': Avg('price'),
            'legend_by': None}}}],
      top_n = 5,
      top_n_term = 'tot_sales',
      pareto_term = 'tot_sales')

pivcht = PivotChart(
          datasource = ds, 
          series_options = [
            {'options': {
               'type': 'column',
               'stacking': True},
             'terms': [
                'tot_sales',
                {'avg_price': {
                    'type': 'line',
                    'yAxis': 1}}]}],
          chart_options = {
            'yAxis': [{}, {'opposite': True}]})