Skip to content
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

Fix color clustering #2377

Merged
merged 18 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
16 changes: 16 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,19 @@ lib.subplotSort = function(a, b) {
}
return numB - numA;
};

// repeatable pseudorandom generator
var randSeed = 2000000000;

lib.seedPseudoRandom = function() {
randSeed = 2000000000;
};

lib.pseudoRandom = function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea - I bet we'll find other places this should be used too.

var lastVal = randSeed;
randSeed = (69069 * randSeed + 1) % 4294967296;
// don't let consecutive vals be too close together
// gets away from really trying to be random, in favor of better local uniformity
if(Math.abs(randSeed - lastVal) < 429496729) return lib.pseudoRandom();
return randSeed / 4294967296;
};
22 changes: 3 additions & 19 deletions src/traces/box/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ var d3 = require('d3');
var Lib = require('../../lib');
var Drawing = require('../../components/drawing');

// repeatable pseudorandom generator
var randSeed = 2000000000;

function seed() {
randSeed = 2000000000;
}

function rand() {
var lastVal = randSeed;
randSeed = (69069 * randSeed + 1) % 4294967296;
// don't let consecutive vals be too close together
// gets away from really trying to be random, in favor of better local uniformity
if(Math.abs(randSeed - lastVal) < 429496729) return rand();
return randSeed / 4294967296;
}

// constants for dynamic jitter (ie less jitter for sparser points)
var JITTERCOUNT = 5; // points either side of this to include
var JITTERSPREAD = 0.01; // fraction of IQR to count as "dense"
Expand Down Expand Up @@ -179,8 +163,8 @@ function plotPoints(sel, axes, trace, t) {
// to support violin points
var mode = trace.boxpoints || trace.points;

// repeatable pseudorandom number generator
seed();
// repeatable pseudo-random number generator
Lib.seedPseudoRandom();

sel.selectAll('g.points')
// since box plot points get an extra level of nesting, each
Expand Down Expand Up @@ -247,7 +231,7 @@ function plotPoints(sel, axes, trace, t) {
var v = pt.v;

var jitterOffset = trace.jitter ?
(newJitter * jitterFactors[i] * (rand() - 0.5)) :
(newJitter * jitterFactors[i] * (Lib.pseudoRandom() - 0.5)) :
0;

var posPx = d.pos + bPos + bdPos * (trace.pointpos + jitterOffset);
Expand Down
8 changes: 5 additions & 3 deletions test/jasmine/tests/gl2d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var selectButton = require('../assets/modebar_button');
var delay = require('../assets/delay');
var readPixel = require('../assets/read_pixel');


function countCanvases() {
return d3.selectAll('canvas').size();
}
Expand Down Expand Up @@ -797,9 +796,11 @@ describe('@gl Test gl2d plots', function() {
var w = 500;
var h = 500;

Lib.seedPseudoRandom();

for(var i = 0; i < N; i++) {
x.push(i);
color.push(Math.random());
color.push(Lib.pseudoRandom());
}

Plotly.newPlot(gd, [{
Expand All @@ -824,8 +825,9 @@ describe('@gl Test gl2d plots', function() {
var total = readPixel(gd.querySelector('.gl-canvas-context'), 0, 0, w, h)
.reduce(function(acc, v) { return acc + v; }, 0);

// the total value was 3762487 before PR
// the total value was 3777134 before PR
// https://github.com/plotly/plotly.js/pull/2377
// and 105545275 after.
expect(total).toBeGreaterThan(4e6);
})
.catch(fail)
Expand Down