|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Create Chart with a Legend</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Create Chart with a Legend</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#mychart { |
|
|
30 |
margin:10px 10px 10px 10px; |
|
|
31 |
width:90%; |
|
|
32 |
max-width: 800px; |
|
|
33 |
height:400px; |
|
|
34 |
} |
|
|
35 |
</style> |
|
|
36 |
<div class="intro"> |
|
|
37 |
<p>This example shows how to add a legend to a <code>Chart</code>.</p> |
|
|
38 |
</div> |
|
|
39 |
<div class="example"> |
|
|
40 |
<div id="mychart"></div> |
|
|
41 |
<script type="text/javascript"> |
|
|
42 |
(function() { |
|
|
43 |
YUI().use('charts-legend', function (Y) |
|
|
44 |
{ |
|
|
45 |
var myDataValues = [ |
|
|
46 |
{date:"5/1/2010", miscellaneous:2000, expenses:3700, revenue:2200}, |
|
|
47 |
{date:"5/2/2010", miscellaneous:50, expenses:9100, revenue:100}, |
|
|
48 |
{date:"5/3/2010", miscellaneous:400, expenses:1100, revenue:1500}, |
|
|
49 |
{date:"5/4/2010", miscellaneous:200, expenses:1900, revenue:2800}, |
|
|
50 |
{date:"5/5/2010", miscellaneous:5000, expenses:5000, revenue:2650} |
|
|
51 |
]; |
|
|
52 |
|
|
|
53 |
var myChart = new Y.Chart({ |
|
|
54 |
legend: { |
|
|
55 |
position: "right", |
|
|
56 |
width: 300, |
|
|
57 |
height: 300, |
|
|
58 |
styles: { |
|
|
59 |
hAlign: "center", |
|
|
60 |
hSpacing: 4 |
|
|
61 |
} |
|
|
62 |
}, |
|
|
63 |
axes: { |
|
|
64 |
category: { |
|
|
65 |
keys: ["date"], |
|
|
66 |
type: "category", |
|
|
67 |
styles: { |
|
|
68 |
label: { |
|
|
69 |
rotation: -90 |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
} |
|
|
73 |
}, |
|
|
74 |
categoryKey: "date", |
|
|
75 |
dataProvider:myDataValues, |
|
|
76 |
horizontalGridlines: true, |
|
|
77 |
verticalGridlines: true, |
|
|
78 |
render:"#mychart" |
|
|
79 |
}); |
|
|
80 |
|
|
|
81 |
}); |
|
|
82 |
})(); |
|
|
83 |
</script> |
|
|
84 |
|
|
|
85 |
</div> |
|
|
86 |
<h3>Adding a legend to a <code>Chart</code> instance</h3> |
|
|
87 |
|
|
|
88 |
<p>When a chart has multiple series, a legend can allow the user to identify each series more easily. The <code>charts</code> module includes a <code>charts-legend</code> submodule. Specifying |
|
|
89 |
<code>charts-legend</code> in you use statement allows you to add a legend to a <code>Chart</code> instance. A legend is added to a chart through the <code>legend</code> attribute. This attribute is an |
|
|
90 |
object containing value pairs for the attributes of a legend. All available attributes for the <code>legend</code> are outlined <a href="index.html#usinglegend">here</a>. In the |
|
|
91 |
example below, we will add a legend to a chart instance.</p> |
|
|
92 |
|
|
|
93 |
<h4>CSS</h4> |
|
|
94 |
<pre class="code prettyprint">#mychart { |
|
|
95 |
margin:10px 10px 10px 10px; |
|
|
96 |
width:90%; |
|
|
97 |
max-width: 800px; |
|
|
98 |
height:400px; |
|
|
99 |
}</pre> |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
<h4>HTML</h4> |
|
|
103 |
<pre class="code prettyprint"><div id="mychart"></div></pre> |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
<h4>JavaScript</h4> |
|
|
107 |
<pre class="code prettyprint">YUI().use('charts-legend', function (Y) |
|
|
108 |
{ |
|
|
109 |
var myDataValues = [ |
|
|
110 |
{date:"5/1/2010", miscellaneous:2000, expenses:3700, revenue:2200}, |
|
|
111 |
{date:"5/2/2010", miscellaneous:50, expenses:9100, revenue:100}, |
|
|
112 |
{date:"5/3/2010", miscellaneous:400, expenses:1100, revenue:1500}, |
|
|
113 |
{date:"5/4/2010", miscellaneous:200, expenses:1900, revenue:2800}, |
|
|
114 |
{date:"5/5/2010", miscellaneous:5000, expenses:5000, revenue:2650} |
|
|
115 |
]; |
|
|
116 |
|
|
|
117 |
var myChart = new Y.Chart({ |
|
|
118 |
legend: { |
|
|
119 |
position: "right", |
|
|
120 |
width: 300, |
|
|
121 |
height: 300, |
|
|
122 |
styles: { |
|
|
123 |
hAlign: "center", |
|
|
124 |
hSpacing: 4 |
|
|
125 |
} |
|
|
126 |
}, |
|
|
127 |
axes: { |
|
|
128 |
category: { |
|
|
129 |
keys: ["date"], |
|
|
130 |
type: "category", |
|
|
131 |
styles: { |
|
|
132 |
label: { |
|
|
133 |
rotation: -90 |
|
|
134 |
} |
|
|
135 |
} |
|
|
136 |
} |
|
|
137 |
}, |
|
|
138 |
categoryKey: "date", |
|
|
139 |
dataProvider:myDataValues, |
|
|
140 |
horizontalGridlines: true, |
|
|
141 |
verticalGridlines: true, |
|
|
142 |
render:"#mychart" |
|
|
143 |
}); |
|
|
144 |
|
|
|
145 |
});</pre> |
|
|
146 |
|
|
|
147 |
</div> |
|
|
148 |
</div> |
|
|
149 |
</div> |
|
|
150 |
|
|
|
151 |
<div class="yui3-u-1-4"> |
|
|
152 |
<div class="sidebar"> |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
<div class="sidebox"> |
|
|
157 |
<div class="hd"> |
|
|
158 |
<h2 class="no-toc">Examples</h2> |
|
|
159 |
</div> |
|
|
160 |
|
|
|
161 |
<div class="bd"> |
|
|
162 |
<ul class="examples"> |
|
|
163 |
|
|
|
164 |
|
|
|
165 |
<li data-description="Shows how to use Charts to create a basic chart."> |
|
|
166 |
<a href="charts-simple.html">Basic Charts Implementation</a> |
|
|
167 |
</li> |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
<li data-description="Shows how to create a chart with multiple series."> |
|
|
172 |
<a href="charts-multiseries.html">Chart with Multiple Series</a> |
|
|
173 |
</li> |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
<li data-description="Shows how to create a column chart with multiple series."> |
|
|
178 |
<a href="charts-column.html">Specify Chart Type</a> |
|
|
179 |
</li> |
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<li data-description="Shows how to create a column chart with a stacked numeric axis."> |
|
|
184 |
<a href="charts-stackedcolumn.html">Create Stacked Chart</a> |
|
|
185 |
</li> |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
<li data-description="Shows how to create a chart with a time axis."> |
|
|
190 |
<a href="charts-timeaxis.html">Create a Chart with a Time Axis</a> |
|
|
191 |
</li> |
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
<li data-description="Shows how to add gridlines to a chart."> |
|
|
196 |
<a href="charts-gridlines.html">Add Gridlines to a Chart</a> |
|
|
197 |
</li> |
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
<li data-description="Shows how to create a chart with planar based events."> |
|
|
202 |
<a href="charts-stackedarea.html">Create a Stacked Area Chart with Planar Based Events</a> |
|
|
203 |
</li> |
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
<li data-description="Shows how to use a chart's styles attribute to customize a chart."> |
|
|
208 |
<a href="charts-globalstyles.html">Customize a Chart</a> |
|
|
209 |
</li> |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
<li data-description="Shows how to customize the default tooltip of a chart."> |
|
|
214 |
<a href="charts-customizedtooltip.html">Customize a Chart's Tooltip</a> |
|
|
215 |
</li> |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
<li data-description="Shows how to explicitly define the axes and series for a chart."> |
|
|
220 |
<a href="charts-objectstyles.html">Define a Chart's Axes and Series</a> |
|
|
221 |
</li> |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
<li data-description="Shows how to use charts to create a pie chart."> |
|
|
226 |
<a href="charts-pie.html">Pie Chart</a> |
|
|
227 |
</li> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
<li data-description="Shows how to create a chart with multiple value axes."> |
|
|
232 |
<a href="charts-dualaxes.html">Dual Axes Chart</a> |
|
|
233 |
</li> |
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
<li data-description="Shows how to access a chart instance's value axis after the chart has rendered."> |
|
|
238 |
<a href="charts-axisupdate.html">Update Chart Axis</a> |
|
|
239 |
</li> |
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
<li data-description="Shows how to access a chart instance's seriesCollection after the chart has rendered."> |
|
|
244 |
<a href="charts-seriesupdate.html">Update Chart Series</a> |
|
|
245 |
</li> |
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
<li data-description="Shows how to add a legend to a chart."> |
|
|
250 |
<a href="charts-legend.html">Create Chart with a Legend</a> |
|
|
251 |
</li> |
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
<li data-description="Shows how to render multiple data points in a singe marker."> |
|
|
256 |
<a href="charts-groupmarkers.html">Group Marker Chart</a> |
|
|
257 |
</li> |
|
|
258 |
|
|
|
259 |
|
|
|
260 |
</ul> |
|
|
261 |
</div> |
|
|
262 |
</div> |
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
</div> |
|
|
267 |
</div> |
|
|
268 |
</div> |
|
|
269 |
</div> |
|
|
270 |
|
|
|
271 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
272 |
<script>prettyPrint();</script> |
|
|
273 |
|
|
|
274 |
<script> |
|
|
275 |
YUI.Env.Tests = { |
|
|
276 |
examples: [], |
|
|
277 |
project: '../assets', |
|
|
278 |
assets: '../assets/charts', |
|
|
279 |
name: 'charts-legend', |
|
|
280 |
title: 'Create Chart with a Legend', |
|
|
281 |
newWindow: '', |
|
|
282 |
auto: false |
|
|
283 |
}; |
|
|
284 |
YUI.Env.Tests.examples.push('charts-simple'); |
|
|
285 |
YUI.Env.Tests.examples.push('charts-multiseries'); |
|
|
286 |
YUI.Env.Tests.examples.push('charts-column'); |
|
|
287 |
YUI.Env.Tests.examples.push('charts-stackedcolumn'); |
|
|
288 |
YUI.Env.Tests.examples.push('charts-timeaxis'); |
|
|
289 |
YUI.Env.Tests.examples.push('charts-gridlines'); |
|
|
290 |
YUI.Env.Tests.examples.push('charts-stackedarea'); |
|
|
291 |
YUI.Env.Tests.examples.push('charts-globalstyles'); |
|
|
292 |
YUI.Env.Tests.examples.push('charts-customizedtooltip'); |
|
|
293 |
YUI.Env.Tests.examples.push('charts-objectstyles'); |
|
|
294 |
YUI.Env.Tests.examples.push('charts-pie'); |
|
|
295 |
YUI.Env.Tests.examples.push('charts-dualaxes'); |
|
|
296 |
YUI.Env.Tests.examples.push('charts-axisupdate'); |
|
|
297 |
YUI.Env.Tests.examples.push('charts-seriesupdate'); |
|
|
298 |
YUI.Env.Tests.examples.push('charts-legend'); |
|
|
299 |
YUI.Env.Tests.examples.push('charts-groupmarkers'); |
|
|
300 |
|
|
|
301 |
</script> |
|
|
302 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|
306 |
</body> |
|
|
307 |
</html> |