6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
24
<img src="../assets/charts/img/chart-01.png" alt="Screenshot of the Charts widget" style="border: 1px solid #bfbfbf; float:right; height:150px; margin: 0 0 8px 8px; width:275px;">
25
The Charts module provides a JavaScript API for visualizing data in a variety of formats across a <a href="http://developer.yahoo.com/yui/articles/gbs">browser test baseline</a>. Based on device and browser capabilities, Charts leverages SVG, HTML Canvas and VML to render its graphical elements.
28
The Charts module features a <code>Chart</code> class that allows you to easily create a chart from a set of data. <code>Chart</code> extends <code>Widget</code> and includes configurable attributes that enable you to customize a Chart. Currently, the <code>Chart</code> widget can be used to create different variations and combinations of line, marker, area, spline, column, bar and pie charts.
31
<h2 id="getting-started">Getting Started</h2>
34
To include the source files for Charts and its dependencies, first load
35
the YUI seed file if you haven't already loaded it.
38
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script></pre>
42
Next, create a new YUI instance for your application and populate it with the
43
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
44
YUI will automatically load any dependencies required by the modules you
48
<pre class="code prettyprint">// Create a new YUI instance and populate it with the required modules.
49
YUI().use('charts', function (Y) {
50
// Charts is available and ready for use. Add implementation
51
// code here.
56
For more information on creating YUI instances and on the
57
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
58
documentation for the <a href="../yui/index.html">YUI Global object</a>.
62
<h2 id="using">Using the charts widget</h2>
64
<p>This section describes how to use the charts widget in further detail.
65
It contains these subsections:</p>
68
<h3 id="instantiating">Instantiating A Chart</h3>
70
<p>All you need to instantiate a chart is
72
<li>A div container to render the chart.</li>
73
<li>An array to provide data for the chart.</li>
78
<pre class="code prettyprint">#mychart {
83
<h4 id="html">HTML</h4>
84
<pre class="code prettyprint"><div id="mychart"></div></pre>
86
<h4 id="javascript">JavaScript</h4>
87
<pre class="code prettyprint">// Data for the chart
89
{category:"5/1/2010", values:2000},
90
{category:"5/2/2010", values:50},
91
{category:"5/3/2010", values:400},
92
{category:"5/4/2010", values:200},
93
{category:"5/5/2010", values:5000}
96
// Instantiate and render the chart
97
var mychart = new Y.Chart({
98
dataProvider: myDataValues,
99
render: "#mychart"
103
<p>By default, <code>Chart</code> creates a graph with lines and markers. This can be changed through the <code>type</code> attribute. Available values are listed below:
110
<td><code>area</code></td>
111
<td>Visualizes quantitative data with a fill between an axis and relevant data points.</td></tr>
113
<td><code>areaspline</code></td>
114
<td>An Area Chart in which data points are connected by a curve.</td>
117
<td><code>bar</code></td>
118
<td>Visualizes bars positioned vertically along a category or time axis. The bars' lengths are proportional to the values they represent along a horizontal axis.</td>
121
<td><code>column</code></td>
122
<td>Visualizes bars positioned vertically along a category or time axis. The bars' lengths are proportional to the values they represent along a horizontal axis.</td>
125
<td><code>combo</code></td>
126
<td>Combination of line, marker and area chart. By default, there is no area fill. This is the default type for a <code>Chart</code>.</td>
129
<td><code>combospline</code></td>
130
<td>A combo chart in which the data points are connected by a curve.</td>
133
<td><code>line</code></td>
134
<td>Visualizes quantitative data on a graph by connecting relevant data points.</td>
137
<td><code>markerseries</code></td>
138
<td>Visualizes quantitative data by plotting relevant data points on a graph.</td>
141
<td><code>pie</code></td>
142
<td>A circular chart divided into wedges which represent data as a percentage of a whole.</td>
145
<td><code>spline</code></td>
146
<td>Visualizes quantitative data on a graph by connecting relevant data points with a curve.</td>
151
<h3 id="chartapplication">Chart Application</h3>
152
<p>The <code>Chart</code> class acts as a facade for two underlying application classes:
154
<dt>CartesianChart</dt><dd>An application used to render multiple series to a graph with x and y axes. Many series can be rendered in a CartesianChart.</dd>
155
<dt>PieChart</dt><dd>An application used to render Pie Charts.</dd>
158
<p>When <code>Chart</code> is instantiated, the <code>type</code> attribute determines which class instance will be returned. A value of <code>pie</code> will return an instance of
159
<code>PieChart</code>. All other values will return an instance of <code>CartesianChart</code>. For the most part, this is a distinction that only occurs under the hood. As a
160
developer, this can be viewed as a single API.</p>
162
<h3 id="attributes">Attributes</h3>
164
<p>The <code>Chart</code> widget adds the following key attributes, in addition to the attributes provided by the base <a href="../widget/index.html#attributes">Widget</a> class:</p>
172
<td><code>axes</code></td>
173
<td><code>Object</code></td>
174
<td>Axes to appear in the chart. This can be an object of axis instances or object literals used to construct the appropriate axes.</td>
177
<td><code>categoryAxis</code></td>
178
<td><code>Axis</code></td>
179
<td>Reference to the chart's category axis.</td>
182
<td><code>categoryAxisName</code></td>
183
<td><code>String</code></td>
184
<td>Indicates the key value used to identify a category axis in the <code>axes</code> hash. If not specified, the categoryKey attribute value will be used.</td>
187
<td><code>categoryKey</code></td>
188
<td><code>String</code></td>
189
<td>The key value used for the chart's category axis. The default value is category.</td>
192
<td><code>categoryType</code></td>
193
<td><code>String</code></td>
194
<td>Indicates whether to use a <code>CategoryAxis</code> or <code>TimeAxis</code> for the <code>Chart</code> instance's category axis. The default value is category.</td>
197
<td><code>dataProvider</code></td>
198
<td><code>Array</code></td>
199
<td>Array of data used to construct the chart.</td>
202
<td><code>direction</code></td>
203
<td><code>String</code></td>
204
<td>Direction of chart's category axis when there is no series collection specified. Charts can be horizontal or vertical. When the chart type is column, the chart is horizontal.
205
When the chart type is bar, the chart is vertical.</td>
208
<td><code>horizontalGridlines</code></td>
209
<td><code>Gridlines</code></td>
210
<td>Reference to the horizontalGridlines for a cartesian chart.</td>
213
<td><code>interactionType</code></td>
214
<td><code>String</code></td>
215
<td>Indicates the the <code>Chart</code> instance will fire <code>marker</code> or <code>planar</code> events. The default value is marker.</td>
218
<td><code>seriesCollection</code></td>
219
<td><code>Array</code></td><td>Collection of series to appear on the chart. This can be an array of Series instances or object literals used to construct the appropriate series.
223
<td><code>seriesKeys</code></td>
224
<td><code>Array</code></td>
225
<td>A collection of keys that map to the series axes. If no keys are set, they will be generated automatically depending on the data structure passed into the chart.</td>
228
<td><code>showAreaFill</code></td>
229
<td><code>Boolean</code></td>
230
<td>Indicates whether or not an area is filled in a combo chart.</td>
233
<td><code>showLines</code></td>
234
<td><code>Boolean</code></td>
235
<td>Indicates whether to display lines in a combo chart.</td>
238
<td><code>showMarkers</code></td>
239
<td><code>Boolean</code></td>
240
<td>Indicates whether to display markers in a combo chart.</td>
243
<td><code>stacked</code></td>
244
<td><code>Boolean</code></td>
245
<td>Indicates whether or not the chart is stacked.</td>
248
<td><code>styles</code></td>
249
<td><code>Object</code></td>
250
<td>properties for the chart.</td>
253
<td><code>tooltip</code></td>
254
<td><code>Object</code></td>
255
<td>Reference to the default tooltip available for the chart.</td>
258
<td><code>type</code></td>
259
<td><code>String</code></td>
260
<td>Indicates the default series type for the chart. The default value is <code>combo</code></td>
263
<td><code>valueAxisName</code></td>
264
<td><code>String</code></td>
265
<td>Indicates the key value used to identify the default value axis.</td>
268
<td><code>verticalGridlines</code></td>
269
<td><code>Gridlines</code></td>
270
<td>Reference to the verticalGridlines for a cartesian chart.</td>
274
<h3 id="dataProvider">The <code>dataProvider</code> Attribute</h3>
275
<p>The only required attributes for instantiating a <code>Chart</code> instance are <code>dataProvider</code> and <code>render</code>. The <code>render</code> attribute can be included
276
in the configuration argument or called explicitly after instantiation.</p>
277
<pre class="code prettyprint">mychart.render("#mychart");</pre>
280
<p>The <code>Chart</code> widget requires an array for its source of data. The <code>Chart</code> widget will accept an array of object literals or arrays. When an array of arrays
281
is received, the values in the first index will be used for the category axis and all subsequent indices will be used for the value axis/axes. When an array of object literals is
282
received, all records with a key matching the <code>categoryKey</code> attribute will be used for the category axis with all other records used for the value axis/axes.</p>
283
<h4 id="multi-dimensional-array">Multi-dimensional Array</h4>
284
<pre class="code prettyprint">var myDataValues = [
285
["5/1/2010", "5/2/2010", "5/3/2010", "5/4/2010", "5/5/2010"],
286
[2000, 50, 400, 200, 5000]
289
<h4 id="object-literal-array">Object Literal Array</h4>
290
<pre class="code prettyprint">var myDataValues = [
291
{category:"5/1/2010", values:2000},
292
{category:"5/2/2010", values:50},
293
{category:"5/3/2010", values:400},
294
{category:"5/4/2010", values:200},
295
{category:"5/5/2010", values:5000}
298
<p>The underlying structure of the <code>dataProvider</code> is an array of object literals. If a <code>Chart</code> receives a multi-dimensional array for its
299
<code>dataProvider</code>, it will convert the array to an array of object literals.</p>
301
<h3 id="usingtooltip">Using the <code>tooltip</code> Attribute</h3>
302
<p>The <code>Chart</code> class comes with a built-in simple tooltip. This tooltip can be customized or disabled with the <code>tooltip</code> attribute which contains the following
306
<tr><th>Property</th><th>Type</th><th>Description</th></tr>
307
<tr><td><code>hideEvent</code></td><td><code>String</code>/<code>Array</code></td><td>Event that hides the tooltip. This allows you to specify which mouse event(s) hides the tooltip. You can also pass this an array of events and each event in the array will hide the tooltip. The default value is <code>mouseout</code>.</td></tr>
308
<tr><td><code>markerEventHandler</code></td><td><code>Function</code></td><td>Displays and hides a tooltip based on marker events.</td></tr>
309
<tr><td><code>markerLabelFunction</code></td><td><code>Function</code></td><td>Reference to the function used to format a marker event triggered tooltip's text. The markerLabelFunction has the following arguments:
311
<dt>categoryItem</dt><dd>An object containing the following:
313
<dt>axis</dt><dd>The axis that the category is bound to</dd>
314
<dt>displayName</dt><dd>The display name set to the category (defaults to key if not provided)</dd>
315
<dt>key</dt><dd>The key of the category</dd>
316
<dt>value</dt><dd>The value of the category</dd>
319
<dt>valueItem</dt><dd>An object containing the following:
321
<dt>axis</dt><dd>The axis that the item's series is bound to</dd>
322
<dt>displayName</dt><dd>The display name of the series (defaults to key if not provided)</dd>
323
<dt>key</dt><dd>The key for the series</dd>
324
<dt>value</dt><dd>The value for the series item<dd>
327
<dt>itemIndex</dt><dd>The index of the item within its series.</dd>
328
<dt>series</dt><dd>The series that the item belongs to</dd>
329
<dt>seriesIndex</dt><dd>The index of the series in the seriesCollection</dd>
332
<tr><td><code>node</code></td><td><code>HTMLElement</code></td><td>Reference (read-only) to the actual dom node of the tooltip.</td></tr>
333
<tr><td><code>planarEventHandler</code></td><td><code>Function</code></td><td>Displays and hides a tooltip based on planar events.</td></tr>
334
<tr><td><code>planarLabelFunction</code></td><td><code>Function</code></td><td>Reference to the function used to format a planar event triggered tooltip's text. The <code>planarLabelFunction</code> has the following arguments:
336
<dt>categoryAxis</dt><dd>Reference to the categoryAxis of the chart.</dd>
337
<dt>valueItems</dt><dd>Array of objects for each series that has a data point in the coordinate plane of the event. Each object contains the following data:
339
<dt>axis</dt><dd>The value axis of the series.</dd>
340
<dt>key</dt><dd>The key for the series.</dd>
341
<dt>value</dt><dd>The value for the series item.</dd>
342
<dt>displayName</dt><dd>The display name of the series. (defaults to key if not provided)</dd>
345
<dt><dt>index</dt><dd>The index of the item within its series.</dd>
346
<dt>seriesArray</dt><dd>Array of series instances for each value item.</dd>
347
<dt>seriesIndex</dt><dd>The index of the series in the <code>seriesCollection</code>.</dd>
350
<tr><td><code>show</code></td><td><code>Boolean</code></td><td>Indicates whether to show a tooltip.</td></tr>
351
<tr><td><code>showEvent</code></td><td><code>String</code></td><td>Event that triggers the tooltip. This allows you to specify which mouse event will cause the tooltip to display. The default value is <code>mouseover</code></td></tr>
352
<tr><td><code>styles</code></td><td><code>Object</code></td><td>Hash of CSS styles that are applied to the tooltip's node.</td></tr>
355
<h3 id="usingstyles">Styling a <code>CartesianChart</code> with the <code>styles</code> Attribute</h3>
356
<p>The <code>styles</code> attribute can be used to update the properties of different chart components in a <code>CartesianChart</code>.
359
<tr><th>Property</th><th>Type</th><th>Description</th></tr>
360
<tr><td><code>axes</code></td><td><code>Object</code></td><td>An object containing references to the <code>styles</code> attribute for each <code>Axis</code> instance in the chart.</td></tr>
361
<tr><td><code>graph</code></td><td><code>Object</code></td><td>A reference to the <code>styles</code> attribute of the chart applications's <code>Graph</code>.</td></tr>
362
<tr><td><code>series</code></td><td><code>Object</code></td><td>An object containing references to the <code>styles</code> attribute for each <code>CartesianSeries</code> instance in the chart.</td></tr>
366
<h3 id="usingaxes">Using the <code>axes</code> Attribute</h3>
367
<p>The <code>axes</code> attribute allows you to specify axes to be used in the chart. The <code>axes</code> attribute contains a hash of either <code>Axis</code> instances or
368
object literals containing information that the <code>Chart</code> will use to create axes. The most common use case is to use object literals. Below are the attributes available:
371
<tr><th>Property</th><th>Type</th><th>Description</th></tr>
372
<tr><td><code>alwaysShowZero</code></td><td><code>Boolean</code></td><td>Ensures that zero appears on a <code>NumericAxis</code> when <code>minimum</code> and <code>maximum</code> are not explicitly set.</td></tr>
373
<tr><td><code>keys</code></td><td><code>Array</code></td><td>An array keys used to bind data from the <code>dataProvider</code> to the axis.</td></tr>
374
<tr><td><code>labelFormat</code></td><td><code>Object</code></td><td>Template for formatting labels. Used by <code>labelFunction</code> in <code>NumericAxis</code> and <code>TimeAxis</code> instances. For <code>TimeAxis</code> instances the <code>labelFormat</code> is an <code>STRFTime</code> string. For <code>NumericAxis</code> instances the <code>labelFormat</code> is an object literal containing the following properties:
377
<li>thousandsSeparator</li>
378
<li>decimalSeparator</li>
379
<li>decimalPlaces</li>
382
<tr><td><code>labelFunction</code></td><td><code>Function</code></td><td>Function used to format label for display.</td></tr>
383
<tr><td><code>maximum</code></td><td><code>Object</code></td><td>The maximum value to display on an axis. (<code>TimeAxis</code> and <code>NumericAxis</code> only)</td></tr>
384
<tr><td><code>minimum</code></td><td><code>Object</code></td><td>The minimum value to display on an axis. (<code>TimeAxis</code> and <code>NumericAxis</code> only)</td></tr>
385
<tr><td><code>position</code></td><td><code>String</code></td><td>Position in relationship to the graph in which to place the axis. (top, right, bottom, left)</td></tr>
386
<tr><td><code>roundingMethod</code></td><td><code>String</code></td><td>Algorithm used for rounding units on a <code>NumericAxis</code> when <code>minimum</code> and <code>maximum</code> are not explicitly set.</td></tr>
390
<h3 id="referenceseriesandaxis">Referencing Series and Axis Instances</h3>
391
<p>Sometimes you'll want to update an axis or a series after a chart has been instantiatied. This can be done with the <code>Chart</code>'s <code>getAxisByKey</code> and
392
<code>getSeries</code> methods. The <code>getAxisByKey</code> method looks up and returns an <code>Axis</code> instance based on its a key reference.</h4>
393
<h4 id="using-getaxisbykey">Using <code>getAxisByKey</code></h4>
394
<pre class="code prettyprint">var leftAxis = mychart.getAxisByKey("values");
395
leftAxis.set("styles", {label:{rotation:-45}});</pre>
398
<p>The <code>getSeries</code> method will accept either an index or a key reference and return a series.</p>
399
<h4 id="using-getseries-with-a-key-">Using <code>getSeries</code> with a Key </h4>
400
<pre class="code prettyprint">var mySeries = mychart.getSeries("category");
401
mySeries.set("visible", false);</pre>
404
<h4 id="using-getseries-with-an-index-">Using <code>getSeries</code> with an Index </h4>
405
<pre class="code prettyprint">var mySeries = mychart.getSeries(0);
406
mySeries.set("visible", false);</pre>
409
<h2 id="issues">Known Issues</h2>
413
Charts load slowly in android devices. Performance optimizations will need to be added in a future release.
418
Planar interaction with chart types that do not include markers can be confusing. It is not readily apparent where to mouseover to display tooltips. This will be addressed in a future release.
423
Default mouse interactions are not intuitive for touch devices. For example, tooltips show and hide on <code>mouseover</code> and <code>mouseout</code> events. Analysis needs to be done to determine
424
the appropriate default events for touch devices. See the <a href="#usingtooltip"> Using the <code>tooltip</code> Attribute</a> section to
425
see how to customize mouse events for the chart.
432
<div id="sidebar" class="yui3-u">
434
<div id="toc" class="sidebox">
436
<h2 class="no-toc">Table of Contents</h2>
442
<a href="#getting-started">Getting Started</a>
445
<a href="#using">Using the charts widget</a>
448
<a href="#instantiating">Instantiating A Chart</a>
451
<a href="#css">CSS</a>
454
<a href="#html">HTML</a>
457
<a href="#javascript">JavaScript</a>
462
<a href="#chartapplication">Chart Application</a>
465
<a href="#attributes">Attributes</a>
468
<a href="#dataProvider">The <code>dataProvider</code> Attribute</a>
471
<a href="#multi-dimensional-array">Multi-dimensional Array</a>
474
<a href="#object-literal-array">Object Literal Array</a>
479
<a href="#usingtooltip">Using the <code>tooltip</code> Attribute</a>
482
<a href="#usingstyles">Styling a <code>CartesianChart</code> with the <code>styles</code> Attribute</a>
485
<a href="#usingaxes">Using the <code>axes</code> Attribute</a>
488
<a href="#referenceseriesandaxis">Referencing Series and Axis Instances</a>
491
<a href="#using-getaxisbykey">Using <code>getAxisByKey</code></a>
494
<a href="#using-getseries-with-a-key-">Using <code>getSeries</code> with a Key </a>
497
<a href="#using-getseries-with-an-index-">Using <code>getSeries</code> with an Index </a>
504
<a href="#issues">Known Issues</a>
512
<div class="sidebox">
514
<h2 class="no-toc">Examples</h2>
518
<ul class="examples">
521
<li data-description="Shows how to use Charts to create a basic chart.">
522
<a href="charts-simple.html">Basic Charts Implementation</a>
527
<li data-description="Shows how to create a chart with multiple series.">
528
<a href="charts-multiseries.html">Chart with Multiple Series</a>
533
<li data-description="Shows how to create a column chart with multiple series.">
534
<a href="charts-column.html">Specify Chart Type</a>
539
<li data-description="Shows how to create a column chart with a stacked numeric axis.">
540
<a href="charts-stackedcolumn.html">Create Stacked Chart</a>
545
<li data-description="Shows how to create a chart with a time axis.">
546
<a href="charts-timeaxis.html">Create a Chart with a Time Axis</a>
551
<li data-description="Shows how to add gridlines to a chart.">
552
<a href="charts-gridlines.html">Add Gridlines to a Chart</a>
557
<li data-description="Shows how to create a chart with planar based events.">
558
<a href="charts-stackedarea.html">Create a Stacked Area Chart with Planar Based Events</a>
563
<li data-description="Shows how to use a chart's styles attribute to customize a chart.">
564
<a href="charts-globalstyles.html">Customize a Chart</a>
569
<li data-description="Shows how to customize the default tooltip of a chart.">
570
<a href="charts-customizedtooltip.html">Customize a Chart's Tooltip</a>
575
<li data-description="Shows how to explicitly define the axes and series for a chart.">
576
<a href="charts-objectstyles.html">Define a Chart's Axes and Series</a>
581
<li data-description="Shows how to use charts to create a pie chart.">
582
<a href="charts-pie.html">Pie Chart</a>
587
<li data-description="Shows how to create a chart with multiple value axes.">
588
<a href="charts-dualaxes.html">Dual Axes Chart</a>
593
<li data-description="Shows how to access a chart instance's value axis after the chart has rendered.">
594
<a href="charts-axisupdate.html">Update Chart Axis</a>
599
<li data-description="Shows how to access a chart instance's seriesCollection after the chart has rendered.">
600
<a href="charts-seriesupdate.html">Update Chart Series</a>
614
<script src="../assets/vendor/prettify/prettify-min.js"></script>
615
<script>prettyPrint();</script>