For some options, Highcharts allows you to specify a javascript function to dynamically change the behavior of the option (See an example). However, when you specify such a function using for example a dictionary in Python, they will not be recognized due to the fact they get converted to JSON making javascript think they are just strings.
Lets take a look at this behavior. We want to use a function to display the number of our chart as the tooltip. So we create an options dictionary containing this function:
options = {
'tooltip': {
'formatter': 'function() { return this.point.y; }'
}
}
When Charts sees this dictionary, it will convert it to json resulting in the following:
import json
json_options = json.dumps(options)
print json_options
{"tooltip": {"formatter": "function() { return this.point.y; }"}}
When you try to plot this, the function will not be recognized and no tooltip will be shown:
import charts
series = [{
'data': [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
}]
charts.plot(series, options=options, show='inline', type='line')
Server running in the folder /Users/arnoutaertgeerts/Projects/python-highcharts at 127.0.0.1:54507
As you can see, the function is formatted as a string and their is no way for javascript to know it actually is a function... To achieve the right behavior, you have to let Charts know you don't want the javascript function to be surrounded by double quotes. You can do this by adding "@#"
before the function keyword and after the final closing bracket }
.
Let's try this:
options = {
"tooltip": {
"formatter": "@#function() { return this.series.name + '<br/>' + this.point.y }@#"
}
}
charts.plot(series, options=options, show='inline', type='line')
By using the "@#"
character sequence, you can let Charts know you don't want the function to be stringified and it will remove the quotes after the JSON is parsed!