From 72a2047a5658b27df4886112b97842079788189b Mon Sep 17 00:00:00 2001 From: Prashant Patel Date: Mon, 27 Nov 2017 15:50:11 +0530 Subject: [PATCH 1/2] add legend format --- c3.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/c3.js b/c3.js index d607b1772..e65dcdbae 100644 --- a/c3.js +++ b/c3.js @@ -1,7 +1,7 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global.c3 = factory()); + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global.c3 = factory()); }(this, (function () { 'use strict'; var CLASS = { @@ -5097,6 +5097,7 @@ c3_chart_internal_fn.getDefaultConfig = function () { legend_padding: 0, legend_item_tile_width: 10, legend_item_tile_height: 10, + legend_format: undefined, // axis axis_rotated: false, axis_x_show: true, @@ -7431,8 +7432,12 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) { $$.api.revert(); } }); - l.append('text').text(function (id) { - return isDefined(config.data_names[id]) ? config.data_names[id] : id; + l.append('text').text(function (id, index) { + if(config.legend_format && isFunction(config.legend_format)) { + return config.legend_format(id, index); + } else { + return isDefined(config.data_names[id]) ? config.data_names[id] : id; + } }).each(function (id, i) { updatePositions(this, id, i); }).style("pointer-events", "none").attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendText : -200).attr('y', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendText); @@ -7445,8 +7450,12 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) { background = $$.legend.insert('g', '.' + CLASS.legendItem).attr("class", CLASS.legendBackground).append('rect'); } - texts = $$.legend.selectAll('text').data(targetIds).text(function (id) { - return isDefined(config.data_names[id]) ? config.data_names[id] : id; + texts = $$.legend.selectAll('text').data(targetIds).text(function (id, index) { + if(config.legend_format && isFunction(config.legend_format)) { + return config.legend_format(id, index); + } else { + return isDefined(config.data_names[id]) ? config.data_names[id] : id; + } } // MEMO: needed for update ).each(function (id, i) { updatePositions(this, id, i); From 47bdff98994dbf89a85a85e3b25cce28fdfb99b6 Mon Sep 17 00:00:00 2001 From: Prashant Patel Date: Sun, 15 Apr 2018 10:36:50 +0530 Subject: [PATCH 2/2] make the changes asked --- src/config.js | 1 + src/legend.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 54b010fd7..cd3536944 100644 --- a/src/config.js +++ b/src/config.js @@ -91,6 +91,7 @@ c3_chart_internal_fn.getDefaultConfig = function () { legend_padding: 0, legend_item_tile_width: 10, legend_item_tile_height: 10, + legend_format: undefined, // axis axis_rotated: false, axis_x_show: true, diff --git a/src/legend.js b/src/legend.js index 1f4ec73a4..40769e446 100644 --- a/src/legend.js +++ b/src/legend.js @@ -264,7 +264,13 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) { } }); l.append('text') - .text(function (id) { return isDefined(config.data_names[id]) ? config.data_names[id] : id; }) + .text(function (id, index) { + if(config.legend_format && isFunction(config.legend_format)) { + return config.legend_format(id, index); + } else { + return isDefined(config.data_names[id]) ? config.data_names[id] : id; + } + }) .each(function (id, i) { updatePositions(this, id, i); }) .style("pointer-events", "none") .attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendText : -200)