Cross Reference: /yui3/src/charts/docs/charts-customizedtooltip.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly<style scoped>
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly#mychart {
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly margin:10px 10px 10px 10px;
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly width:90%;
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly max-width: 800px;
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly height:400px;
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly}
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly</style>
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly<div class="intro">
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly<p>This example shows how to customize the default tooltip of a `Chart`.</p>
bf6ad7630f65756fbcb4c8fb9936a4fe542a6308Jenny Donnelly</div>
<div class="example">
{{>charts-customizedtooltip-source}}
</div>
<h3>This example shows how to customize the tooltip for a `Chart`.</h3>
<p>A `Chart` instance comes with a simple default tooltip. This tooltip is represented by the `tooltip` attribute. Through the tooltip attribute you can do the following:
<ul>
<li>Style the tooltip background, border and text.</li>
<li>Customize and format the tooltip message.</li>
<li>Change the show and hide events.</li>
<li>Disable the tooltip.</li>
</ul>
</p>
<p>The `tooltip` attribute contains the following properties:
<dl>
<dt>node</dt><dd>Reference to the actual dom node</dd>
<dt>showEvent</dt><dd>Event that should trigger the tooltip</dd>
<dt>hideEvent</dt><dd>Event that should trigger the removal of a tooltip (can be an event or an array of events)</dd>
<dt>styles</dt><dd>A hash of style properties that will be applied to the tooltip node</dd>
<dt>show</dt><dd>Indicates whether or not to show the tooltip</dd>
<dt>markerEventHandler</dt><dd>Displays and hides tooltip based on marker events</dd>
<dt>planarEventHandler</dt><dd>Displays and hides tooltip based on planar events</dd>
<dt>markerLabelFunction</dt><dd>Reference to the function used to format a marker event triggered tooltip's text. The method contains
the following arguments:
<dl>
<dt>categoryItem</dt><dd>An object containing the following:
<dl>
<dt>axis</dt><dd>The axis to which the category is bound.</dd>
<dt>displayName</dt><dd>The display name set to the category (defaults to key if not provided).</dd>
<dt>key</dt><dd>The key of the category.</dd>
<dt>value</dt><dd>The value of the category.</dd>
</dl>
</dd>
<dt>valueItem</dt><dd>An object containing the following:
<dl>
<dt>axis</dt><dd>The axis to which the item's series is bound.</dd>
<dt>displayName</dt><dd>The display name of the series. (defaults to key if not provided)</dd>
<dt>key</dt><dd>The key for the series.</dd>
<dt>value</dt><dd>The value for the series item.</dd>
</dl>
</dd>
<dt>itemIndex</dt><dd>The index of the item within the series.</dd>
<dt>series</dt><dd> The `CartesianSeries` instance of the item.</dd>
<dt>seriesIndex</dt><dd>The index of the series in the `seriesCollection`.</dd>
</dl>
The method returns an `HTMLElement` which is written into the DOM using `appendChild`. If you override this method and choose to return an html string, you
will also need to override the tooltip's `setTextFunction` method to accept an html string.
</dd>
<dt>planarLabelFunction</dt><dd>Reference to the function used to format a planar event triggered tooltip's text
<dl>
<dt>categoryAxis</dt><dd> `CategoryAxis` Reference to the categoryAxis of the chart.
<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:
<dl>
<dt>axis</dt><dd>The value axis of the series.</dd>
<dt>key</dt><dd>The key for the series.</dd>
<dt>value</dt><dd>The value for the series item.</dd>
<dt>displayName</dt><dd>The display name of the series. (defaults to key if not provided)</dd>
</dl>
</dd>
<dt>index</dt><dd>The index of the item within its series.</dd>
<dt>seriesArray</dt><dd>Array of series instances for each value item.</dd>
<dt>seriesIndex</dt><dd>The index of the series in the `seriesCollection`.</dd>
</dl>
The method returns an `HTMLElement` which is written into the DOM using `appendChild`. If you override this method and choose to return an html string, you
will also need to override the tooltip's `setTextFunction` method to accept an html string.
</dd>
<dt>setTextFunction</dt><dd>Method that writes content returned from `planarLabelFunction` or `markerLabelFunction` into the the tooltip node.
has the following signature:
<dl>
<dt>label</dt><dd>The `HTMLElement` that the content is to be added.</dd>
<dt>val</dt><dd>The content to be rendered into tooltip. This can be a `String` or `HTMLElement`. If an HTML string is used, it will be rendered as a
string.</dd>
</dl>
</dd>
</dl>
<p>In this example, we have changed the styles and set a custom `markerLabelFunction` to format the text.</p>
```
var myDataValues = [
{category:"5/1/2010", Miscellaneous:2000, Expenses:3700, Revenue:2200},
{category:"5/2/2010", Miscellaneous:50, Expenses:9100, Revenue:100},
{category:"5/3/2010", Miscellaneous:400, Expenses:1100, Revenue:1500},
{category:"5/4/2010", Miscellaneous:200, Expenses:1900, Revenue:2800},
{category:"5/5/2010", Miscellaneous:5000, Expenses:5000, Revenue:2650}
];
var myTooltip = {
styles: {
backgroundColor: "#333",
color: "#eee",
borderColor: "#fff",
textAlign: "center"
},
markerLabelFunction: function(categoryItem, valueItem, itemIndex, series, seriesIndex)
{
var msg = document.createElement("div"),
underlinedTextBlock = document.createElement("span"),
boldTextBlock = document.createElement("div");
underlinedTextBlock.style.textDecoration = "underline";
boldTextBlock.style.marginTop = "5px";
boldTextBlock.style.fontWeight = "bold";
underlinedTextBlock.appendChild(document.createTextNode(valueItem.displayName + " for " +
categoryItem.axis.get("labelFunction").apply(this, [categoryItem.value, categoryItem.axis.get("labelFormat")])));
boldTextBlock.appendChild(document.createTextNode(valueItem.axis.get("labelFunction").apply(this, [valueItem.value, {prefix:"$", decimalPlaces:2}])));
msg.appendChild(underlinedTextBlock);
msg.appendChild(document.createElement("br"));
msg.appendChild(boldTextBlock);
return msg;
}
};
var mychart = new Y.Chart({
dataProvider:myDataValues,
type:"bar",
render:"#mychart",
tooltip: myTooltip
});
```