Commit 3e7a177a authored by David Schnur's avatar David Schnur

Merge pull request #63 from dnschnur/better-legend-colors

Auto-generation of legend colors no longer results in only white and black above a certain number of series.
parents c4a74e57 2a7ae881
...@@ -126,6 +126,9 @@ Bug fixes ...@@ -126,6 +126,9 @@ Bug fixes
- Axis labels no longer appear as decimals rather than integers in - Axis labels no longer appear as decimals rather than integers in
certain cases. (patch by Clemens Stolle, issue 541) certain cases. (patch by Clemens Stolle, issue 541)
- Automatic color generation no longer produces only whites and blacks
when there are many series. (patch by David Schnur and Tom Cleaveland)
Flot 0.7 Flot 0.7
-------- --------
......
...@@ -419,55 +419,59 @@ ...@@ -419,55 +419,59 @@
} }
function fillInSeriesOptions() { function fillInSeriesOptions() {
var i;
var neededColors = series.length, maxIndex = 0, i;
// collect what we already got of colors
var neededColors = series.length, // Subtract the number of series that already have fixed
usedColors = [], // colors from the number we need to generate.
assignedColors = [];
for (i = 0; i < series.length; ++i) { for (i = 0; i < series.length; ++i) {
var sc = series[i].color; var sc = series[i].color;
if (sc != null) { if (sc != null) {
--neededColors; neededColors--;
if (typeof sc == "number") if (typeof sc == "number" && sc > maxIndex) {
assignedColors.push(sc); maxIndex = sc;
else }
usedColors.push($.color.parse(series[i].color));
} }
} }
// we might need to generate more colors if higher indices
// are assigned
for (i = 0; i < assignedColors.length; ++i) {
neededColors = Math.max(neededColors, assignedColors[i] + 1);
}
// produce colors as needed
var colors = [], variation = 0;
i = 0;
while (colors.length < neededColors) {
var c;
if (options.colors.length == i) // check degenerate case
c = $.color.make(100, 100, 100);
else
c = $.color.parse(options.colors[i]);
// vary color if needed // If any of the user colors are numeric indexes, then we
var sign = variation % 2 == 1 ? -1 : 1; // need to generate at least as many as the highest index.
c.scale('rgb', 1 + sign * Math.ceil(variation / 2) * 0.2);
// FIXME: if we're getting to close to something else, if (maxIndex > neededColors) {
// we should probably skip this one neededColors = maxIndex + 1;
colors.push(c); }
++i; // Generate the needed colors, based on the option colors
if (i >= options.colors.length) {
i = 0; var c, colors = [], colorPool = options.colors,
++variation; colorPoolSize = colorPool.length, variation = 0;
for (i = 0; i < neededColors; i++) {
c = $.color.parse(colorPool[i % colorPoolSize] || "#666");
// Each time we exhaust the colors in the pool we adjust
// a scaling factor used to produce more variations on
// those colors. The factor alternates negative/positive
// to produce lighter/darker colors.
// Reset the variation after every few cycles, or else
// it will end up producing only white or black colors.
if (i % colorPoolSize == 0 && i) {
if (variation >= 0) {
if (variation < 0.5) {
variation = -variation - 0.2;
} else variation = 0;
} else variation = -variation;
} }
colors[i] = c.scale('rgb', 1 + variation);
} }
// fill in the options // Finalize the series options, filling in their colors
var colori = 0, s; var colori = 0, s;
for (i = 0; i < series.length; ++i) { for (i = 0; i < series.length; ++i) {
s = series[i]; s = series[i];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment