Skip to content

Commit

Permalink
Merge pull request #4465 from plotly/symbol-numbers-gl2d
Browse files Browse the repository at this point in the history
Fix symbol numbers in scattergl and splom traces
  • Loading branch information
archmoj authored Jan 6, 2020
2 parents d11e92f + 6468acc commit c91104f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 27 deletions.
33 changes: 22 additions & 11 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,31 @@ drawing.symbolList = [];

Object.keys(SYMBOLDEFS).forEach(function(k) {
var symDef = SYMBOLDEFS[k];
drawing.symbolList = drawing.symbolList.concat(
[symDef.n, k, symDef.n + 100, k + '-open']);
drawing.symbolNames[symDef.n] = k;
drawing.symbolFuncs[symDef.n] = symDef.f;
var n = symDef.n;
drawing.symbolList.push(
n,
k,
n + 100,
k + '-open'
);
drawing.symbolNames[n] = k;
drawing.symbolFuncs[n] = symDef.f;

if(symDef.needLine) {
drawing.symbolNeedLines[symDef.n] = true;
drawing.symbolNeedLines[n] = true;
}
if(symDef.noDot) {
drawing.symbolNoDot[symDef.n] = true;
drawing.symbolNoDot[n] = true;
} else {
drawing.symbolList = drawing.symbolList.concat(
[symDef.n + 200, k + '-dot', symDef.n + 300, k + '-open-dot']);
drawing.symbolList.push(
n + 200,
k + '-dot',
n + 300,
k + '-open-dot'
);
}
if(symDef.noFill) {
drawing.symbolNoFill[symDef.n] = true;
drawing.symbolNoFill[n] = true;
}
});

Expand All @@ -253,8 +263,9 @@ drawing.symbolNumber = function(v) {
v = drawing.symbolNames.indexOf(v);
if(v >= 0) { v += vbase; }
}
if((v % 100 >= MAXSYMBOL) || v >= 400) { return 0; }
return Math.floor(Math.max(v, 0));

return (v % 100 >= MAXSYMBOL || v >= 400) ?
0 : Math.floor(Math.max(v, 0));
};

function makePointPath(symbolNumber, r) {
Expand Down
22 changes: 13 additions & 9 deletions src/traces/scattergl/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var formatColor = require('../../lib/gl_format_color').formatColor;
var subTypes = require('../scatter/subtypes');
var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');

var helpers = require('./helpers');
var constants = require('./constants');
var DESELECTDIM = require('../../constants/interactions').DESELECTDIM;

Expand Down Expand Up @@ -59,7 +60,7 @@ function convertStyle(gd, trace) {
opts.markerSel = convertMarkerSelection(trace, trace.selected);
opts.markerUnsel = convertMarkerSelection(trace, trace.unselected);

if(!trace.unselected && Array.isArray(trace.marker.opacity)) {
if(!trace.unselected && Lib.isArrayOrTypedArray(trace.marker.opacity)) {
var mo = trace.marker.opacity;
opts.markerUnsel.opacity = new Array(mo.length);
for(i = 0; i < mo.length; i++) {
Expand Down Expand Up @@ -187,15 +188,18 @@ function convertTextStyle(gd, trace) {
optsOut.color = tfc;
}

if(Array.isArray(tfs) || Array.isArray(tff)) {
if(Lib.isArrayOrTypedArray(tfs) || Array.isArray(tff)) {
// if any textfont param is array - make render a batch
optsOut.font = new Array(count);
for(i = 0; i < count; i++) {
var fonti = optsOut.font[i] = {};

fonti.size = Array.isArray(tfs) ?
(isNumeric(tfs[i]) ? tfs[i] : 0) :
tfs;
fonti.size = (
Lib.isTypedArray(tfs) ? tfs[i] :
Array.isArray(tfs) ? (
isNumeric(tfs[i]) ? tfs[i] : 0
) : tfs
);

fonti.family = Array.isArray(tff) ? tff[i] : tff;
}
Expand All @@ -214,15 +218,15 @@ function convertMarkerStyle(trace) {
var optsOut = {};
var i;

var multiSymbol = Array.isArray(optsIn.symbol);
var multiSymbol = Lib.isArrayOrTypedArray(optsIn.symbol);
var multiColor = Lib.isArrayOrTypedArray(optsIn.color);
var multiLineColor = Lib.isArrayOrTypedArray(optsIn.line.color);
var multiOpacity = Lib.isArrayOrTypedArray(optsIn.opacity);
var multiSize = Lib.isArrayOrTypedArray(optsIn.size);
var multiLineWidth = Lib.isArrayOrTypedArray(optsIn.line.width);

var isOpen;
if(!multiSymbol) isOpen = constants.OPEN_RE.test(optsIn.symbol);
if(!multiSymbol) isOpen = helpers.isOpenSymbol(optsIn.symbol);

// prepare colors
if(multiSymbol || multiColor || multiLineColor || multiOpacity) {
Expand Down Expand Up @@ -253,7 +257,7 @@ function convertMarkerStyle(trace) {
for(i = 0; i < count; i++) {
if(multiSymbol) {
var symbol = optsIn.symbol[i];
isOpen = constants.OPEN_RE.test(symbol);
isOpen = helpers.isOpenSymbol(symbol);
}
if(isOpen) {
borderColors[i] = colors[i].slice();
Expand Down Expand Up @@ -398,7 +402,7 @@ function getSymbolSdf(symbol) {
var symbolNoDot = !!Drawing.symbolNoDot[symbolNumber % 100];
var symbolNoFill = !!Drawing.symbolNoFill[symbolNumber % 100];

var isDot = constants.DOT_RE.test(symbol);
var isDot = helpers.isDotSymbol(symbol);

// get symbol sdf from cache or generate it
if(SYMBOL_SDF[symbol]) return SYMBOL_SDF[symbol];
Expand Down
3 changes: 2 additions & 1 deletion src/traces/scattergl/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var Lib = require('../../lib');
var Registry = require('../../registry');

var helpers = require('./helpers');
var attributes = require('./attributes');
var constants = require('../scatter/constants');
var subTypes = require('../scatter/subtypes');
Expand All @@ -25,7 +26,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}

var isOpen = traceIn.marker ? /-open/.test(traceIn.marker.symbol) : false;
var isOpen = traceIn.marker ? helpers.isOpenSymbol(traceIn.marker.symbol) : false;
var isBubble = subTypes.isBubble(traceIn);

var len = handleXYDefaults(traceIn, traceOut, layout, coerce);
Expand Down
23 changes: 23 additions & 0 deletions src/traces/scattergl/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2012-2020, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var constants = require('./constants');

exports.isOpenSymbol = function(symbol) {
return (typeof symbol === 'string') ?
constants.OPEN_RE.test(symbol) :
symbol % 200 > 100;
};

exports.isDotSymbol = function(symbol) {
return (typeof symbol === 'string') ?
constants.DOT_RE.test(symbol) :
symbol > 200;
};
8 changes: 4 additions & 4 deletions src/traces/scattergl/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function calcHover(pointData, x, y, trace) {

var font = trace.textfont;
if(font) {
di.ts = Array.isArray(font.size) ? font.size[id] : font.size;
di.ts = Lib.isArrayOrTypedArray(font.size) ? font.size[id] : font.size;
di.tc = Array.isArray(font.color) ? font.color[id] : font.color;
di.tf = Array.isArray(font.family) ? font.family[id] : font.family;
}
Expand All @@ -118,7 +118,7 @@ function calcHover(pointData, x, y, trace) {
if(marker) {
di.ms = Lib.isArrayOrTypedArray(marker.size) ? marker.size[id] : marker.size;
di.mo = Lib.isArrayOrTypedArray(marker.opacity) ? marker.opacity[id] : marker.opacity;
di.mx = Array.isArray(marker.symbol) ? marker.symbol[id] : marker.symbol;
di.mx = Lib.isArrayOrTypedArray(marker.symbol) ? marker.symbol[id] : marker.symbol;
di.mc = Lib.isArrayOrTypedArray(marker.color) ? marker.color[id] : marker.color;
}

Expand All @@ -143,10 +143,10 @@ function calcHover(pointData, x, y, trace) {
if(hoverlabel) {
di.hbg = Array.isArray(hoverlabel.bgcolor) ? hoverlabel.bgcolor[id] : hoverlabel.bgcolor;
di.hbc = Array.isArray(hoverlabel.bordercolor) ? hoverlabel.bordercolor[id] : hoverlabel.bordercolor;
di.hts = Array.isArray(hoverlabel.font.size) ? hoverlabel.font.size[id] : hoverlabel.font.size;
di.hts = Lib.isArrayOrTypedArray(hoverlabel.font.size) ? hoverlabel.font.size[id] : hoverlabel.font.size;
di.htc = Array.isArray(hoverlabel.font.color) ? hoverlabel.font.color[id] : hoverlabel.font.color;
di.htf = Array.isArray(hoverlabel.font.family) ? hoverlabel.font.family[id] : hoverlabel.font.family;
di.hnl = Array.isArray(hoverlabel.namelength) ? hoverlabel.namelength[id] : hoverlabel.namelength;
di.hnl = Lib.isArrayOrTypedArray(hoverlabel.namelength) ? hoverlabel.namelength[id] : hoverlabel.namelength;
}
var hoverinfo = trace.hoverinfo;
if(hoverinfo) {
Expand Down
4 changes: 2 additions & 2 deletions src/traces/splom/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var attributes = require('./attributes');
var subTypes = require('../scatter/subtypes');
var handleMarkerDefaults = require('../scatter/marker_defaults');
var mergeLength = require('../parcoords/merge_length');
var OPEN_RE = /-open/;
var isOpenSymbol = require('../scattergl/helpers').isOpenSymbol;

module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
function coerce(attr, dflt) {
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout

handleMarkerDefaults(traceIn, traceOut, defaultColor, layout, coerce);

var isOpen = OPEN_RE.test(traceOut.marker.symbol);
var isOpen = isOpenSymbol(traceOut.marker.symbol);
var isBubble = subTypes.isBubble(traceOut);
coerce('marker.line.width', isOpen || isBubble ? 1 : 0);

Expand Down
Binary file added test/image/baselines/gl2d_symbol_numbers.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 added test/image/baselines/splom_symbol_numbers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/image/mocks/gl2d_symbol_numbers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"data": [
{
"y": [
0,
0,
0
],
"type": "scattergl",
"marker": {
"size": 24,
"symbol": "diamond-open-dot"
}
},
{
"y": [
1,
1,
1
],
"type": "scattergl",
"marker": {
"size": 24,
"symbol": 302
}
}
],
"layout": {
"width": 600,
"height": 400
}
}
41 changes: 41 additions & 0 deletions test/image/mocks/splom_symbol_numbers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"data": [
{
"type": "splom",
"marker": {
"size": [
12,
16,
24,
32
],
"symbol": [
1,
102,
203,
304
]
},
"dimensions": [
{
"values": [
0,
1,
2,
3
],
"label": "A"
},
{
"values": [
0,
2,
5,
6
],
"label": "B"
}
]
}
]
}

0 comments on commit c91104f

Please sign in to comment.