Skip to content

Commit

Permalink
show text when legend item only has text - centralize style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed May 27, 2021
1 parent 0bd9660 commit 10d1138
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
92 changes: 59 additions & 33 deletions src/components/legend/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ module.exports = function style(s, gd, legend) {
.each(styleOHLC);

function styleLines(d) {
var styleGuide = getStyleGuide(d);
var showFill = styleGuide.showFill;
var showLine = styleGuide.showLine;
var showGradientLine = styleGuide.showGradientLine;
var showGradientFill = styleGuide.showGradientFill;
var anyFill = styleGuide.anyFill;
var anyLine = styleGuide.anyLine;

var d0 = d[0];
var trace = d0.trace;
var showFill = trace.visible && trace.fill && trace.fill !== 'none';
var showLine = subTypes.hasLines(trace);
var contours = trace.contours;
var showGradientLine = false;
var showGradientFill = false;
var dMod, tMod;

var cOpts = extractOpts(trace);
Expand All @@ -127,28 +130,10 @@ module.exports = function style(s, gd, legend) {
}
};

if(contours) {
var coloring = contours.coloring;

if(coloring === 'lines') {
showGradientLine = true;
} else {
showLine = coloring === 'none' || coloring === 'heatmap' || contours.showlines;
}

if(contours.type === 'constraint') {
showFill = contours._operation !== '=';
} else if(coloring === 'fill' || coloring === 'heatmap') {
showGradientFill = true;
}
}

// with fill and no markers or text, move the line and fill up a bit
// so it's more centered
var markersOrText = subTypes.hasMarkers(trace) || subTypes.isText(trace);
var anyFill = showFill || showGradientFill;
var anyLine = showLine || showGradientLine;
var pathStart = (markersOrText || !anyFill) ? 'M5,0' :

var pathStart = (subTypes.hasMarkers(trace) || !anyFill) ? 'M5,0' :
// with a line leave it slightly below center, to leave room for the
// line thickness and because the line is usually more prominent
anyLine ? 'M5,-2' : 'M5,-3';
Expand Down Expand Up @@ -184,11 +169,15 @@ module.exports = function style(s, gd, legend) {
}

function stylePoints(d) {
var styleGuide = getStyleGuide(d);
var anyFill = styleGuide.anyFill;
var anyLine = styleGuide.anyLine;
var showLine = styleGuide.showLine;
var showMarker = styleGuide.showMarker;

var d0 = d[0];
var trace = d0.trace;
var showMarkers = subTypes.hasMarkers(trace);
var showText = subTypes.isText(trace);
var showLines = subTypes.hasLines(trace);
var showText = !showMarker && !anyLine && !anyFill && subTypes.hasText(trace);
var dMod, tMod;

// 'scatter3d' don't use gd.calcdata,
Expand Down Expand Up @@ -217,11 +206,11 @@ module.exports = function style(s, gd, legend) {
}

// constrain text, markers, etc so they'll fit on the legend
if(showMarkers || showText || showLines) {
if(showMarker || showText || showLine) {
var dEdit = {};
var tEdit = {};

if(showMarkers) {
if(showMarker) {
dEdit.mc = boundVal('marker.color', pickFirst);
dEdit.mx = boundVal('marker.symbol', pickFirst);
dEdit.mo = boundVal('marker.opacity', Lib.mean, [0.2, 1]);
Expand All @@ -238,7 +227,7 @@ module.exports = function style(s, gd, legend) {
tEdit.marker.size = ms;
}

if(showLines) {
if(showLine) {
tEdit.line = {
width: boundVal('line.width', pickFirst, [0, 10], CST_LINE_WIDTH)
};
Expand All @@ -265,7 +254,7 @@ module.exports = function style(s, gd, legend) {
var ptgroup = d3.select(this).select('g.legendpoints');

var pts = ptgroup.selectAll('path.scatterpts')
.data(showMarkers ? dMod : []);
.data(showMarker ? dMod : []);
// make sure marker is on the bottom, in case it enters after text
pts.enter().insert('path', ':first-child')
.classed('scatterpts', true)
Expand All @@ -275,7 +264,7 @@ module.exports = function style(s, gd, legend) {

// 'mrc' is set in pointStyle and used in textPointStyle:
// constrain it here
if(showMarkers) dMod[0].mrc = 3;
if(showMarker) dMod[0].mrc = 3;

var txt = ptgroup.selectAll('g.pointtext')
.data(showText ? dMod : []);
Expand Down Expand Up @@ -636,3 +625,40 @@ function getGradientDirection(reversescale, isRadial) {
var str = isRadial ? 'radial' : 'horizontal';
return str + (reversescale ? '' : 'reversed');
}

function getStyleGuide(d) {
var trace = d[0].trace;
var contours = trace.contours;
var showLine = subTypes.hasLines(trace);
var showMarker = subTypes.hasMarkers(trace);

var showFill = trace.visible && trace.fill && trace.fill !== 'none';
var showGradientLine = false;
var showGradientFill = false;

if(contours) {
var coloring = contours.coloring;

if(coloring === 'lines') {
showGradientLine = true;
} else {
showLine = coloring === 'none' || coloring === 'heatmap' || contours.showlines;
}

if(contours.type === 'constraint') {
showFill = contours._operation !== '=';
} else if(coloring === 'fill' || coloring === 'heatmap') {
showGradientFill = true;
}
}

return {
showMarker: showMarker,
showLine: showLine,
showFill: showFill,
showGradientLine: showGradientLine,
showGradientFill: showGradientFill,
anyLine: showLine || showGradientLine,
anyFill: showFill || showGradientFill,
};
}
4 changes: 0 additions & 4 deletions src/traces/scatter/subtypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ module.exports = {
trace.mode.indexOf('text') !== -1;
},

isText: function(trace) {
return trace.visible && trace.mode === 'text';
},

isBubble: function(trace) {
return Lib.isPlainObject(trace.marker) &&
Lib.isArrayOrTypedArray(trace.marker.size);
Expand Down

0 comments on commit 10d1138

Please sign in to comment.