Skip to content

Commit

Permalink
fix(shape): Fix normalization on hidden data (#645)
Browse files Browse the repository at this point in the history
Added to get different ratio when data is hidden

Fix #643
Close #645
  • Loading branch information
netil committed Nov 9, 2018
1 parent 1546dda commit aba9496
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
13 changes: 13 additions & 0 deletions spec/internals/data-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
13 changes: 13 additions & 0 deletions spec/shape/shape.arc-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
6 changes: 4 additions & 2 deletions src/api/api.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
}
}
Expand Down
60 changes: 37 additions & 23 deletions src/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

0 comments on commit aba9496

Please sign in to comment.