This example shows how to customize the default tooltip of a Chart
.
This example shows how to customize the tooltip for a Chart
.
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:
- Style the tooltip background, border and text.
- Customize and format the tooltip message.
- Change the show and hide events.
- Disable the tooltip.
The tooltip
attribute contains the following properties:
- showEvent: event that should trigger the tooltip
- hideEvent: event that should trigger the removal of a tooltip (can be an event or an array of events)
- styles: hash of style properties that will be applied to the tooltip node
- show: indicates whether or not to show the tooltip
- markerEventHandler: displays and hides tooltip based on marker events
- planarEventHandler: displays and hides tooltip based on planar events
- markerLabelFunction: reference to the function used to format a marker event triggered tooltip's text
- planarLabelFunction: reference to the function used to format a planar event triggered tooltip's text
In this example, we have changed the styles and set a custom markerLabelFunction
to format the text.
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 = "<span style=\"text-decoration:underline\">" + valueItem.displayName + " for " + categoryItem.axis.get("labelFunction").apply(this, [categoryItem.value, categoryItem.axis.get("labelFormat")]) + "</span><br/><div style=\"margin-top:5px;font-weight:bold\">" + valueItem.axis.get("labelFunction").apply(this, [valueItem.value, {prefix:"$", decimalPlaces:2}]) + "</div>"; return msg; } }; var mychart = new Y.Chart({ dataProvider:myDataValues, type:"bar", render:"#mychart", tooltip: myTooltip });