<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Customize a Chart's Tooltip</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<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>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Customize a Chart's Tooltip</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style scoped>
#mychart {
margin:10px 10px 10px 10px;
width:90%;
max-width: 800px;
height:400px;
}
</style>
<div class="intro">
<p>This example shows how to customize the default tooltip of a <code>Chart</code>.</p>
</div>
<div class="example">
<div id="mychart"></div>
<script type="text/javascript">
(function() {
YUI().use('charts', function (Y)
{
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: "#eee",
textAlign: "center",
padding: "2px 8px 2px 8px"
},
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
});
});
})();
</script>
</div>
<h3>This example shows how to customize the tooltip for a <code>Chart</code>.</h3>
<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:
<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 <code>tooltip</code> 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 <code>CartesianSeries</code> instance of the item.</dd>
<dt>seriesIndex</dt><dd>The index of the series in the <code>seriesCollection</code>.</dd>
</dl>
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
will also need to override the tooltip's <code>setTextFunction</code> 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> <code>CategoryAxis</code> 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 <code>seriesCollection</code>.</dd>
</dl>
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
will also need to override the tooltip's <code>setTextFunction</code> method to accept an html string.
</dd>
<dt>setTextFunction</dt><dd>Method that writes content returned from <code>planarLabelFunction</code> or <code>markerLabelFunction</code> into the the tooltip node.
has the following signature:
<dl>
<dt>label</dt><dd>The <code>HTMLElement</code> that the content is to be added.</dd>
<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
string.</dd>
</dl>
</dd>
</dl>
<p>In this example, we have changed the styles and set a custom <code>markerLabelFunction</code> to format the text.</p>
<h4>CSS</h4>
<pre class="code prettyprint">#mychart {
margin:10px 10px 10px 10px;
width:90%;
max-width: 800px;
height:400px;
}</pre>
<h4>HTML</h4>
<pre class="code prettyprint"><div id="mychart"></div></pre>
<h4>JavaScript</h4>
<pre class="code prettyprint">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
});</pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Shows how to use Charts to create a basic chart.">
<a href="charts-simple.html">Basic Charts Implementation</a>
</li>
<li data-description="Shows how to create a chart with multiple series.">
<a href="charts-multiseries.html">Chart with Multiple Series</a>
</li>
<li data-description="Shows how to create a column chart with multiple series.">
<a href="charts-column.html">Specify Chart Type</a>
</li>
<li data-description="Shows how to create a column chart with a stacked numeric axis.">
<a href="charts-stackedcolumn.html">Create Stacked Chart</a>
</li>
<li data-description="Shows how to create a chart with a time axis.">
<a href="charts-timeaxis.html">Create a Chart with a Time Axis</a>
</li>
<li data-description="Shows how to add gridlines to a chart.">
<a href="charts-gridlines.html">Add Gridlines to a Chart</a>
</li>
<li data-description="Shows how to create a chart with planar based events.">
<a href="charts-stackedarea.html">Create a Stacked Area Chart with Planar Based Events</a>
</li>
<li data-description="Shows how to use a chart's styles attribute to customize a chart.">
<a href="charts-globalstyles.html">Customize a Chart</a>
</li>
<li data-description="Shows how to customize the default tooltip of a chart.">
<a href="charts-customizedtooltip.html">Customize a Chart's Tooltip</a>
</li>
<li data-description="Shows how to explicitly define the axes and series for a chart.">
<a href="charts-objectstyles.html">Define a Chart's Axes and Series</a>
</li>
<li data-description="Shows how to use charts to create a pie chart.">
<a href="charts-pie.html">Pie Chart</a>
</li>
<li data-description="Shows how to create a chart with multiple value axes.">
<a href="charts-dualaxes.html">Dual Axes Chart</a>
</li>
<li data-description="Shows how to access a chart instance's value axis after the chart has rendered.">
<a href="charts-axisupdate.html">Update Chart Axis</a>
</li>
<li data-description="Shows how to access a chart instance's seriesCollection after the chart has rendered.">
<a href="charts-seriesupdate.html">Update Chart Series</a>
</li>
<li data-description="Shows how to add a legend to a chart.">
<a href="charts-legend.html">Create Chart with a Legend</a>
</li>
<li data-description="Shows how to render multiple data points in a singe marker.">
<a href="charts-groupmarkers.html">Group Marker Chart</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/charts',
name: 'charts-customizedtooltip',
title: 'Customize a Chart's Tooltip',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('charts-simple');
YUI.Env.Tests.examples.push('charts-multiseries');
YUI.Env.Tests.examples.push('charts-column');
YUI.Env.Tests.examples.push('charts-stackedcolumn');
YUI.Env.Tests.examples.push('charts-timeaxis');
YUI.Env.Tests.examples.push('charts-gridlines');
YUI.Env.Tests.examples.push('charts-stackedarea');
YUI.Env.Tests.examples.push('charts-globalstyles');
YUI.Env.Tests.examples.push('charts-customizedtooltip');
YUI.Env.Tests.examples.push('charts-objectstyles');
YUI.Env.Tests.examples.push('charts-pie');
YUI.Env.Tests.examples.push('charts-dualaxes');
YUI.Env.Tests.examples.push('charts-axisupdate');
YUI.Env.Tests.examples.push('charts-seriesupdate');
YUI.Env.Tests.examples.push('charts-legend');
YUI.Env.Tests.examples.push('charts-groupmarkers');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>