We can also use the highlighter on bar charts. We decide we will add the highlighter plugin to our stacked bar chart, Monthly Revenue by Product Category, from Chapter 4, Horizontal and Stacked Bar Charts, executing the following steps:
showMarker
to false
. We also only want the x
value. So, we set tooltipAxes
to x
:... highlighter: { show: true, tooltipLocation: 'e', showMarker: false, tooltipAxes: 'x' }, ...
jqplotDataHighlight
. If we move our cursor from one stack to another within the same bar, jqplotDataUnhighlight
will not fire. So, we begin by removing the highlight
class from all the legend table rows:$('#rev_category').on('jqplotDataHighlight', function (ev, seriesIndex, pointIndex, data) { $("tr.jqplot-table-legend").removeClass("highlight");
reverse
method, and then loop through each table row in the legend. When the index of the legend matches the index of the series we highlighted, we add the highlight
class, which adds a border to the table cell:$($("tr.jqplot-table-legend").get().reverse()) .each(function(i,index) { if(i == seriesIndex){ $(this).addClass("highlight"); } }); });
jqplotDataUnhighlight
event, we remove the highlight
class from all our legend rows. Then, within our style
element, we include the highlight
class: $('#rev_category').on('jqplotDataUnhighlight',
function (ev, seriesIndex, pointIndex, data) {
$("tr.jqplot-table-legend").removeClass("highlight");
});
...
<style>
.jqplot-highlighter-tooltip { background: rgba(208,208,208,1);}
.highlight .jqplot-table-legend-label { border: 1px solid #000; padding: 0px 0px 0px 4px;}
</style>
We pull up the chart in our browser and see the results of our effort in the following screenshot. We begin highlighting different stacks in our bar chart. We are able to see the individual dollar amount for each data point. Also, the legend is highlighted so that it's easier to match a bar with the product category.
3.142.250.203