Skip to content

Commit 5f45be4

Browse files
authored
Merge pull request #441 from DigitalSlideArchive/plot-colors
Make plot colors more consistent for numerous discrete values
2 parents 6ac5373 + e73f805 commit 5f45be4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

histomicsui/web_client/panels/MetadataPlot.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,19 @@ var MetadataPlot = Panel.extend({
465465
},
466466

467467
plotDataToPlotly: function (plotData) {
468-
const colorBrewerPaired12 = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'];
468+
let colorBrewerPaired12 = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'];
469469
const viridis = ['#440154', '#482172', '#423d84', '#38578c', '#2d6f8e', '#24858d', '#1e9a89', '#2ab07e', '#51c468', '#86d449', '#c2df22', '#fde724'];
470-
let colorScale;
471-
if (plotData.series.c && (plotData.series.c.type === 'number' || !plotData.series.c.distinctcount)) {
470+
// concatenate this so we have predictable colors for a longer scale
471+
// for discrete values. It would probably be better to use a longer
472+
// scale
473+
colorBrewerPaired12 = colorBrewerPaired12.concat(viridis);
474+
let colorScale, sColorScale;
475+
if (plotData.series.c && (plotData.series.c.type === 'number' || !plotData.series.c.distinctcount || plotData.series.c.distinctcount > colorBrewerPaired12.length)) {
472476
colorScale = window.d3.scale.linear().domain(viridis.map((_, i) => i / (viridis.length - 1) * ((plotData.series.c.max - plotData.series.c.min) || 0) + plotData.series.c.min)).range(viridis);
473477
}
478+
if (plotData.series.s && (plotData.series.s.type === 'number' || !plotData.series.s.distinctcount || plotData.series.s.distinctcount > colorBrewerPaired12.length)) {
479+
sColorScale = window.d3.scale.linear().domain(viridis.map((_, i) => i / (viridis.length - 1) * ((plotData.series.s.max - plotData.series.s.min) || 0) + plotData.series.s.min)).range(viridis);
480+
}
474481
const plotlyData = {
475482
x: plotData.series.x ? plotData.data.map((d) => d[plotData.series.x.index]) : 0,
476483
y: plotData.data.map((d) => d[plotData.series.y.index]),
@@ -490,7 +497,7 @@ var MetadataPlot = Panel.extend({
490497
: 10,
491498
color: plotData.series.c
492499
? (
493-
!plotData.series.c.distinctcount
500+
colorScale
494501
? plotData.data.map((d) => colorScale(d[plotData.series.c.index]))
495502
: plotData.data.map((d) => colorBrewerPaired12[plotData.series.c.distinct.indexOf(d[plotData.series.c.index])] || '#000000')
496503
)
@@ -524,8 +531,15 @@ var MetadataPlot = Panel.extend({
524531
for (let didx = 0; didx < plotData.data.length; didx += 1) {
525532
if (plotData.data[didx][plotData.series.s.index] === k) {
526533
const cval = plotData.data[didx][plotData.series.c.index];
527-
const cidx = plotData.series.c.distinct.indexOf(cval);
528-
return {target: kidx, value: {line: {color: colorBrewerPaired12[cidx]}}};
534+
const cidx = plotData.series.s.distinct.indexOf(cval);
535+
return {
536+
target: kidx,
537+
value: {
538+
line: {
539+
color: colorScale ? sColorScale[cval] : colorBrewerPaired12[cidx]
540+
}
541+
}
542+
};
529543
}
530544
}
531545
return {target: kidx, value: {line: {color: '#000000'}}};

0 commit comments

Comments
 (0)