Line chart with Avg() and Count()

This chart plots the average book rating in each genre together with the number of books in each genre.

NOTE that we use the SQL functions for average and count!

code

ds = DataPool(
        series=[{
            'options': {
                'source': Book.objects.raw(
                            "SELECT "
                            "    demoproject_book.id, "
                            "    demoproject_genre.name as genre_name, "
                            "    avg(rating) as rating_avg, "
                            "    count(genre_id) as genre_count "
                            "FROM demoproject_book "
                            "JOIN demoproject_genre ON "
                            "     genre_id == demoproject_genre.id "
                            "GROUP BY genre_id "
                          )
            },
            'terms': [
                'genre_name',
                'rating_avg',
                'genre_count'
            ]
        }]
)

cht = Chart(
        datasource=ds,
        series_options=[{
            'options': {
                'type': 'line',
                'stacking': False
            },
            'terms': {
                'genre_name': [
                    'rating_avg', 'genre_count'
                ]
            }
        }],
        chart_options={
            'title': {
                'text': 'Book rating and count per Genre'
            },
            'xAxis': {
                'title': {
                    'text': 'Genre'
                }
            }
        }
)