Skip to content

indicator: several improvements/fixes #4029

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

Merged
merged 18 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
10 changes: 7 additions & 3 deletions src/traces/indicator/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function calc(gd, trace) {
var cd = [];

var lastReading = trace.value;
var secondLastReading = trace.delta ? trace.delta.reference : trace._lastValue || trace.value;
var secondLastReading = trace._lastValue || trace.value;
var deltaRef = secondLastReading;
if(trace._hasDelta && typeof trace.delta.reference === 'number') {
deltaRef = trace.delta.reference;
}
cd[0] = {
y: lastReading,
lastY: secondLastReading,

delta: lastReading - secondLastReading,
relativeDelta: (lastReading - secondLastReading) / secondLastReading,
delta: lastReading - deltaRef,
relativeDelta: (lastReading - deltaRef) / deltaRef,
};
return cd;
}
Expand Down
4 changes: 3 additions & 1 deletion src/traces/indicator/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
var bignumberFontSize;
if(traceOut._hasNumber) {
coerce('number.valueformat');
if(!traceOut.number.valueformat) traceOut.number.valueformat = attributes.number.valueformat.dflt;
coerce('number.font.color', layout.font.color);
coerce('number.font.family', layout.font.family);
coerce('number.font.size');
Expand All @@ -63,7 +64,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
}
coerce('delta.reference', traceOut.value);
coerce('delta.relative');
coerce('delta.valueformat', traceOut.delta.relative ? '2%' : '.3s');
coerce('delta.valueformat');
if(!traceOut.delta.valueformat) traceOut.delta.valueformat = traceOut.delta.relative ? '+2%' : '+.3s';
coerce('delta.increasing.symbol');
coerce('delta.increasing.color');
coerce('delta.decreasing.symbol');
Expand Down
185 changes: 131 additions & 54 deletions src/traces/indicator/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function drawBulletGauge(gd, plotGroup, cd, gaugeOpts) {

function drawRect(s) {
s
.attr('width', function(d) { return Math.max(0, ax.c2p(d.range[1] - d.range[0]));})
.attr('width', function(d) { return Math.max(0, ax.c2p(d.range[1]) - ax.c2p(d.range[0]));})
.attr('x', function(d) { return ax.c2p(d.range[0]);})
.attr('y', function(d) { return 0.5 * (1 - d.thickness) * bulletHeight;})
.attr('height', function(d) { return d.thickness * bulletHeight; });
Expand Down Expand Up @@ -527,6 +527,7 @@ function drawNumbers(gd, plotGroup, cd, opts) {

var numbers = Lib.ensureSingle(plotGroup, 'g', 'numbers');
var bignumberbBox, deltabBox;
var numbersbBox;

var data = [];
if(trace._hasNumber) data.push('number');
Expand Down Expand Up @@ -561,11 +562,12 @@ function drawNumbers(gd, plotGroup, cd, opts) {
.transition()
.duration(transitionOpts.duration)
.ease(transitionOpts.easing)
.each('end', function() { trace._lastValue = cd[0].y; onComplete && onComplete(); })
.each('end', function() { onComplete && onComplete(); })
.each('interrupt', function() { onComplete && onComplete(); })
.attrTween('text', function() {
var that = d3.select(this);
var interpolator = d3.interpolateNumber(cd[0].lastY, cd[0].y);
trace._lastValue = cd[0].y;
return function(t) {
that.text(bignumberPrefix + fmt(interpolator(t)) + bignumberSuffix);
};
Expand All @@ -574,17 +576,14 @@ function drawNumbers(gd, plotGroup, cd, opts) {
number.text(bignumberPrefix + fmt(cd[0].y) + bignumberSuffix);
}

number.attr('data-unformatted', bignumberPrefix + fmt(cd[0].y) + bignumberSuffix);
bignumberbBox = Drawing.bBox(number.node());

bignumberbBox = measureText(bignumberPrefix + fmt(cd[0].y) + bignumberSuffix, trace.number.font, numbersAnchor);
return number;
}

function drawDelta() {
// delta
var deltaAx = mockAxis(gd, {tickformat: trace.delta.valueformat});
var deltaFmt = function(v) { return Axes.tickText(deltaAx, v).text;};
if(!trace._deltaLastValue) trace._deltaLastValue = 0;
var deltaValue = function(d) {
var value = trace.delta.relative ? d.relativeDelta : d.delta;
return value;
Expand All @@ -596,44 +595,58 @@ function drawNumbers(gd, plotGroup, cd, opts) {
var deltaFill = function(d) {
return d.delta >= 0 ? trace.delta.increasing.color : trace.delta.decreasing.color;
};
if(trace._deltaLastValue === undefined) {
trace._deltaLastValue = deltaValue(cd[0]);
}
var delta = numbers.select('text.delta');
delta
.call(Drawing.font, trace.delta.font)
.call(Color.fill, deltaFill(cd[0]));
.call(Color.fill, deltaFill({delta: trace._deltaLastValue}));

if(hasTransition) {
delta
.transition()
.duration(transitionOpts.duration)
.ease(transitionOpts.easing)
.each('end', function(d) { trace._deltaLastValue = deltaValue(d); onComplete && onComplete(); })
.each('interrupt', function() { onComplete && onComplete(); })
.attrTween('text', function() {
.tween('text', function() {
var that = d3.select(this);
var to = deltaValue(cd[0]);
var from = trace._deltaLastValue;
var interpolator = d3.interpolateNumber(from, to);
trace._deltaLastValue = to;
return function(t) {
that.text(deltaFormatText(interpolator(t)));
that.call(Color.fill, deltaFill({delta: interpolator(t)}));
};
});
})
.each('end', function() { onComplete && onComplete(); })
.each('interrupt', function() { onComplete && onComplete(); });
} else {
delta.text(function() {
return deltaFormatText(deltaValue(cd[0]));
});
})
.call(Color.fill, deltaFill(cd[0]));
}

delta.attr('data-unformatted', deltaFormatText(deltaValue(cd[0])));
deltabBox = Drawing.bBox(delta.node());

deltabBox = measureText(deltaFormatText(deltaValue(cd[0])), trace.delta.font, numbersAnchor);
return delta;
}

// Position delta relative to bignumber
var key = trace.mode + trace.align;
var delta;
if(trace._hasDelta) delta = drawDelta();
if(trace._hasNumber) drawBignumber();
if(trace._hasDelta) {
delta = drawDelta();
key += trace.delta.position + trace.delta.font.size + trace.delta.font.family + trace.delta.valueformat;
key += trace.delta.increasing.symbol + trace.delta.decreasing.symbol;
numbersbBox = deltabBox;
}
if(trace._hasNumber) {
drawBignumber();
key += trace.number.font.size + trace.number.font.family + trace.number.valueformat + trace.number.suffix + trace.number.prefix;
numbersbBox = bignumberbBox;
}

// Position delta relative to bignumber
if(trace._hasDelta && trace._hasNumber) {
var bignumberCenter = [
(bignumberbBox.left + bignumberbBox.right) / 2,
Expand All @@ -644,51 +657,92 @@ function drawNumbers(gd, plotGroup, cd, opts) {
(deltabBox.top + deltabBox.bottom) / 2
];

var dx, dy;
var padding = 0.75 * trace.delta.font.size;
if(trace.delta.position === 'left') {
delta.attr('dx', bignumberbBox.left - deltabBox.right - cn.horizontalPadding);
delta.attr('dy', bignumberCenter[1] - deltaCenter[1]);
dx = cache(trace, 'deltaPos', 0, -1 * (bignumberbBox.width * (position[trace.align]) + deltabBox.width * (1 - position[trace.align]) + padding), key, Math.min);
dy = bignumberCenter[1] - deltaCenter[1];

numbersbBox = {
width: bignumberbBox.width + deltabBox.width + padding,
height: Math.max(bignumberbBox.height, deltabBox.height),
left: deltabBox.left + dx,
right: bignumberbBox.right,
top: Math.min(bignumberbBox.top, deltabBox.top + dy),
bottom: Math.max(bignumberbBox.bottom, deltabBox.bottom + dy)
};
}
if(trace.delta.position === 'right') {
delta.attr('dx', bignumberbBox.right - deltabBox.left + cn.horizontalPadding);
delta.attr('dy', bignumberCenter[1] - deltaCenter[1]);
dx = cache(trace, 'deltaPos', 0, bignumberbBox.width * (1 - position[trace.align]) + deltabBox.width * position[trace.align] + padding, key, Math.max);
dy = bignumberCenter[1] - deltaCenter[1];

numbersbBox = {
width: bignumberbBox.width + deltabBox.width + padding,
height: Math.max(bignumberbBox.height, deltabBox.height),
left: bignumberbBox.left,
right: deltabBox.right + dx,
top: Math.min(bignumberbBox.top, deltabBox.top + dy),
bottom: Math.max(bignumberbBox.bottom, deltabBox.bottom + dy)
};
}
if(trace.delta.position === 'bottom') {
delta.attr('dx', null);
delta.attr('dy', deltabBox.height);
dx = null;
dy = deltabBox.height;

numbersbBox = {
width: Math.max(bignumberbBox.width, deltabBox.width),
height: bignumberbBox.height + deltabBox.height,
left: Math.min(bignumberbBox.left, deltabBox.left),
right: Math.max(bignumberbBox.right, deltabBox.right),
top: bignumberbBox.bottom - bignumberbBox.height,
bottom: bignumberbBox.bottom + deltabBox.height
};
}
if(trace.delta.position === 'top') {
delta.attr('dx', null);
delta.attr('dy', bignumberbBox.top);
dx = null;
dy = bignumberbBox.top;

numbersbBox = {
width: Math.max(bignumberbBox.width, deltabBox.width),
height: bignumberbBox.height + deltabBox.height,
left: Math.min(bignumberbBox.left, deltabBox.left),
right: Math.max(bignumberbBox.right, deltabBox.right),
top: bignumberbBox.bottom - bignumberbBox.height - deltabBox.height,
bottom: bignumberbBox.bottom
};
}

delta.attr({dx: dx, dy: dy});
}

// Resize numbers to fit within space and position
numbers.attr('transform', function() {
var m = opts.numbersScaler(numbers);
var key = m[2];
if(!(trace._numbersScale && trace._numbersScale.key === key)) {
trace._numbersScale = {key: key, value: 1};
}
var scaleRatio = trace._numbersScale.value = Math.min(trace._numbersScale.value, m[0]);
var numbersbBox = m[1];
var translateY;
if(!trace._scaleNumbers) scaleRatio = 1;
if(trace._isAngular) {
// align vertically to bottom
translateY = numbersY - scaleRatio * numbersbBox.bottom;
} else {
// align vertically to center
translateY = numbersY - scaleRatio * (numbersbBox.top + numbersbBox.bottom) / 2;
}
if(trace._hasNumber || trace._hasDelta) {
numbers.attr('transform', function() {
var m = opts.numbersScaler(numbersbBox);
key += m[2];
var scaleRatio = cache(trace, 'numbersScale', 1, m[0], key, Math.min);
var translateY;
if(!trace._scaleNumbers) scaleRatio = 1;
if(trace._isAngular) {
// align vertically to bottom
translateY = numbersY - scaleRatio * numbersbBox.bottom;
} else {
// align vertically to center
translateY = numbersY - scaleRatio * (numbersbBox.top + numbersbBox.bottom) / 2;
}

// Stash the top position of numbersbBox for title positioning
trace._numbersTop = scaleRatio * (numbersbBox.top) + translateY;
// Stash the top position of numbersbBox for title positioning
trace._numbersTop = scaleRatio * (numbersbBox.top) + translateY;

var ref = numbersbBox[numbersAlign];
if(numbersAlign === 'center') ref = (numbersbBox.left + numbersbBox.right) / 2;
var translateX = numbersX - scaleRatio * ref;
return strTranslate(translateX, translateY) + ' scale(' + scaleRatio + ')';
});
var ref = numbersbBox[numbersAlign];
if(numbersAlign === 'center') ref = (numbersbBox.left + numbersbBox.right) / 2;
var translateX = numbersX - scaleRatio * ref;

// Stash translateX
translateX = cache(trace, 'numbersTranslate', 0, translateX, key, Math.max);
return strTranslate(translateX, translateY) + ' scale(' + scaleRatio + ')';
});
}
}

// Apply fill, stroke, stroke-width to SVG shape
Expand Down Expand Up @@ -769,17 +823,40 @@ function strTranslate(x, y) {
return 'translate(' + x + ',' + y + ')';
}

function fitTextInsideBox(el, width, height) {
function fitTextInsideBox(textBB, width, height) {
// compute scaling ratio to have text fit within specified width and height
var textBB = Drawing.bBox(el.node());
// var textBB = Drawing.bBox(el.node());
var ratio = Math.min(width / textBB.width, height / textBB.height);
return [ratio, textBB, width + 'x' + height];
}

function fitTextInsideCircle(el, radius) {
function fitTextInsideCircle(textBB, radius) {
// compute scaling ratio to have text fit within specified radius
var textBB = Drawing.bBox(el.node());
// var textBB = Drawing.bBox(el.node());
var elRadius = Math.sqrt((textBB.width / 2) * (textBB.width / 2) + textBB.height * textBB.height);
var ratio = radius / elRadius;
return [ratio, textBB, radius];
}

function measureText(txt, font, textAnchor) {
var element = document.createElementNS('http://www.w3.org/2000/svg', 'text');
var sel = d3.select(element);
sel.text(txt)
.attr('x', 0)
.attr('y', 0)
.attr('text-anchor', textAnchor)
.attr('data-unformatted', txt)
.call(Drawing.font, font);
return Drawing.bBox(sel.node());
}

function cache(trace, name, initialValue, value, key, fn) {
var objName = '_cache' + name;
if(!(trace[objName] && trace[objName].key === key)) {
trace[objName] = {key: key, value: initialValue};
}
var v = Lib.aggNums(fn, null, [trace[objName].value, value], 2);
trace[objName].value = v;

return v;
}
Binary file modified test/image/baselines/gl3d_indicator_scatter3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_bignumber.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_datacard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_datacard2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_datacard3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_gauge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_grid_template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions test/jasmine/tests/indicator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ describe('Indicator defaults', function() {
it('defaults to formatting numbers using SI prefix', function() {
var out = _supply({type: 'indicator', mode: 'number+delta', value: 1});
expect(out.number.valueformat).toBe('.3s');
expect(out.delta.valueformat).toBe('.3s');
expect(out.delta.valueformat).toBe('+.3s');
});

it('defaults to displaying relative changes in percentage', function() {
var out = _supply({type: 'indicator', mode: 'delta', delta: {relative: true}, value: 1});
expect(out.delta.valueformat).toBe('2%');
expect(out.delta.valueformat).toBe('+2%');
});

it('ignores empty valueformat', function() {
var out = _supply({type: 'indicator', mode: 'number+delta', number: {valueformat: ''}, delta: {valueformat: ''}, value: 1});
expect(out.delta.valueformat).toBe('+.3s');
expect(out.number.valueformat).toBe('.3s');
});

it('defaults delta.reference to current value', function() {
Expand Down Expand Up @@ -292,11 +298,11 @@ describe('Indicator plot', function() {
delta: {reference: 200}
}], {width: 400, height: 400})
.then(function() {
assertContent(gd._fullData[0].delta.increasing.symbol + '20.0');
assertContent(gd._fullData[0].delta.increasing.symbol + '+20.0');
return Plotly.restyle(gd, 'delta.relative', true);
})
.then(function() {
assertContent(gd._fullData[0].delta.increasing.symbol + '10%');
assertContent(gd._fullData[0].delta.increasing.symbol + '+10%');
return Plotly.restyle(gd, 'delta.valueformat', '.3f');
})
.then(function() {
Expand Down Expand Up @@ -504,7 +510,7 @@ describe('Indicator animations', function() {

Plotly.plot(gd, mock)
.then(function() {
gd.data[0].value = '400';
gd.data[0].value = 400;
return Plotly.react(gd, gd.data, gd.layout);
})
.then(delay(300))
Expand Down