-
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.html
file, and update the number/ids of the empty divs in the "overlays" div. Incss/dd_dynamic_chart.css
update the positioning of the divs by creating or modifying the corresponding css classes. Inmain.js
, adjust thedata_doc.addSalt
calls 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.addSalt
calls inmain.js
, we'll use thegoogle.load
method to load thecorechart
package and tell it to run the functionloadChartData
once the library is loaded (this is specified in thecallback
parameter). -
Once the library is loaded, the function
loadChartData
will be called, so inside this function we'll use$.get
requests 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 (
buildU3Chart
andbuildU6Chart
in our example), we first create a Google VisualizationDataTable
object, and then reformat our JSON data into an array of arrays and use Google's built-inaddRows
method to add it to our DataTable object. In theoptions
JSON 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.LineChart
method and call thedraw
function with theDataTable
and our options object to draw the chart.
Note: The
google.visualization.LineChart
function must be passed a div retrieved with plain JavaScript; a jQuery object will not work properly.
- Open up the
dd_dyanmic_chart.html
file in FireFox to view your video with its included dynamic charts!