YUI 3.x Home -

YUI Library Examples: Charts: Customize a Chart's Tooltip

Charts: Customize a Chart's Tooltip

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.

  1. var myDataValues = [
  2. {category:"5/1/2010", Miscellaneous:2000, Expenses:3700, Revenue:2200},
  3. {category:"5/2/2010", Miscellaneous:50, Expenses:9100, Revenue:100},
  4. {category:"5/3/2010", Miscellaneous:400, Expenses:1100, Revenue:1500},
  5. {category:"5/4/2010", Miscellaneous:200, Expenses:1900, Revenue:2800},
  6. {category:"5/5/2010", Miscellaneous:5000, Expenses:5000, Revenue:2650}
  7. ];
  8.  
  9. var myTooltip = {
  10. styles: {
  11. backgroundColor: "#333",
  12. color: "#eee",
  13. borderColor: "#fff",
  14. textAlign: "center"
  15. },
  16. markerLabelFunction: function(categoryItem, valueItem, itemIndex, series, seriesIndex)
  17. {
  18. var msg = "<span style=\"text-decoration:underline\">" + valueItem.displayName + " for " +
  19. categoryItem.axis.get("labelFunction").apply(this, [categoryItem.value, categoryItem.axis.get("labelFormat")]) +
  20. "</span><br/><div style=\"margin-top:5px;font-weight:bold\">" + valueItem.axis.get("labelFunction").apply(this, [valueItem.value, {prefix:"$", decimalPlaces:2}]) + "</div>";
  21. return msg;
  22. }
  23. };
  24.  
  25. var mychart = new Y.Chart({
  26. dataProvider:myDataValues,
  27. type:"bar",
  28. render:"#mychart",
  29. tooltip: myTooltip
  30. });
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
                });

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings