{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Javascript functions in options\n", "\n", "For some options, Highcharts allows you to specify a javascript function to dynamically change the behavior of the option (See an [example](http://www.highcharts.com/docs/chart-concepts/tooltip#formatter)). 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.\n", "\n", "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: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "options = {\n", " 'tooltip': { \n", " 'formatter': 'function() { return this.point.y; }'\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When **Charts** sees this dictionary, it will convert it to json resulting in the following:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"tooltip\": {\"formatter\": \"function() { return this.point.y; }\"}}\n" ] } ], "source": [ "import json\n", "json_options = json.dumps(options)\n", "print json_options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you try to plot this, the function will not be recognized and no tooltip will be shown:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Server running in the folder /Users/arnoutaertgeerts/Projects/python-highcharts at 127.0.0.1:54507\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "