|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Customize a Chart's Tooltip</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: Customize a Chart's Tooltip</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 customize the default tooltip of 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', function (Y) |
|
|
44 |
{ |
|
|
45 |
var myDataValues = [ |
|
|
46 |
{category:"5/1/2010", Miscellaneous:2000, Expenses:3700, Revenue:2200}, |
|
|
47 |
{category:"5/2/2010", Miscellaneous:50, Expenses:9100, Revenue:100}, |
|
|
48 |
{category:"5/3/2010", Miscellaneous:400, Expenses:1100, Revenue:1500}, |
|
|
49 |
{category:"5/4/2010", Miscellaneous:200, Expenses:1900, Revenue:2800}, |
|
|
50 |
{category:"5/5/2010", Miscellaneous:5000, Expenses:5000, Revenue:2650} |
|
|
51 |
]; |
|
|
52 |
|
|
|
53 |
var myTooltip = { |
|
|
54 |
styles: { |
|
|
55 |
backgroundColor: "#333", |
|
|
56 |
color: "#eee", |
|
|
57 |
borderColor: "#eee", |
|
|
58 |
textAlign: "center", |
|
|
59 |
padding: "2px 8px 2px 8px" |
|
|
60 |
}, |
|
|
61 |
markerLabelFunction: function(categoryItem, valueItem, itemIndex, series, seriesIndex) |
|
|
62 |
{ |
|
|
63 |
var msg = document.createElement("div"), |
|
|
64 |
underlinedTextBlock = document.createElement("span"), |
|
|
65 |
boldTextBlock = document.createElement("div"); |
|
|
66 |
underlinedTextBlock.style.textDecoration = "underline"; |
|
|
67 |
boldTextBlock.style.marginTop = "5px"; |
|
|
68 |
boldTextBlock.style.fontWeight = "bold"; |
|
|
69 |
underlinedTextBlock.appendChild(document.createTextNode(valueItem.displayName + " for " + |
|
|
70 |
categoryItem.axis.get("labelFunction").apply(this, [categoryItem.value, categoryItem.axis.get("labelFormat")]))); |
|
|
71 |
boldTextBlock.appendChild(document.createTextNode(valueItem.axis.get("labelFunction").apply(this, [valueItem.value, {prefix:"$", decimalPlaces:2}]))); |
|
|
72 |
msg.appendChild(underlinedTextBlock); |
|
|
73 |
msg.appendChild(document.createElement("br")); |
|
|
74 |
msg.appendChild(boldTextBlock); |
|
|
75 |
return msg; |
|
|
76 |
} |
|
|
77 |
}; |
|
|
78 |
|
|
|
79 |
var mychart = new Y.Chart({ |
|
|
80 |
dataProvider:myDataValues, |
|
|
81 |
type:"bar", |
|
|
82 |
render:"#mychart", |
|
|
83 |
tooltip: myTooltip |
|
|
84 |
}); |
|
|
85 |
}); |
|
|
86 |
})(); |
|
|
87 |
</script> |
|
|
88 |
|
|
|
89 |
</div> |
|
|
90 |
|
|
|
91 |
<h3>This example shows how to customize the tooltip for a <code>Chart</code>.</h3> |
|
|
92 |
|
|
|
93 |
<p>A <code>Chart</code> instance comes with a simple default tooltip. This tooltip is represented by the <code>tooltip</code> attribute. Through the tooltip attribute you can do the following: |
|
|
94 |
<ul> |
|
|
95 |
<li>Style the tooltip background, border and text.</li> |
|
|
96 |
<li>Customize and format the tooltip message.</li> |
|
|
97 |
<li>Change the show and hide events.</li> |
|
|
98 |
<li>Disable the tooltip.</li> |
|
|
99 |
</ul> |
|
|
100 |
</p> |
|
|
101 |
<p>The <code>tooltip</code> attribute contains the following properties: |
|
|
102 |
|
|
|
103 |
<dl> |
|
|
104 |
<dt>node</dt><dd>Reference to the actual dom node</dd> |
|
|
105 |
<dt>showEvent</dt><dd>Event that should trigger the tooltip</dd> |
|
|
106 |
<dt>hideEvent</dt><dd>Event that should trigger the removal of a tooltip (can be an event or an array of events)</dd> |
|
|
107 |
<dt>styles</dt><dd>A hash of style properties that will be applied to the tooltip node</dd> |
|
|
108 |
<dt>show</dt><dd>Indicates whether or not to show the tooltip</dd> |
|
|
109 |
<dt>markerEventHandler</dt><dd>Displays and hides tooltip based on marker events</dd> |
|
|
110 |
<dt>planarEventHandler</dt><dd>Displays and hides tooltip based on planar events</dd> |
|
|
111 |
<dt>markerLabelFunction</dt><dd>Reference to the function used to format a marker event triggered tooltip's text. The method contains |
|
|
112 |
the following arguments: |
|
|
113 |
<dl> |
|
|
114 |
<dt>categoryItem</dt><dd>An object containing the following: |
|
|
115 |
<dl> |
|
|
116 |
<dt>axis</dt><dd>The axis to which the category is bound.</dd> |
|
|
117 |
<dt>displayName</dt><dd>The display name set to the category (defaults to key if not provided).</dd> |
|
|
118 |
<dt>key</dt><dd>The key of the category.</dd> |
|
|
119 |
<dt>value</dt><dd>The value of the category.</dd> |
|
|
120 |
</dl> |
|
|
121 |
</dd> |
|
|
122 |
<dt>valueItem</dt><dd>An object containing the following: |
|
|
123 |
<dl> |
|
|
124 |
<dt>axis</dt><dd>The axis to which the item's series is bound.</dd> |
|
|
125 |
<dt>displayName</dt><dd>The display name of the series. (defaults to key if not provided)</dd> |
|
|
126 |
<dt>key</dt><dd>The key for the series.</dd> |
|
|
127 |
<dt>value</dt><dd>The value for the series item.</dd> |
|
|
128 |
</dl> |
|
|
129 |
</dd> |
|
|
130 |
<dt>itemIndex</dt><dd>The index of the item within the series.</dd> |
|
|
131 |
<dt>series</dt><dd> The <code>CartesianSeries</code> instance of the item.</dd> |
|
|
132 |
<dt>seriesIndex</dt><dd>The index of the series in the <code>seriesCollection</code>.</dd> |
|
|
133 |
</dl> |
|
|
134 |
The method returns an <code>HTMLElement</code> which is written into the DOM using <code>appendChild</code>. If you override this method and choose to return an html string, you |
|
|
135 |
will also need to override the tooltip's <code>setTextFunction</code> method to accept an html string. |
|
|
136 |
</dd> |
|
|
137 |
<dt>planarLabelFunction</dt><dd>Reference to the function used to format a planar event triggered tooltip's text |
|
|
138 |
<dl> |
|
|
139 |
<dt>categoryAxis</dt><dd> <code>CategoryAxis</code> Reference to the categoryAxis of the chart. |
|
|
140 |
<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: |
|
|
141 |
<dl> |
|
|
142 |
<dt>axis</dt><dd>The value axis of the series.</dd> |
|
|
143 |
<dt>key</dt><dd>The key for the series.</dd> |
|
|
144 |
<dt>value</dt><dd>The value for the series item.</dd> |
|
|
145 |
<dt>displayName</dt><dd>The display name of the series. (defaults to key if not provided)</dd> |
|
|
146 |
</dl> |
|
|
147 |
</dd> |
|
|
148 |
<dt>index</dt><dd>The index of the item within its series.</dd> |
|
|
149 |
<dt>seriesArray</dt><dd>Array of series instances for each value item.</dd> |
|
|
150 |
<dt>seriesIndex</dt><dd>The index of the series in the <code>seriesCollection</code>.</dd> |
|
|
151 |
</dl> |
|
|
152 |
The method returns an <code>HTMLElement</code> which is written into the DOM using <code>appendChild</code>. If you override this method and choose to return an html string, you |
|
|
153 |
will also need to override the tooltip's <code>setTextFunction</code> method to accept an html string. |
|
|
154 |
</dd> |
|
|
155 |
<dt>setTextFunction</dt><dd>Method that writes content returned from <code>planarLabelFunction</code> or <code>markerLabelFunction</code> into the the tooltip node. |
|
|
156 |
has the following signature: |
|
|
157 |
<dl> |
|
|
158 |
<dt>label</dt><dd>The <code>HTMLElement</code> that the content is to be added.</dd> |
|
|
159 |
<dt>val</dt><dd>The content to be rendered into tooltip. This can be a <code>String</code> or <code>HTMLElement</code>. If an HTML string is used, it will be rendered as a |
|
|
160 |
string.</dd> |
|
|
161 |
</dl> |
|
|
162 |
</dd> |
|
|
163 |
</dl> |
|
|
164 |
|
|
|
165 |
<p>In this example, we have changed the styles and set a custom <code>markerLabelFunction</code> to format the text.</p> |
|
|
166 |
|
|
|
167 |
<h4>CSS</h4> |
|
|
168 |
<pre class="code prettyprint">#mychart { |
|
|
169 |
margin:10px 10px 10px 10px; |
|
|
170 |
width:90%; |
|
|
171 |
max-width: 800px; |
|
|
172 |
height:400px; |
|
|
173 |
}</pre> |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
<h4>HTML</h4> |
|
|
177 |
<pre class="code prettyprint"><div id="mychart"></div></pre> |
|
|
178 |
|
|
|
179 |
|
|
|
180 |
<h4>JavaScript</h4> |
|
|
181 |
<pre class="code prettyprint">var myDataValues = [ |
|
|
182 |
{category:"5/1/2010", Miscellaneous:2000, Expenses:3700, Revenue:2200}, |
|
|
183 |
{category:"5/2/2010", Miscellaneous:50, Expenses:9100, Revenue:100}, |
|
|
184 |
{category:"5/3/2010", Miscellaneous:400, Expenses:1100, Revenue:1500}, |
|
|
185 |
{category:"5/4/2010", Miscellaneous:200, Expenses:1900, Revenue:2800}, |
|
|
186 |
{category:"5/5/2010", Miscellaneous:5000, Expenses:5000, Revenue:2650} |
|
|
187 |
]; |
|
|
188 |
|
|
|
189 |
var myTooltip = { |
|
|
190 |
styles: { |
|
|
191 |
backgroundColor: "#333", |
|
|
192 |
color: "#eee", |
|
|
193 |
borderColor: "#fff", |
|
|
194 |
textAlign: "center" |
|
|
195 |
}, |
|
|
196 |
markerLabelFunction: function(categoryItem, valueItem, itemIndex, series, seriesIndex) |
|
|
197 |
{ |
|
|
198 |
var msg = document.createElement("div"), |
|
|
199 |
underlinedTextBlock = document.createElement("span"), |
|
|
200 |
boldTextBlock = document.createElement("div"); |
|
|
201 |
underlinedTextBlock.style.textDecoration = "underline"; |
|
|
202 |
boldTextBlock.style.marginTop = "5px"; |
|
|
203 |
boldTextBlock.style.fontWeight = "bold"; |
|
|
204 |
underlinedTextBlock.appendChild(document.createTextNode(valueItem.displayName + " for " + |
|
|
205 |
categoryItem.axis.get("labelFunction").apply(this, [categoryItem.value, categoryItem.axis.get("labelFormat")]))); |
|
|
206 |
boldTextBlock.appendChild(document.createTextNode(valueItem.axis.get("labelFunction").apply(this, [valueItem.value, {prefix:"$", decimalPlaces:2}]))); |
|
|
207 |
msg.appendChild(underlinedTextBlock); |
|
|
208 |
msg.appendChild(document.createElement("br")); |
|
|
209 |
msg.appendChild(boldTextBlock); |
|
|
210 |
return msg; |
|
|
211 |
} |
|
|
212 |
}; |
|
|
213 |
|
|
|
214 |
var mychart = new Y.Chart({ |
|
|
215 |
dataProvider:myDataValues, |
|
|
216 |
type:"bar", |
|
|
217 |
render:"#mychart", |
|
|
218 |
tooltip: myTooltip |
|
|
219 |
});</pre> |
|
|
220 |
|
|
|
221 |
|
|
|
222 |
</div> |
|
|
223 |
</div> |
|
|
224 |
</div> |
|
|
225 |
|
|
|
226 |
<div class="yui3-u-1-4"> |
|
|
227 |
<div class="sidebar"> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
<div class="sidebox"> |
|
|
232 |
<div class="hd"> |
|
|
233 |
<h2 class="no-toc">Examples</h2> |
|
|
234 |
</div> |
|
|
235 |
|
|
|
236 |
<div class="bd"> |
|
|
237 |
<ul class="examples"> |
|
|
238 |
|
|
|
239 |
|
|
|
240 |
<li data-description="Shows how to use Charts to create a basic chart."> |
|
|
241 |
<a href="charts-simple.html">Basic Charts Implementation</a> |
|
|
242 |
</li> |
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
<li data-description="Shows how to create a chart with multiple series."> |
|
|
247 |
<a href="charts-multiseries.html">Chart with Multiple Series</a> |
|
|
248 |
</li> |
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
<li data-description="Shows how to create a column chart with multiple series."> |
|
|
253 |
<a href="charts-column.html">Specify Chart Type</a> |
|
|
254 |
</li> |
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
<li data-description="Shows how to create a column chart with a stacked numeric axis."> |
|
|
259 |
<a href="charts-stackedcolumn.html">Create Stacked Chart</a> |
|
|
260 |
</li> |
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
<li data-description="Shows how to create a chart with a time axis."> |
|
|
265 |
<a href="charts-timeaxis.html">Create a Chart with a Time Axis</a> |
|
|
266 |
</li> |
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
<li data-description="Shows how to add gridlines to a chart."> |
|
|
271 |
<a href="charts-gridlines.html">Add Gridlines to a Chart</a> |
|
|
272 |
</li> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<li data-description="Shows how to create a chart with planar based events."> |
|
|
277 |
<a href="charts-stackedarea.html">Create a Stacked Area Chart with Planar Based Events</a> |
|
|
278 |
</li> |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
<li data-description="Shows how to use a chart's styles attribute to customize a chart."> |
|
|
283 |
<a href="charts-globalstyles.html">Customize a Chart</a> |
|
|
284 |
</li> |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
<li data-description="Shows how to customize the default tooltip of a chart."> |
|
|
289 |
<a href="charts-customizedtooltip.html">Customize a Chart's Tooltip</a> |
|
|
290 |
</li> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<li data-description="Shows how to explicitly define the axes and series for a chart."> |
|
|
295 |
<a href="charts-objectstyles.html">Define a Chart's Axes and Series</a> |
|
|
296 |
</li> |
|
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
<li data-description="Shows how to use charts to create a pie chart."> |
|
|
301 |
<a href="charts-pie.html">Pie Chart</a> |
|
|
302 |
</li> |
|
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|
306 |
<li data-description="Shows how to create a chart with multiple value axes."> |
|
|
307 |
<a href="charts-dualaxes.html">Dual Axes Chart</a> |
|
|
308 |
</li> |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
<li data-description="Shows how to access a chart instance's value axis after the chart has rendered."> |
|
|
313 |
<a href="charts-axisupdate.html">Update Chart Axis</a> |
|
|
314 |
</li> |
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
<li data-description="Shows how to access a chart instance's seriesCollection after the chart has rendered."> |
|
|
319 |
<a href="charts-seriesupdate.html">Update Chart Series</a> |
|
|
320 |
</li> |
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
<li data-description="Shows how to add a legend to a chart."> |
|
|
325 |
<a href="charts-legend.html">Create Chart with a Legend</a> |
|
|
326 |
</li> |
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
<li data-description="Shows how to render multiple data points in a singe marker."> |
|
|
331 |
<a href="charts-groupmarkers.html">Group Marker Chart</a> |
|
|
332 |
</li> |
|
|
333 |
|
|
|
334 |
|
|
|
335 |
</ul> |
|
|
336 |
</div> |
|
|
337 |
</div> |
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
</div> |
|
|
342 |
</div> |
|
|
343 |
</div> |
|
|
344 |
</div> |
|
|
345 |
|
|
|
346 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
347 |
<script>prettyPrint();</script> |
|
|
348 |
|
|
|
349 |
<script> |
|
|
350 |
YUI.Env.Tests = { |
|
|
351 |
examples: [], |
|
|
352 |
project: '../assets', |
|
|
353 |
assets: '../assets/charts', |
|
|
354 |
name: 'charts-customizedtooltip', |
|
|
355 |
title: 'Customize a Chart's Tooltip', |
|
|
356 |
newWindow: '', |
|
|
357 |
auto: false |
|
|
358 |
}; |
|
|
359 |
YUI.Env.Tests.examples.push('charts-simple'); |
|
|
360 |
YUI.Env.Tests.examples.push('charts-multiseries'); |
|
|
361 |
YUI.Env.Tests.examples.push('charts-column'); |
|
|
362 |
YUI.Env.Tests.examples.push('charts-stackedcolumn'); |
|
|
363 |
YUI.Env.Tests.examples.push('charts-timeaxis'); |
|
|
364 |
YUI.Env.Tests.examples.push('charts-gridlines'); |
|
|
365 |
YUI.Env.Tests.examples.push('charts-stackedarea'); |
|
|
366 |
YUI.Env.Tests.examples.push('charts-globalstyles'); |
|
|
367 |
YUI.Env.Tests.examples.push('charts-customizedtooltip'); |
|
|
368 |
YUI.Env.Tests.examples.push('charts-objectstyles'); |
|
|
369 |
YUI.Env.Tests.examples.push('charts-pie'); |
|
|
370 |
YUI.Env.Tests.examples.push('charts-dualaxes'); |
|
|
371 |
YUI.Env.Tests.examples.push('charts-axisupdate'); |
|
|
372 |
YUI.Env.Tests.examples.push('charts-seriesupdate'); |
|
|
373 |
YUI.Env.Tests.examples.push('charts-legend'); |
|
|
374 |
YUI.Env.Tests.examples.push('charts-groupmarkers'); |
|
|
375 |
|
|
|
376 |
</script> |
|
|
377 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
</body> |
|
|
382 |
</html> |