|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Update Chart Axis</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: Update Chart Axis</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 |
|
|
|
36 |
.fields label { |
|
|
37 |
font-weight:bold; |
|
|
38 |
display:block; |
|
|
39 |
float:left; |
|
|
40 |
width:8em; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
.fields { |
|
|
44 |
border-top:1px solid #aaa; |
|
|
45 |
padding:10px; |
|
|
46 |
} |
|
|
47 |
</style> |
|
|
48 |
<div class="intro"> |
|
|
49 |
<p>This example shows how to access <code>Chart</code> instance's value axis after the <code>Chart</code> has rendered.</p> |
|
|
50 |
</div> |
|
|
51 |
<div class="example"> |
|
|
52 |
<div id="mychart"></div> |
|
|
53 |
<form id="changeValue" class="fields" action="#"> |
|
|
54 |
<div class="body"> |
|
|
55 |
<p> |
|
|
56 |
<label for="axisSelector">axis:</label> |
|
|
57 |
<select name="axisSelector" id="axisSelector"> |
|
|
58 |
<option value="dateRange">dateRange</option> |
|
|
59 |
<option value="financials">financials</option> |
|
|
60 |
</select> |
|
|
61 |
</p> |
|
|
62 |
<p> |
|
|
63 |
<label for="color">color:</label> |
|
|
64 |
<input type="text" name="color" id="color" /> |
|
|
65 |
</p> |
|
|
66 |
<p> |
|
|
67 |
<label for="rotation">rotation:</label> |
|
|
68 |
<input type="text" name="rotation" id="rotation" /> |
|
|
69 |
</p> |
|
|
70 |
</div> |
|
|
71 |
<div class="footer"> |
|
|
72 |
<p> |
|
|
73 |
<button type="button" class="action" id="updateAxis">Update Axis</button> |
|
|
74 |
</p> |
|
|
75 |
</div> |
|
|
76 |
</form> |
|
|
77 |
<script type="text/javascript"> |
|
|
78 |
(function() { |
|
|
79 |
YUI().use('escape', 'charts', function (Y) |
|
|
80 |
{ |
|
|
81 |
//dataProvider source |
|
|
82 |
var myDataValues = [ |
|
|
83 |
{date:"1/1/2010", miscellaneous:2000, expenses:3700, revenue:2200}, |
|
|
84 |
{date:"2/1/2010", miscellaneous:5000, expenses:9100, revenue:100}, |
|
|
85 |
{date:"3/1/2010", miscellaneous:4000, expenses:1900, revenue:1500}, |
|
|
86 |
{date:"4/1/2010", miscellaneous:3000, expenses:3900, revenue:2800}, |
|
|
87 |
{date:"5/1/2010", miscellaneous:500, expenses:7000, revenue:2650}, |
|
|
88 |
{date:"6/1/2010", miscellaneous:3000, expenses:4700, revenue:1200} |
|
|
89 |
]; |
|
|
90 |
|
|
|
91 |
//Define our axes for the chart. |
|
|
92 |
var myAxes = { |
|
|
93 |
financials:{ |
|
|
94 |
keys:["miscellaneous", "revenue", "expenses"], |
|
|
95 |
position:"right", |
|
|
96 |
type:"numeric", |
|
|
97 |
styles:{ |
|
|
98 |
majorTicks:{ |
|
|
99 |
display: "none" |
|
|
100 |
} |
|
|
101 |
} |
|
|
102 |
}, |
|
|
103 |
dateRange:{ |
|
|
104 |
keys:["date"], |
|
|
105 |
position:"bottom", |
|
|
106 |
type:"category", |
|
|
107 |
styles:{ |
|
|
108 |
majorTicks:{ |
|
|
109 |
display: "none" |
|
|
110 |
}, |
|
|
111 |
label: { |
|
|
112 |
rotation:-45, |
|
|
113 |
margin:{top:5} |
|
|
114 |
} |
|
|
115 |
} |
|
|
116 |
} |
|
|
117 |
}; |
|
|
118 |
|
|
|
119 |
//instantiate the chart |
|
|
120 |
var myChart = new Y.Chart({ |
|
|
121 |
type:"column", |
|
|
122 |
categoryKey:"date", |
|
|
123 |
dataProvider:myDataValues, |
|
|
124 |
axes:myAxes, |
|
|
125 |
horizontalGridlines: true, |
|
|
126 |
verticalGridlines: true, |
|
|
127 |
render:"#mychart" |
|
|
128 |
}); |
|
|
129 |
|
|
|
130 |
//Click handler |
|
|
131 |
Y.on("click", function(e) { |
|
|
132 |
var axisName = Y.one("#axisSelector").get("value"), |
|
|
133 |
rotation = parseFloat(Y.one("#rotation").get("value")), |
|
|
134 |
color = Y.Escape.html(Y.one("#color").get("value")), |
|
|
135 |
label = null, |
|
|
136 |
axis; |
|
|
137 |
if(axisName) |
|
|
138 |
{ |
|
|
139 |
axis = myChart.getAxisByKey(axisName); |
|
|
140 |
if(!isNaN(rotation)) |
|
|
141 |
{ |
|
|
142 |
label = {rotation:rotation}; |
|
|
143 |
} |
|
|
144 |
if(color) |
|
|
145 |
{ |
|
|
146 |
if(!label) |
|
|
147 |
{ |
|
|
148 |
label = {}; |
|
|
149 |
} |
|
|
150 |
label.color = color; |
|
|
151 |
} |
|
|
152 |
if(label) |
|
|
153 |
{ |
|
|
154 |
axis.set("styles", {label:label}); |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
}, "#updateAxis"); |
|
|
158 |
}); |
|
|
159 |
})(); |
|
|
160 |
</script> |
|
|
161 |
|
|
|
162 |
</div> |
|
|
163 |
<h3>Access and Update a <code>Chart</code> Instance's Axis.</h3> |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
<p>Often times, you will need to update a chart after it has been rendered. This example demonstrates how to access and update an axis. Specifically, we'll update the rotation and color of |
|
|
167 |
the axis labels.</p> |
|
|
168 |
<p>A <code>Chart</code> instance's axes can be accessed through the <code>getAxisByKey</code> method. This method takes the axis' key identifier as an argument. If you have explicitly set your |
|
|
169 |
axis through the <code>axes</code> attribute, you will know the key identifier. If not, the default key identifier for the value axis is "values" and the default key identifier for the category |
|
|
170 |
axis is <code>category</code>. Once you have a reference for the axis, you can update all of its public attributes.</p> |
|
|
171 |
|
|
|
172 |
<h4>CSS</h4> |
|
|
173 |
<pre class="code prettyprint">#mychart { |
|
|
174 |
margin:10px 10px 10px 10px; |
|
|
175 |
width:90%; |
|
|
176 |
max-width: 800px; |
|
|
177 |
height:400px; |
|
|
178 |
}</pre> |
|
|
179 |
|
|
|
180 |
|
|
|
181 |
<h4>HTML</h4> |
|
|
182 |
<pre class="code prettyprint"><div id="mychart"></div></pre> |
|
|
183 |
|
|
|
184 |
|
|
|
185 |
<h4>JavaScript</h4> |
|
|
186 |
<pre class="code prettyprint">YUI().use('charts', function (Y) |
|
|
187 |
{ |
|
|
188 |
//dataProvider source |
|
|
189 |
var myDataValues = [ |
|
|
190 |
{date:"1/1/2010", miscellaneous:2000, expenses:3700, revenue:2200}, |
|
|
191 |
{date:"2/1/2010", miscellaneous:5000, expenses:9100, revenue:100}, |
|
|
192 |
{date:"3/1/2010", miscellaneous:4000, expenses:1900, revenue:1500}, |
|
|
193 |
{date:"4/1/2010", miscellaneous:3000, expenses:3900, revenue:2800}, |
|
|
194 |
{date:"5/1/2010", miscellaneous:500, expenses:7000, revenue:2650}, |
|
|
195 |
{date:"6/1/2010", miscellaneous:3000, expenses:4700, revenue:1200} |
|
|
196 |
]; |
|
|
197 |
|
|
|
198 |
//Define our axes for the chart. |
|
|
199 |
var myAxes = { |
|
|
200 |
financials:{ |
|
|
201 |
keys:["miscellaneous", "revenue", "expenses"], |
|
|
202 |
position:"right", |
|
|
203 |
type:"numeric", |
|
|
204 |
styles:{ |
|
|
205 |
majorTicks:{ |
|
|
206 |
display: "none" |
|
|
207 |
} |
|
|
208 |
} |
|
|
209 |
}, |
|
|
210 |
dateRange:{ |
|
|
211 |
keys:["date"], |
|
|
212 |
position:"bottom", |
|
|
213 |
type:"category", |
|
|
214 |
styles:{ |
|
|
215 |
majorTicks:{ |
|
|
216 |
display: "none" |
|
|
217 |
}, |
|
|
218 |
label: { |
|
|
219 |
rotation:-45, |
|
|
220 |
margin:{top:5} |
|
|
221 |
} |
|
|
222 |
} |
|
|
223 |
} |
|
|
224 |
}; |
|
|
225 |
|
|
|
226 |
//instantiate the chart |
|
|
227 |
var myChart = new Y.Chart({ |
|
|
228 |
type:"column", |
|
|
229 |
categoryKey:"date", |
|
|
230 |
dataProvider:myDataValues, |
|
|
231 |
axes:myAxes, |
|
|
232 |
horizontalGridlines: true, |
|
|
233 |
verticalGridlines: true, |
|
|
234 |
render:"#mychart" |
|
|
235 |
}); |
|
|
236 |
|
|
|
237 |
//Click handler |
|
|
238 |
Y.on("click", function(e) { |
|
|
239 |
var axisName = Y.one("#axisSelector").get("value"), |
|
|
240 |
rotation = parseFloat(Y.one("#rotation").get("value")), |
|
|
241 |
color = Y.Escape.html(Y.one("#color").get("value")), |
|
|
242 |
label = null, |
|
|
243 |
axis; |
|
|
244 |
if(axisName) |
|
|
245 |
{ |
|
|
246 |
axis = myChart.getAxisByKey(axisName); |
|
|
247 |
if(!isNaN(rotation)) |
|
|
248 |
{ |
|
|
249 |
label = {rotation:rotation}; |
|
|
250 |
} |
|
|
251 |
if(color) |
|
|
252 |
{ |
|
|
253 |
if(!label) |
|
|
254 |
{ |
|
|
255 |
label = {}; |
|
|
256 |
} |
|
|
257 |
label.color = color; |
|
|
258 |
} |
|
|
259 |
if(label) |
|
|
260 |
{ |
|
|
261 |
axis.set("styles", {label:label}); |
|
|
262 |
} |
|
|
263 |
} |
|
|
264 |
}, "#updateAxis"); |
|
|
265 |
});</pre> |
|
|
266 |
|
|
|
267 |
</div> |
|
|
268 |
</div> |
|
|
269 |
</div> |
|
|
270 |
|
|
|
271 |
<div class="yui3-u-1-4"> |
|
|
272 |
<div class="sidebar"> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<div class="sidebox"> |
|
|
277 |
<div class="hd"> |
|
|
278 |
<h2 class="no-toc">Examples</h2> |
|
|
279 |
</div> |
|
|
280 |
|
|
|
281 |
<div class="bd"> |
|
|
282 |
<ul class="examples"> |
|
|
283 |
|
|
|
284 |
|
|
|
285 |
<li data-description="Shows how to use Charts to create a basic chart."> |
|
|
286 |
<a href="charts-simple.html">Basic Charts Implementation</a> |
|
|
287 |
</li> |
|
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
<li data-description="Shows how to create a chart with multiple series."> |
|
|
292 |
<a href="charts-multiseries.html">Chart with Multiple Series</a> |
|
|
293 |
</li> |
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
<li data-description="Shows how to create a column chart with multiple series."> |
|
|
298 |
<a href="charts-column.html">Specify Chart Type</a> |
|
|
299 |
</li> |
|
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
<li data-description="Shows how to create a column chart with a stacked numeric axis."> |
|
|
304 |
<a href="charts-stackedcolumn.html">Create Stacked Chart</a> |
|
|
305 |
</li> |
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
<li data-description="Shows how to create a chart with a time axis."> |
|
|
310 |
<a href="charts-timeaxis.html">Create a Chart with a Time Axis</a> |
|
|
311 |
</li> |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
<li data-description="Shows how to add gridlines to a chart."> |
|
|
316 |
<a href="charts-gridlines.html">Add Gridlines to a Chart</a> |
|
|
317 |
</li> |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
<li data-description="Shows how to create a chart with planar based events."> |
|
|
322 |
<a href="charts-stackedarea.html">Create a Stacked Area Chart with Planar Based Events</a> |
|
|
323 |
</li> |
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
<li data-description="Shows how to use a chart's styles attribute to customize a chart."> |
|
|
328 |
<a href="charts-globalstyles.html">Customize a Chart</a> |
|
|
329 |
</li> |
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
<li data-description="Shows how to customize the default tooltip of a chart."> |
|
|
334 |
<a href="charts-customizedtooltip.html">Customize a Chart's Tooltip</a> |
|
|
335 |
</li> |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
<li data-description="Shows how to explicitly define the axes and series for a chart."> |
|
|
340 |
<a href="charts-objectstyles.html">Define a Chart's Axes and Series</a> |
|
|
341 |
</li> |
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
<li data-description="Shows how to use charts to create a pie chart."> |
|
|
346 |
<a href="charts-pie.html">Pie Chart</a> |
|
|
347 |
</li> |
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
<li data-description="Shows how to create a chart with multiple value axes."> |
|
|
352 |
<a href="charts-dualaxes.html">Dual Axes Chart</a> |
|
|
353 |
</li> |
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
<li data-description="Shows how to access a chart instance's value axis after the chart has rendered."> |
|
|
358 |
<a href="charts-axisupdate.html">Update Chart Axis</a> |
|
|
359 |
</li> |
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
<li data-description="Shows how to access a chart instance's seriesCollection after the chart has rendered."> |
|
|
364 |
<a href="charts-seriesupdate.html">Update Chart Series</a> |
|
|
365 |
</li> |
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
<li data-description="Shows how to add a legend to a chart."> |
|
|
370 |
<a href="charts-legend.html">Create Chart with a Legend</a> |
|
|
371 |
</li> |
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
<li data-description="Shows how to render multiple data points in a singe marker."> |
|
|
376 |
<a href="charts-groupmarkers.html">Group Marker Chart</a> |
|
|
377 |
</li> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
</ul> |
|
|
381 |
</div> |
|
|
382 |
</div> |
|
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
</div> |
|
|
387 |
</div> |
|
|
388 |
</div> |
|
|
389 |
</div> |
|
|
390 |
|
|
|
391 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
392 |
<script>prettyPrint();</script> |
|
|
393 |
|
|
|
394 |
<script> |
|
|
395 |
YUI.Env.Tests = { |
|
|
396 |
examples: [], |
|
|
397 |
project: '../assets', |
|
|
398 |
assets: '../assets/charts', |
|
|
399 |
name: 'charts-axisupdate', |
|
|
400 |
title: 'Update Chart Axis', |
|
|
401 |
newWindow: '', |
|
|
402 |
auto: false |
|
|
403 |
}; |
|
|
404 |
YUI.Env.Tests.examples.push('charts-simple'); |
|
|
405 |
YUI.Env.Tests.examples.push('charts-multiseries'); |
|
|
406 |
YUI.Env.Tests.examples.push('charts-column'); |
|
|
407 |
YUI.Env.Tests.examples.push('charts-stackedcolumn'); |
|
|
408 |
YUI.Env.Tests.examples.push('charts-timeaxis'); |
|
|
409 |
YUI.Env.Tests.examples.push('charts-gridlines'); |
|
|
410 |
YUI.Env.Tests.examples.push('charts-stackedarea'); |
|
|
411 |
YUI.Env.Tests.examples.push('charts-globalstyles'); |
|
|
412 |
YUI.Env.Tests.examples.push('charts-customizedtooltip'); |
|
|
413 |
YUI.Env.Tests.examples.push('charts-objectstyles'); |
|
|
414 |
YUI.Env.Tests.examples.push('charts-pie'); |
|
|
415 |
YUI.Env.Tests.examples.push('charts-dualaxes'); |
|
|
416 |
YUI.Env.Tests.examples.push('charts-axisupdate'); |
|
|
417 |
YUI.Env.Tests.examples.push('charts-seriesupdate'); |
|
|
418 |
YUI.Env.Tests.examples.push('charts-legend'); |
|
|
419 |
YUI.Env.Tests.examples.push('charts-groupmarkers'); |
|
|
420 |
|
|
|
421 |
</script> |
|
|
422 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
423 |
|
|
|
424 |
|
|
|
425 |
|
|
|
426 |
</body> |
|
|
427 |
</html> |