To use dygraphs, include the dygraph-combined-dev.js
JavaScript file and instantiate a Dygraph
object.
Here's a basic example to get things started:
<html> <head> <script type="text/javascript" src="dygraph-combined-dev.js"></script> </head> <body> <div id="graphdiv"></div> <script type="text/javascript"> g = new Dygraph( // containing div document.getElementById("graphdiv"), // CSV or path to a CSV file. "Date,Temperature\n" + "2008-05-07,75\n" + "2008-05-08,70\n" + "2008-05-09,80\n" ); </script> </body> </html>
In order to keep this example self-contained, the second parameter is raw CSV data. The dygraphs library parses this data (including column headers), resizes its container to a reasonable default, calculates appropriate axis ranges and tick marks and draws the graph.
In most applications, it makes more sense to include a CSV file instead. If the second parameter to the constructor doesn't contain a newline, it will be interpreted as the path to a CSV file. The Dygraph will perform an XMLHttpRequest to retrieve this file and display the data when it becomes available. Make sure your CSV file is readable and serving from a place that understands XMLHttpRequest's! In particular, you cannot specify a CSV file using "file:///"
. Here's an example: (data from Weather Underground)
<html> <head> <script type="text/javascript" src="dygraph-combined-dev.js"></script> </head> <body> <div id="graphdiv2" style="width:500px; height:300px;"></div> <script type="text/javascript"> g2 = new Dygraph( document.getElementById("graphdiv2"), "temperatures.csv", // path to CSV file {} // options ); </script> </body> </html>
The file used is temperatures.csv
.
There are a few things to note here:
temperatures.csv
, which is Date,High,Low
.This problem can be fixed by specifying the appropriate options in the "additional options" parameter to the Dygraph constructor. To set the number of days for a moving average, use the rollPeriod
option. Here's how it's done:
<html> <head> <script type="text/javascript" src="dygraph-combined-dev.js"></script> </head> <body> <div id="graphdiv3" style="width:500px; height:300px;"></div> <script type="text/javascript"> g3 = new Dygraph( document.getElementById("graphdiv3"), "temperatures.csv", { rollPeriod: 7, showRoller: true } ); </script> </body> </html>
A rolling average can be set using the text box in the lower left-hand corner of the graph (the showRoller attribute is what makes this appear). Also note that we've explicitly set the size of the chart div.
Another significant feature of the dygraphs library is the ability to display error bars around data series. One standard deviation must be specified for each data point. A ±n sigma band will be drawn around the data series at that point. If a moving average is being displayed, dygraphs will compute the standard deviation of the average at each point. I.E. σ = sqrt( (σ12 + σ22 + ... + σn2) / n )
Here's a demonstration. There are two data series. One is N(100,10)
with a standard deviation of 10 specified at each point. The other is N(80,20)
with a standard deviation of 20 specified at each point. The CSV file was generated using Octave and can be viewed at twonormals.csv.
<html> <head> <script type="text/javascript" src="combined.js"></script> </head> <body> <div id="graphdiv4" style="width:480px; height:320px;"></div> <script type="text/javascript"> g4 = new Dygraph( document.getElementById("graphdiv4"), "twonormals.csv", { rollPeriod: 7, showRoller: true, errorBars: true, valueRange: [50,125] } ); </script> </body> </html>
Things to note here:
The Google Visualization API provides a standard interface for describing data. Once you've specified your data using this API, you can plug in any GViz-compatible visualization. dygraphs is such a visualization. In particular, it can be used as a drop-in replacement for the AnnotatedTimeline visualization used on Google Finance and other sites. To see how this works, check out the gviz annotation demo.
For a simple demonstration of how to use dygraphs a GViz visualization, see http://danvk.org/dygraphs/tests/gviz.html. dygraphs can also be used as a GViz gadget. This allows it to be embedded inside of a Google Spreadsheet. For a demonstration of this, see this spreadsheet. The URL for the gadget is http://danvk.org/dygraphs/gadget.xml
.
Here's an example of a published gviz gadget using dygraphs:
Situations often arise where you want to plot fractions, e.g. the fraction of respondents in a poll who said they'd vote for candidate X or the number of hits divided by at bats (baseball's batting average). Fractions require special treatment for two main reasons:
a1/b1
and a2/b2
is (a1+a2)/(b1+b2)
, not (a1/b1 + a2/b2)/2
.Fortunately, dygraphs handles both of these for you! Here's a chart and the command that generated it:
new Dygraph( document.getElementById("baseballdiv"), "suzuki-mariners.txt", { fractions: true, errorBars: true, showRoller: true, rollPeriod: 15 } );
The fractions
option indicates that the values in each column should be parsed as fractions (e.g. "1/2" instead of "0.5"). The errorBars
option indicates that we'd like to see a confidence interval around each data point. By default, when fractions
is set, you get a Wilson confidence interval. If you look carefully at the chart, you can see that the error bars are asymmetric.
A couple things to notice about this chart:
This chart shows monthly closes of the Dow Jones Industrial Average, both in nominal and real (i.e. adjusted for inflation) dollars. The shaded areas show its monthly high and low. CPI values with a base from 1982-84 are used to adjust for inflation.
Display:
Here are a few problems that I've frequently run into while using the dygraphs library.
YYYYMMDD, series1, series2,
… . And if you set the errorBars
property, make sure you alternate data series and standard deviations.<center>
tag. This applies to the CSS text-align
property as well. If you want to center a Dygraph, put it inside a table with align = center
set.dateWindow
property to a date. It expects milliseconds since epoch, which can be obtained from a JavaScript Date object's valueOf method.If you need to support Internet Explorer, check out our notes on IE.
To get some inspiration, look at how the charts in our gallery are built.