-
Notifications
You must be signed in to change notification settings - Fork 1
Examples: Dynamic Chart
examples/dynamic_chart
One of the features of DataDocs we're most excited about is its support for visualizations (even interactive ones!) driven by API data. In this example we show how the Google Visualizations API can be used to create customized, interactive line charts using API data, though any combination of data and visualization library can be used as long as the results are rendered inside a div.
Note: Due to the implementation details of Google Visualizations, this particular example is not properly interactive in fullscreen mode, though it works as expected at its original resolution. Over time, we hope to include to provide an index of libraries that work correctly in fullscreen mode as well.
As in the simple api example, we'll start with empty <div> tags and then add the visualizations to them once our libraries have loaded:
-
Open the
dd_dynamic_chart.htmlfile, and update the number/ids of the empty divs in the "overlays" div. Incss/dd_dynamic_chart.cssupdate the positioning of the divs by creating or modifying the corresponding css classes. Inmain.js, adjust thedata_doc.addSaltcalls to target your updated divs. -
Before we can render our chart, we need to make sure the charting library has loaded. In
dd_dynamic_chart.html, the Google Visualization API is loaded just below the "load the google visualization library" comment. After ourdata_doc.addSaltcalls inmain.js, we'll use thegoogle.loadmethod to load thecorechartpackage and tell it to run the functionloadChartDataonce the library is loaded (this is specified in thecallbackparameter). -
Once the library is loaded, the function
loadChartDatawill be called, so inside this function we'll use$.getrequests to load our data files. In this example, we're looking at unemployment data from the FRED API. Each of these requests will trigger an individual function to reformat the data, customize the look and feel of the chart and build it in a specified div.
Note: For a discussion of how to use cron jobs to have the data in your JSON files update automatically from an API, see the simple api tutorial.
- In the JSON callback function (
buildU3ChartandbuildU6Chartin our example), we first create a Google VisualizationDataTableobject, and then reformat our JSON data into an array of arrays and use Google's built-inaddRowsmethod to add it to our DataTable object. In theoptionsJSON object, we can customize virtually everything about our chart's appearance according to the documentation here . Finally, we pass our target<div>tag via javascript thegoogle.visualization.LineChartmethod and call thedrawfunction with theDataTableand our options object to draw the chart.
Note: The
google.visualization.LineChartfunction must be passed a div retrieved with plain JavaScript; a jQuery object will not work properly.
- Open up the
dd_dyanmic_chart.htmlfile in FireFox to view your video with its included dynamic charts!