diff --git a/Gruntfile.coffee b/Gruntfile.coffee index bb51330..a783980 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -92,7 +92,7 @@ module.exports = (grunt) -> specs: 'spec/*-spec.js' helpers: 'spec/*-helper.js' styles: 'c3.css' - vendor: 'https://raw.githubusercontent.com/mbostock/d3/v3.5.0/d3.min.js' + vendor: 'https://rawgit.com/mbostock/d3/v3.5.0/d3.min.js' uglify: c3: diff --git a/spec/tooltip-spec.js b/spec/tooltip-spec.js index de3983f..6038cbe 100644 --- a/spec/tooltip-spec.js +++ b/spec/tooltip-spec.js @@ -98,4 +98,25 @@ describe('c3 chart tooltip', function () { expect(left).toBe(leftExpected); }); }); + + describe('tooltip getTooltipContent', function () { + beforeAll(function () { + tooltipConfiguration = { + data_order: 'desc' + }; + }); + + it('should sort values desc', function () { + var eventRect = d3.select('.c3-event-rect-2').node(); + window.setMouseEvent(chart, 'mousemove', 100, 100, eventRect); + + var tooltipTable = d3.select('.c3-tooltip')[0]; + var expected = ["", "c3-tooltip-name--data3", + "c3-tooltip-name--data1", "c3-tooltip-name--data2"]; + var i; + for (i = 0; i < tooltipTable[0].rows.length; i++) { + expect(tooltipTable[0].rows[i].className).toBe(expected[i]); + } + }); + }); }); diff --git a/src/tooltip.js b/src/tooltip.js index 9cf9b2f..17f151a 100644 --- a/src/tooltip.js +++ b/src/tooltip.js @@ -30,6 +30,17 @@ c3_chart_internal_fn.getTooltipContent = function (d, defaultTitleFormat, defaul nameFormat = config.tooltip_format_name || function (name) { return name; }, valueFormat = config.tooltip_format_value || defaultValueFormat, text, i, title, value, name, bgcolor; + + if (config.data_order === 'desc') { + d.sort(function(a,b){ + return b.value-a.value; + }); + } else if (config.data_order === 'asc') { + d.sort(function(a,b){ + return a.value-b.value; + }); + } + for (i = 0; i < d.length; i++) { if (! (d[i] && (d[i].value || d[i].value === 0))) { continue; }