From f4a48eb8248b9d9f80c69a7c2a662c1479223766 Mon Sep 17 00:00:00 2001 From: Masayuki Tanaka Date: Sat, 15 Mar 2014 14:36:35 +0900 Subject: [PATCH] Fix data.selection.multiple - #66 --- c3.js | 33 ++++++++++++++++-------------- htdocs/samples/selection_line.html | 6 ++++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/c3.js b/c3.js index 6182ab0..7b9e773 100644 --- a/c3.js +++ b/c3.js @@ -66,7 +66,7 @@ __data_selection_enabled = getConfig(['data', 'selection', 'enabled'], false), __data_selection_grouped = getConfig(['data', 'selection', 'grouped'], false), __data_selection_isselectable = getConfig(['data', 'selection', 'isselectable'], function () { return true; }), - __data_selection_multiselected = getConfig(['data', 'selection', 'multiselected'], true); + __data_selection_multiple = getConfig(['data', 'selection', 'multiple'], true); // subchart @@ -1461,7 +1461,7 @@ .remove(); } function togglePoint(selected, target, d, i) { - (selected) ? selectPoint(target, d, i) : unselectPoint(target, d, i); + selected ? selectPoint(target, d, i) : unselectPoint(target, d, i); } function selectBar() { @@ -1469,7 +1469,7 @@ function unselectBar() { } function toggleBar(selected, target, d, i) { - (selected) ? selectBar(target, d, i) : unselectBar(target, d, i); + selected ? selectBar(target, d, i) : unselectBar(target, d, i); } function filterRemoveNull(data) { @@ -2179,13 +2179,12 @@ } if (__data_selection_grouped || isWithin) { if (__data_selection_enabled && __data_selection_isselectable(d)) { - if (!__data_selection_multiselected) { - main.selectAll(target.nodeName).each(function (d, i) { - toggle(false, this, d, i); - }); + if (__data_selection_multiple) { + _this.classed(SELECTED, !isSelected); + toggle(!isSelected, _this, d, i); + } else { + c3.select([d.id], [i], true); } - _this.classed(SELECTED, !isSelected); - toggle(!isSelected, _this, d, i); } __point_onclick(d, _this); // TODO: should be __data_onclick } @@ -3182,9 +3181,11 @@ if (! __data_selection_enabled) { return; } main.selectAll('.-shapes').selectAll('.-shape').each(function (d, i) { var selectShape = (this.nodeName === 'circle') ? selectPoint : selectBar, - unselectShape = (this.nodeName === 'circle') ? unselectPoint : unselectBar; - if (indices.indexOf(i) >= 0) { - if (__data_selection_isselectable(d) && (__data_selection_grouped || isUndefined(ids) || ids.indexOf(d.id) >= 0)) { + unselectShape = (this.nodeName === 'circle') ? unselectPoint : unselectBar, + isTargetId = __data_selection_grouped || !ids || ids.indexOf(d.id) >= 0, + isTargetIndex = !indices || indices.indexOf(i) >= 0; + if (isTargetId && isTargetIndex) { + if (__data_selection_isselectable(d)) { selectShape(d3.select(this).classed(SELECTED, true), d, i); } } else if (isDefined(resetOther) && resetOther) { @@ -3196,9 +3197,11 @@ c3.unselect = function (ids, indices) { if (! __data_selection_enabled) { return; } main.selectAll('.-shapes').selectAll('.-shape').each(function (d, i) { - var unselectShape = (this.nodeName === 'circle') ? unselectPoint : unselectBar; - if (isUndefined(indices) || indices.indexOf(i) >= 0) { - if (__data_selection_isselectable(d) && (__data_selection_grouped || isUndefined(ids) || ids.indexOf(d.id) >= 0)) { + var unselectShape = (this.nodeName === 'circle') ? unselectPoint : unselectBar, + isTargetId = __data_selection_grouped || !ids || ids.indexOf(d.id) >= 0, + isTargetIndex = !indices || indices.indexOf(i) >= 0; + if (isTargetId && isTargetIndex) { + if (__data_selection_isselectable(d)) { unselectShape(d3.select(this).classed(SELECTED, false), d, i); } } diff --git a/htdocs/samples/selection_line.html b/htdocs/samples/selection_line.html index 0229d0f..fcd36bb 100644 --- a/htdocs/samples/selection_line.html +++ b/htdocs/samples/selection_line.html @@ -12,12 +12,14 @@ bindto: '#chart', data: { columns: [ - ['sample', 30, 200, 100, 400, 150, 250] + ['sample1', 30, 200, 100, 400, 150, 250], + ['sample2', 130, 300, 200, 500, 250, 350], + ['sample3', 230, 400, 300, 600, 350, 450] ], selection: { enabled: true, grouped: true, - multiselected: false + multiple: false } } });