Sie sind auf Seite 1von 8

1/18/13

Documentation
Demo Usage IE Compatibility GViz Data Baseball chart Stock chart GWT Compatibility Data Policy Options Reference JSDoc Reference Data Formats Annotations

dygraphs JavaScript Visualization Library

dygraphs JavaScript Visualization Library


http://github.com/danvk/dygraphs See blog, mailing list, downloads, demos and open issues dygraphs is an open source JavaScript library that produces produces interactive, zoomable charts of time series. It is designed to display dense data sets and enable users to explore and interpret them.

A demo is worth a thousand words:


(Mouse over to highlight individual values. Click and drag to zoom. Double-click to zoom back out. Change the number and hit enter to adjust the averaging period.)

Project
Blog Source Issues Report Bug Contribute a change Contact

Daily Temperatures in New York vs. San Francisco


90 80 NY SF

Temperature (F)

Demos/Usage
Known Users New! (browse gallery) (browse demos) Basic Demo GViz Demo Equation Plotter Performance Test Annotations Annotations (GViz) Filled Chart Fractions Visibility Labels in a DIV Numeric Axis Dotted Points Native Format Crazy Styles Tick spacing Callbacks Hourly/M inutely data Isolated Points M issing Data Bordered chart Custom Bars Custom Label Styles M inimal Example Negative Numbers Custom Underlay / background Tests for zoom operations Log scale tests

70 60 50 40 30 20
14

Jan 07

Apr 07

Jul 07

Oct 07

Jan 08

Apr 08

Jul 08

Oct 08

Jan 09

Apr 09

Jul 09

Oct 09

Some things to notice: There's less seasonal temperature variation in SF than in NY. The difference is about 15 F for SF vs. 50 F for NY. The daily data (set rolling period to 1) is quite noisy and hides this conclusion. Using a 14-day moving average makes it clearer. A 100-day rolling period averages out nearly all the specifics from the data. There's a gap in the data for SF, when the weather station was down (zoom into October 2007 to see it). The bands around each point indicate average highs and lows. There is a lot of data in this chart: low, average and high for each city on each day of a three year period 6000 data points in all. dygraphs allows the user to explore the data and discover these facts. For more demos, browse the dygraph tests directory. To see other people who are using dygraphs, check out the known users.

Features
Some of the features of dygraphs: Plots time series without using an external server or Flash Works in Internet Explorer (using excanvas) Lightweight (69kb) and responsive Displays values on mouseover, making interaction easily discoverable Supports error bands around data series Interactive zoom Displays Annotations on the chart Adjustable averaging period Can intelligently chart fractions Customizable click-through actions Compatible with the Google Visualization API Intelligent defaults make it easy to use

Usage
dygraphs.com 1/8

1/18/13

dygraphs JavaScript Visualization Library

To use dygraphs, include the d g a h c m i e . sJavaScript file and instantiate a D g a hobject. yrp-obndj yrp Here's a basic example to get things started:

HTML
<tl hm> <ed ha> <cittp=tx/aacit srp ye"etjvsrp" sc"yrp-obndj"<srp> r=dgahcmie.s>/cit <ha> /ed <oy bd> <i i=gahi"<dv dv d"rpdv>/i> <cittp=tx/aacit> srp ye"etjvsrp" g=nwDgah e yrp( / cnann dv / otiig i dcmn.eEeetyd"rpdv) ouetgtlmnBI(gahi", / CVo pt t aCVfl. / S r ah o S ie "aeTmeauen + Dt,eprtr\" "080-77\"+ 20-50,5n "080-87\"+ 20-50,0n "080-98\" 20-50,0n ) ; <srp> /cit <bd> /oy <hm> /tl

OUTPUT
80 78 76 74 72 70 07May 06:00 12:00 18:00 08May 06:00 12:00 18:00 09May

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 " i e / / . Here's an example: (data from Weather fl:/" Underground)

HTML
<tl hm> <ed ha> <cittp=tx/aacit srp ye"etjvsrp" sc"yrp-obndj"<srp> r=dgahcmie.s>/cit <ha> /ed <oy bd> <i i=gahi2 dv d"rpdv" sye"it:0p;hih:0p;>/i> tl=wdh50x egt30x"<dv <cittp=tx/aacit> srp ye"etjvsrp" g =nwDgah 2 e yrp(

dygraphs.com

2/8

1/18/13

dygraphs JavaScript Visualization Library


dcmn.eEeetyd"rpdv", ouetgtlmnBI(gahi2) "eprtrscv,/ pt t CVfl tmeaue.s" / ah o S ie { } / otos / pin ) ; <srp> /cit <bd> /oy <hm> /tl

OUTPUT
90 80 70 60 50 40 30 Jan 07 Apr 07 Jul 07 Oct 07

The file used is t m e a u e . s . eprtrscv There are a few things to note here: The Dygraph sent off an XHR to get the temperatures.csv file. The labels were taken from the first line of t m e a u e . s , which is D t , i h L w eprtrscv aeHg,o. The Dygraph automatically chose two different, easily-distinguishable colors for the two data series. The labels on the x-axis have switched from days to months. If you zoom in, they'll switch to weeks and then days. Some heuristics are used to determine a good vertical range for the data. The idea is to make all the data visible and have human-friendly values on the axis (i.e. 200 instead of 193.4). Generally this works well. The data is very spiky. A moving average would be easier to interpret. 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 r l P r o option. Here's how it's done: oleid

HTML
<tl hm> <ed ha> <cittp=tx/aacit srp ye"etjvsrp" sc"yrp-obndj"<srp> r=dgahcmie.s>/cit <ha> /ed <oy bd> <i i=gahi3 dv d"rpdv" sye"it:0p;hih:0p;>/i> tl=wdh50x egt30x"<dv <cittp=tx/aacit> srp ye"etjvsrp" g =nwDgah 3 e yrp( dcmn.eEeetyd"rpdv", ouetgtlmnBI(gahi3) "eprtrscv, tmeaue.s" { rlPro:7 oleid , soRle:tu hwolr re } ) ; <srp> /cit <bd> /oy <hm> /tl

OUTPUT
90 80

dygraphs.com

3/8

1/18/13
70 60 50 40 30 7 Jan 07 Apr 07

dygraphs JavaScript Visualization Library

Jul 07

Oct 07

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.

Error Bars
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( (1 2 + 2 2 + ... + n 2 ) / n ) Here's a demonstration. There are two data series. One is N 1 0 1 )with a standard deviation of 10 specified at each point. The (0,0 other is N 8 , 0 with a standard deviation of 20 specified at each point. The CSV file was generated using Octave and can be (02) viewed at twonormals.csv.

HTML
<tl hm> <ed ha> <cittp=tx/aacit srp ye"etjvsrp" sc"obndj"<srp> r=cmie.s>/cit <ha> /ed <oy bd> <i i=gahi4 dv d"rpdv" sye"it:8p;hih:2p;>/i> tl=wdh40x egt30x"<dv <cittp=tx/aacit> srp ye"etjvsrp" g =nwDgah 4 e yrp( dcmn.eEeetyd"rpdv", ouetgtlmnBI(gahi4) "wnrascv, tooml.s" { rlPro:7 oleid , soRle:tu, hwolr re erras tu, roBr: re vleag:[015 auRne 5,2] } ) ; <srp> /cit <bd> /oy <hm> /tl

OUTPUT
120 110 100 90 80 70 60 50 7 Jan 07 Apr 07 Jul 07 Oct 07

dygraphs.com

4/8

1/18/13

dygraphs JavaScript Visualization Library

Things to note here: The errorBars option affects both the interpretation of the CSV file and the display of the graph. When errorBars is set to true, each line is interpreted as YYYYMMDD, A, sigma_A, B, sigma_B, The first line of the CSV file doesn't mention the error columns. In this case, it's just "Date,Series1,Series2". The averaging visibly affects the error bars. This is most clear if you crank up the rolling period to something like 100 days. For the earliest dates, there won't be 100 data points to average so the signal will be noisier. The error bars get smaller like sqrt(N) going forward in time until there's a full 100 points to average. The error bars are partially transparent. This can be seen when they overlap one another.

Internet Explorer Compatibility


The dygraphs library relies heavily on the HTML5 < a v s tag, which Microsoft Internet Explorer did not traditionally support. To cna> use Microsoft's native canvas implementation in IE9, you need to set an HTML5 doctype on your page:
<DCYEhm> !OTP tl

When IE9 is in HTML5 mode, dygraphs works just like in other modern browsers. If you want to support previous versions of Internet Explorer (IE6IE8), you'll need to include the excanvas library, which emulates the < a v s tag using VML. You can add excanvas by including the following snippet: cna>
<DCYEhm> !OTP tl <tl hm> <ed ha> <eaht-qi=XU-optbe cnet"EEuaeE;I=mltI9> mt tpeuv"-ACmail" otn=I=mltI7 EEuaeE" <-[fI]<citsc"aht/xavsj"<srp>!edf-> !-i E>srp r=pt/oecna.s>/cit<[ni]<ha> /ed

(This is surprisingly tricky because the HTML5 doctype breaks excanvas in IE8. See this discussion for details.) While VML emulation sounds like it would be slow, it works well in practice for most charts. One common gotcha to look out for: make sure you don't have any trailing commas in parameter lists, e.g.
nwDgahe,dt,{ e yrp(l aa soRle:tu, / nt taln cma hwolr re / oe riig om } )

Most browsers will ignore the trailing comma, but it will break under IE.

GViz Data
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 h t : / a v . r / y r p s g d e . m . tp/dnkogdgah/agtxl Here's an example of a published gviz gadget using dygraphs:

Charting Fractions
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: The average of a / 1and a / 2is ( 1 a ) ( 1 b ) not ( 1 b + a / 2 / . 1b 2b a+2/b+2, a/1 2b)2 The normal approximation is not always applicable and more sophisticated confidence intervals (e.g. the Wilson confidence interval) must be employed to avoid ratios that exceed 100% or go below 0%. Fortunately, dygraphs handles both of these for you! Here's a chart and the command that generated it:

Batting Average for Ichiro Suzuki vs. Mariners (2004)


70

dygraphs.com

5/8

1/18/13
60 50 40 30 20 10 0 15 20

dygraphs JavaScript Visualization Library

40

60

80

100

120

140

160

Command:
nwDgah e yrp( dcmn.eEeetyd"aeali", ouetgtlmnBI(bsbldv) "uuimrnr.x" szk-aiestt, { fatos tu, rcin: re erras tu, roBr: re soRle:tu, hwolr re rlPro:1 oleid 5 } ) ;

The f a t o soption indicates that the values in each column should be parsed as fractions (e.g. "1/2" instead of "0.5"). The rcin e r r a soption indicates that we'd like to see a confidence interval around each data point. By default, when f a t o sis set, roBr rcin 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: The error bars for Ichiro's batting average are larger than for the Mariners', since he has far fewer at bats than his team. dygraphs makes it easy to see "batting average over the last 30 games". This is ordinarily quite difficult to compute. It makes it clear where the "hot" and "cold" part of Suzuki's season were. If you set the averaging period to something large, like 200, you'll see the team's and player's batting average through that game. The final number is the overall batting average for the season. Where the error bars do not overlap, we can say with 95% confidence that the series differ. There is a better than 95% chance that Ichiro was a better hitter than his team as a whole in 2004, the year he won the batting title.

One last demo


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.
D

14K 12K 10K 8K 6K 4K A 2K 0 1 1920 1930 1940 1950 1960 1970 1980 1990 2000 C

Display:

Nominal

Real

Annotations

Other Options
dygraphs.com 6/8

1/18/13

dygraphs JavaScript Visualization Library

In addition to the options mentioned above (s o R l e , r l P r o , e r r a s v l e a g ), there are many others. hwolr oleid roBr, auRne For a full list, see the Dygraphs Options Reference page.

Common Gotchas
Here are a few problems that I've frequently run into while using the dygraphs library. If your chart doesn't display, be sure to check your browser's JavaScript error console. dygraphs makes every attempt to log errors and warnings, and these can often guide you in the right direction. Make sure your CSV files are readable! If your graph isn't showing up, the XMLHttpRequest for the CSV file may be failing. You can determine whether this is the case using tools like Firebug. Make sure your CSV files are in the correct format. They must be of the form Y Y M D , s r e 1 s r e 2 . And if YYMD eis, eis, you set the e r r a sproperty, make sure you alternate data series and standard deviations. roBr dygraphs are not happy when placed inside a < e t r tag. This applies to the CSS t x - l g property as well. If you cne> etain want to center a Dygraph, put it inside a table with a i n = c n e set. lg etr Don't set the d t W n o property to a date. It expects milliseconds since epoch, which can be obtained from a JavaScript aeidw Date object's valueOf method. Make sure you don't have any trailing commas in your call to the Dygraph constructor or in the options parameter. Firefox, Chrome and Safari ignore these but they can cause a graph to not display in Internet Explorer.

GWT Compatibility
There is currently no GWT wrapper around Dygraphs, however there is a class that can be used to easily load Dygraphs into the browser. To use it, include the generated dygraph-gwt.jar file in your classpath and add the following line to your GWT module:
<neisnm=ogdnkdgah"> ihrt ae"r.av.yrps/

Call org.danvk.Dygraphs.install() when your application starts to install the JavaScript code into the browser. You can use JSNI to call Dygraphs from your GWT code, as in the example below. The example uses the Visualization API for GWT and the Dygraphs GViz API.
pbi sai ntv JvSrpOjc daDgah ulc ttc aie aacitbet rwyrp( Eeeteeet DtTbedtTbe dul mn,dul mx)/lmn lmn, aaal aaal, obe iY obe aY *{ vrcat=nw$n.yrp.Vzhr(lmn) a hr e wdDgahGiCateeet; catda(aaal, hr.rwdtTbe { vleag:[iY mx] auRne mn, aY }; ) rtr cat eun hr; }*; -/

Known Users
Since its public release in late 2009, dygraphs has found many users across the web. This is a small collection of the uses that we know about. If you're using dygraphs, please send Dan a link and he'll add it to this list. dygraphs was originally developed at Google and has found wide use on internal dashboards and servers there. There are also a few uses of dygraphs on public Google products: Google Correlate Uses dygraphs for time series visualization. Mostly a standard configuration, with just a few tweaks to match Google style. Google Correlate - Search by Drawing This is a highly customized configuration which lets the user draw a time series. Based on this demo. Google Latitude History Dashboard Uses mouse interaction callbacks to synchronize time series points with markers on a Google Map. dygraphs has also found use in other organizations: Integrated Space Weather Analysis System (NASA) We use [dygraphs] in the Integrated Space Weather Analysis System available from the Space Weather Laboratory at NASA Goddard Space Flight Center. It works quite well for time series data from various missions and simulations that we store. Eutelsat Eutelsat uses dygraphs for charting spacecraft telemetry for a fleet of 25 geostationary satellites. The spacecraft
dygraphs.com

engineers are very happy with it. All satellite combined are producing about 200 millions unique data points per day 7/8

1/18/13

dygraphs JavaScript Visualization Library

engineers are very happy with it. All satellite combined are producing about 200 millions unique data points per day so we really appreciate the excellent performance of dygraphs. 10gen MongoDB Monitoring Service A free monitoring service for MongoDB from 10gen (the creators of MongoDB). Used by thousands of servers and users. Makes use of synchronized charts to display many quantities simultaneously. Duck Duck Go Traffic Dashboard DDG uses dygraphs to display a public chart of their daily traffic. They use annotations and the moving average features. Wikimedia Foundation - Moodbar data dashboard dygraphs is used internally at Wikimedia as a handy solution to monitor the results of a bunch of small experiments. quadrant-framework (MySQL Load Testing Framework) A user friendly framework for creating and visualizing MySQL database load test jobs. For more information on its use of dygraphs, see this post. Spinwave Systems (Home energy monitoring) dygraphs is used to chart energy usage over time. Jwebchart jWebChart is a stand-alone and Thredds' embedded plotting system for netCDF files. NetCDF is a common standard for the storage and distribution of scientific data. n-gramas - Explore las tendencias en los artculos periodsticos de Colombia. (English: "Explore trends in newspaper articles of Colombia"). dygraphs is used for displaying the results of this ngrams viewer. Uses an extension for exporting the plots as PNG images ([1], [2]). Are you using dygraphs? Please let Dan know and he'll add your link here!

Data Policy
dygraphs is purely client-side JavaScript. It does not send your data to any servers the data is processed entirely in the client's browser.
Created May 9, 2008 by Dan Vanderkam

dygraphs.com

8/8

Das könnte Ihnen auch gefallen