From a905046ceb86fbdc75c919c5a17132febe9d43f5 Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Thu, 24 Dec 2015 12:36:29 -0500 Subject: [PATCH 1/2] added total row to tooltip --- src/config.js | 3 +++ src/tooltip.js | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index ae9aa9f..44cbe4a 100644 --- a/src/config.js +++ b/src/config.js @@ -208,6 +208,9 @@ c3_chart_internal_fn.getDefaultConfig = function () { tooltip_init_position: {top: '0px', left: '50px'}, tooltip_onshow: function () {}, tooltip_onhide: function () {}, + tooltip_total_show: false, + tooltip_total_name: undefined, + tooltip_total_format_value: undefined, // title title_text: undefined, title_padding: { diff --git a/src/tooltip.js b/src/tooltip.js index ce76601..720c33b 100644 --- a/src/tooltip.js +++ b/src/tooltip.js @@ -29,7 +29,9 @@ c3_chart_internal_fn.getTooltipContent = function (d, defaultTitleFormat, defaul titleFormat = config.tooltip_format_title || defaultTitleFormat, nameFormat = config.tooltip_format_name || function (name) { return name; }, valueFormat = config.tooltip_format_value || defaultValueFormat, - text, i, title, value, name, bgcolor, + totalName = config.tooltip_total_name || "Total", + totalValueFormat = config.tooltip_total_format_value || valueFormat, + text, i, title, value, name, bgcolor, totalValue, orderAsc = $$.isOrderAsc(); if (config.data_groups.length === 0) { @@ -70,6 +72,17 @@ c3_chart_internal_fn.getTooltipContent = function (d, defaultTitleFormat, defaul text += ""; } } + // adding a "Total" row to the tooltip - if a formatter is not given then the total + // value will be formatted the same way as the other values + if (config.tooltip_total_show) { + totalValue = 0; + for (i=0; i < d.length; i++) { + totalValue += d[i].value; + } + totalValue = totalValueFormat(totalValue); + text += "" + totalName + ""; + text += "" + totalValue + ""; + } return text + ""; }; c3_chart_internal_fn.tooltipPosition = function (dataToShow, tWidth, tHeight, element) { From 14603daa221b975889381006edf31be3a1d6a87a Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Fri, 8 Jan 2016 12:39:06 -0500 Subject: [PATCH 2/2] src/tooltip total row cleanup --- src/tooltip.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tooltip.js b/src/tooltip.js index 720c33b..696394f 100644 --- a/src/tooltip.js +++ b/src/tooltip.js @@ -76,12 +76,15 @@ c3_chart_internal_fn.getTooltipContent = function (d, defaultTitleFormat, defaul // value will be formatted the same way as the other values if (config.tooltip_total_show) { totalValue = 0; - for (i=0; i < d.length; i++) { + for (i = 0; i < d.length; i++) { + if (! (d[i] && (d[i].value || d[i].value === 0))) { continue; } totalValue += d[i].value; } totalValue = totalValueFormat(totalValue); - text += "" + totalName + ""; - text += "" + totalValue + ""; + text += ""; + text += "" + totalName + ""; + text += "" + totalValue + ""; + text += ""; } return text + ""; };