diff --git a/spec/internals/data-spec.js b/spec/internals/data-spec.js index 88d7b25ac..98969d13a 100644 --- a/spec/internals/data-spec.js +++ b/spec/internals/data-spec.js @@ -1505,6 +1505,19 @@ describe("DATA", () => { }); }); + it("check when hiding data", done => { + // when + chart.hide("data1"); + + setTimeout(() => { + chart.$.main.selectAll(`.${CLASS.target}-data2 path`).each(function() { + expect(this.getBBox().height).to.be.equal(chartHeight); + }); + + done(); + }, 300); + }); + it("set options data.type='area'", () => { args.data.type = "area"; args.data.columns = [ diff --git a/spec/shape/shape.arc-spec.js b/spec/shape/shape.arc-spec.js index 7745d96e7..056f609db 100644 --- a/spec/shape/shape.arc-spec.js +++ b/spec/shape/shape.arc-spec.js @@ -63,6 +63,19 @@ describe("SHAPE ARC", () => { .to.match(/M1\..+,211\..+A211\..+,211\..+,0,0,1,-124\..+,-171\..+L0,0Z/); }); + it("check when hiding data", () => { + const arc = chart.$.arc; + let total = 0; + + // when + chart.hide("data1"); + + chart.data.shown().map(v => v.id) + .forEach(id => total += parseFloat(arc.select(`.${CLASS.target}-${id} text`).text())); + + expect(total).to.be.equal(100); + }); + it("should have correct d even if data id can be converted to a color", done => { const chart = util.generate({ data: { diff --git a/src/api/api.data.js b/src/api/api.data.js index 1c5360eee..6ef42076a 100644 --- a/src/api/api.data.js +++ b/src/api/api.data.js @@ -65,7 +65,7 @@ extend(data, { * chart.data.values("data1"); * // --> [10, 20, 30, 40] */ - values: function(targetId) { + values: function(targetId, flat = true) { let values = null; if (targetId) { @@ -75,7 +75,9 @@ extend(data, { values = []; targets.forEach(v => { - values = values.concat(v.values.map(d => d.value)); + const dataValue = v.values.map(d => d.value); + + flat ? (values = values.concat(dataValue)) : values.push(dataValue); }); } } diff --git a/src/data/data.js b/src/data/data.js index 39cd56caf..595e56c82 100644 --- a/src/data/data.js +++ b/src/data/data.js @@ -765,37 +765,51 @@ extend(ChartInternal.prototype, { getRatio(type, d, asPercent) { const $$ = this; const config = $$.config; - let ratio = d && (d.ratio || d.value); + const api = $$.api; + let ratio = 0; - if (type === "arc") { - // if has padAngle set, calculate rate based on value - if ($$.pie.padAngle()()) { - let total = $$.getTotalDataSum(); + if (d && api.data.shown.call(api).length) { + const dataValues = api.data.values.bind(api); - if ($$.hiddenTargetIds.length) { - total -= d3Sum($$.api.data.values.call($$.api, $$.hiddenTargetIds)); + ratio = d.ratio || d.value; + + if (type === "arc") { + // if has padAngle set, calculate rate based on value + if ($$.pie.padAngle()()) { + let total = $$.getTotalDataSum(); + + if ($$.hiddenTargetIds.length) { + total -= d3Sum(dataValues($$.hiddenTargetIds)); + } + + ratio = d.value / total; + + // otherwise, based on the rendered angle value + } else { + ratio = (d.endAngle - d.startAngle) / ( + Math.PI * ($$.hasType("gauge") && !config.gauge_fullCircle ? 1 : 2) + ); } + } else if (type === "index") { + let total = this.getTotalPerIndex(); - ratio = d.value / total; + if ($$.hiddenTargetIds.length) { + const hiddenSum = dataValues($$.hiddenTargetIds, false) + .reduce((acc, curr) => acc.map((v, i) => v + curr[i])); - // otherwise, based on the rendered angle value - } else { - ratio = (d.endAngle - d.startAngle) / ( - Math.PI * ($$.hasType("gauge") && !config.gauge_fullCircle ? 1 : 2) - ); - } - } else if (type === "index" && !d.ratio) { - const totalPerIndex = this.getTotalPerIndex(); + total = total.map((v, i) => v - hiddenSum[i]); + } - if (totalPerIndex && d.value) { - d.ratio = d.value / totalPerIndex[d.index]; - } + if (total && d.value) { + d.ratio = d.value / total[d.index]; + } - ratio = d.ratio; - } else if (type === "radar") { - ratio = (parseFloat(Math.max(d.value, 0)) / $$.maxValue) * config.radar_size_ratio; + ratio = d.ratio; + } else if (type === "radar") { + ratio = (parseFloat(Math.max(d.value, 0)) / $$.maxValue) * config.radar_size_ratio; + } } - return asPercent ? ratio * 100 : ratio; + return asPercent && ratio ? ratio * 100 : ratio; } });