diff --git a/src/data.js b/src/data.js index 6de610e5d..c08e84da7 100644 --- a/src/data.js +++ b/src/data.js @@ -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) { diff --git a/src/shape.js b/src/shape.js index 834d0f2f0..d289baf2f 100644 --- a/src/shape.js +++ b/src/shape.js @@ -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;