I would like your help for a little project I'm doing with Django and Highcharts.js.
My target: hide legend in Highcharts's chart from my script views.py
From my views.py file I have managed to be able to correctly plot different types of charts. But I can't hide the legend from views.py.
To give an example I have borrowed the code from this django-highcharts-example tutorial in Github.
Here is the example code to replicate the behaviour of the script:
views.py
def ticket_class_view_3(request):
dataset = Passenger.objects \
.values('ticket_class') \
.annotate(survived_count=Count('ticket_class', filter=Q(survived=True)),
not_survived_count=Count('ticket_class', filter=Q(survived=False))) \
.order_by('ticket_class')
categories = list()
survived_series_data = list()
not_survived_series_data = list()
for entry in dataset:
categories.append('%s Class' % entry['ticket_class'])
survived_series_data.append(entry['survived_count'])
not_survived_series_data.append(entry['not_survived_count'])
survived_series = {
'name': 'Survived',
'data': survived_series_data,
'color': 'green'
}
not_survived_series = {
'name': 'Survived',
'data': not_survived_series_data,
'color': 'red',
'showInLegend': "false" }
chart = {
'chart': {'type': 'column'},
'title': {'text': 'Titanic Survivors by Ticket Class'},
'xAxis': {'categories': categories},
'series': [survived_series, not_survived_series],
'plotOptions': {'column': {'showInLegend': 'false'}}
}
dump = json.dumps(chart)
return render(request, 'ticket_class_3.html', {'chart': dump})
The html where I import Highcharts.js and create the charts.
ticket_class_3.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Django Highcharts Example</title>
</head>
<body>
<a href="{% url 'home' %}">Return to homepage</a>
<div id="container"></div>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script>
Highcharts.chart('container', {{ chart|safe }});
</script>
</body>
</html>
I have tried to hide the legend as it is in the example code above in views.py
'plotOptions': {'column': {'showInLegend': 'false'}}
I have also tried it when creating the series with the same result: the legend still appears.
not_survived_series = {
'name': 'Survived',
'data': not_survived_series_data,
'color': 'red',
'showInLegend': "false"}
However, if I add this showInLegend option in the html code, it works correctly. But I would like to be able to do it from Python. Is it possible, can you help me?
If you need an example easier to execute, let me know and I'll try to prepare it.