Merge pull request #268 from danvk/color-option
authorDan Vanderkam <danvdk@gmail.com>
Fri, 13 Sep 2013 00:36:38 +0000 (17:36 -0700)
committerDan Vanderkam <danvdk@gmail.com>
Fri, 13 Sep 2013 00:36:38 +0000 (17:36 -0700)
Add per-series 'color' option. Fixes issue 263

auto_tests/tests/per_series.js
dygraph-options-reference.js
dygraph.js

index 1a04496..7289e0c 100644 (file)
@@ -165,3 +165,12 @@ perSeriesTestCase.prototype.testOldAxisSpecInNewSeriesThrows = function() {
 
   assertTrue(threw);
 }
+
+perSeriesTestCase.prototype.testColorOption = function() {
+  var graph = document.getElementById("graph");
+  var data = "X,A,B,C\n0,1,2,3\n";
+  var g = new Dygraph(graph, data, {});
+  assertEquals(['rgb(64,128,0)', 'rgb(64,0,128)', 'rgb(0,128,128)'], g.getColors());
+  g.updateOptions({series : { B : { color : 'purple' }}});
+  assertEquals(['rgb(64,128,0)', 'purple', 'rgb(0,128,128)'], g.getColors());
+}
index 6220750..770b531 100644 (file)
@@ -99,12 +99,19 @@ Dygraph.OPTIONS_REFERENCE =  // <JSON>
     ],
     "description": "A function to call when a data point is clicked. and the point that was clicked."
   },
+  "color": {
+    "default": "(see description)",
+    "labels": ["Data Series Colors"],
+    "type": "string",
+    "example": "red",
+    "description": "A per-series color definition. Used in conjunction with, and overrides, the colors option."
+  },
   "colors": {
     "default": "(see description)",
     "labels": ["Data Series Colors"],
     "type": "array<string>",
     "example": "['red', '#00FF00']",
-    "description": "List of colors for the data series. These can be of the form \"#AABBCC\" or \"rgb(255,100,200)\" or \"yellow\", etc. If not specified, equally-spaced points around a color wheel are used."
+    "description": "List of colors for the data series. These can be of the form \"#AABBCC\" or \"rgb(255,100,200)\" or \"yellow\", etc. If not specified, equally-spaced points around a color wheel are used. Overridden by the 'color' option."
   },
   "connectSeparatedPoints": {
     "default": "false",
index effe10d..cee63e8 100644 (file)
@@ -1161,28 +1161,32 @@ Dygraph.prototype.setColors_ = function() {
   var num = labels.length - 1;
   this.colors_ = [];
   this.colorsMap_ = {};
+
+  // These are used for when no custom colors are specified.
+  var sat = this.attr_('colorSaturation') || 1.0;
+  var val = this.attr_('colorValue') || 0.5;
+  var half = Math.ceil(num / 2);
+
   var colors = this.attr_('colors');
-  var i;
-  if (!colors) {
-    var sat = this.attr_('colorSaturation') || 1.0;
-    var val = this.attr_('colorValue') || 0.5;
-    var half = Math.ceil(num / 2);
-    for (i = 1; i <= num; i++) {
-      if (!this.visibility()[i-1]) continue;
-      // alternate colors for high contrast.
-      var idx = i % 2 ? Math.ceil(i / 2) : (half + i / 2);
-      var hue = (1.0 * idx/ (1 + num));
-      var colorStr = Dygraph.hsvToRGB(hue, sat, val);
-      this.colors_.push(colorStr);
-      this.colorsMap_[labels[i]] = colorStr;
+  var visibility = this.visibility();
+  for (var i = 0; i < num; i++) {
+    if (!visibility[i]) {
+      continue;
     }
-  } else {
-    for (i = 0; i < num; i++) {
-      if (!this.visibility()[i]) continue;
-      var colorStr = colors[i % colors.length];
-      this.colors_.push(colorStr);
-      this.colorsMap_[labels[1 + i]] = colorStr;
+    var label = labels[i + 1];
+    var colorStr = this.attributes_.getForSeries('color', label);
+    if (!colorStr) {
+      if (colors) {
+        colorStr = colors[i % colors.length];
+      } else {
+        // alternate colors for high contrast.
+        var idx = i % 2 ? (half + (i + 1)/ 2) : Math.ceil((i + 1) / 2);
+        var hue = (1.0 * idx / (1 + num));
+        colorStr = Dygraph.hsvToRGB(hue, sat, val);
+      }
     }
+    this.colors_.push(colorStr);
+    this.colorsMap_[label] = colorStr;
   }
 };