Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve normalized stack bar performance #2699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,31 +610,40 @@ ChartInternal.prototype.convertValuesToStep = function (values) {
*/
ChartInternal.prototype.getRatio = function(type, d, asPercent = false) {
const $$ = this;
const api = $$.api;
let ratio = 0;

if (d && api.data.shown.call(api).length) {
ratio = d.ratio || d.value;
if (!d) {
return 0;
}

if (type === "arc") {
if ($$.hasType('gauge')) {
ratio = (d.endAngle - d.startAngle) / (Math.PI * ($$.config.gauge_fullCircle ? 2 : 1));
let ratio;
if (type === 'arc') {
if ($$.hasType('gauge')) {
ratio = (d.endAngle - d.startAngle) / (Math.PI * ($$.config.gauge_fullCircle ? 2 : 1));
} else {
ratio = d.value / $$.getTotalDataSum();
}
} else if (type === 'index') {
if (isNumber(d.value)) {
const total = $$.getTotalPerIndex($$.axis.getId(d.id));
if (total && total[d.index] > 0) {
ratio = d.value / total[d.index];
} else {
const total = $$.getTotalDataSum();

ratio = d.value / total;
ratio = 0;
}
} else if (type === "index") {
const total = $$.getTotalPerIndex($$.axis.getId(d.id));

d.ratio = isNumber(d.value) && total && total[d.index] > 0 ?
d.value / total[d.index] : 0;

ratio = d.ratio;
} else {
ratio = 0;
}
// update d with potential new ratio
d.ratio = ratio;
} else {
ratio = d.ratio || d.value;
}

if (asPercent) {
ratio *= 100;
}

return asPercent && ratio ? ratio * 100 : ratio;
return ratio;
};

ChartInternal.prototype.updateDataAttributes = function (name, attrs) {
Expand Down
41 changes: 24 additions & 17 deletions src/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,35 @@ ChartInternal.prototype.getShapeOffset = function (typeFilter, indices, isSub) {
var scale = isSub ? $$.getSubYScale(d.id) : $$.getYScale(d.id),
y0 = scale(0), offset = y0;
targets.forEach(function (t) {
// skip if not part of offset (part1)
if (t.id === d.id || indices[t.id] !== indices[d.id]) {
return;
}

// skip if not part of offset (part 2)
if (targetIds.indexOf(t.id) >= targetIds.indexOf(d.id)) {
return;
}

const rowValues = $$.isStepType(d) ? $$.convertValuesToStep(t.values) : t.values;
const isTargetNormalized = $$.isTargetNormalized(d.id);
const values = rowValues.map(v => (isTargetNormalized ? $$.getRatio("index", v, true) : v.value));

if (t.id === d.id || indices[t.id] !== indices[d.id]) { return; }
if (targetIds.indexOf(t.id) < targetIds.indexOf(d.id)) {
// check if the x values line up
if (isUndefined(rowValues[i]) || +rowValues[i].x !== +d.x) { // "+" for timeseries
// if not, try to find the value that does line up
i = -1;
rowValues.forEach(function (v, j) {
const x1 = v.x.constructor === Date ? +v.x : v.x;
const x2 = d.x.constructor === Date ? +d.x : d.x;
// check if the x values line up
if (isUndefined(rowValues[i]) || +rowValues[i].x !== +d.x) { // "+" for timeseries
// if not, try to find the value that does line up
i = -1;
rowValues.forEach(function (v, j) {
const x1 = v.x.constructor === Date ? +v.x : v.x;
const x2 = d.x.constructor === Date ? +d.x : d.x;

if (x1 === x2) {
i = j;
}
});
}
if (i in rowValues && rowValues[i].value * d.value >= 0) {
offset += scale(values[i]) - y0;
}
if (x1 === x2) {
i = j;
}
});
}
if (i in rowValues && rowValues[i].value * d.value >= 0) {
offset += scale(values[i]) - y0;
}
});
return offset;
Expand Down