Add per-series 'color' option.
[dygraphs.git] / dygraph.js
index 48475ef..4e15985 100644 (file)
@@ -80,7 +80,7 @@ var Dygraph = function(div, data, opts, opt_fourth_param) {
 };
 
 Dygraph.NAME = "Dygraph";
-Dygraph.VERSION = "1.0.0";
+Dygraph.VERSION = "1.0.1";
 Dygraph.__repr__ = function() {
   return "[" + this.NAME + " " + this.VERSION + "]";
 };
@@ -857,7 +857,7 @@ Dygraph.prototype.toDataYCoord = function(y, axis) {
   var yRange = this.yAxisRange(axis);
 
   if (typeof(axis) == "undefined") axis = 0;
-  if (!this.axes_[axis].logscale) {
+  if (!this.attributes_.getForAxis("logscale", axis)) {
     return yRange[0] + (area.y + area.h - y) / area.h * (yRange[1] - yRange[0]);
   } else {
     // Computing the inverse of toDomCoord.
@@ -1161,14 +1161,41 @@ 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;
+  var visibility = this.visibility();
+  for (var i = 0; i < num; i++) {
+    if (!visibility[i]) {
+      continue;
+    }
+    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;
+  }
+/*
   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;
+      var customColor = this.attributes_.getForSeries('color', labels[i]);
       // alternate colors for high contrast.
       var idx = i % 2 ? Math.ceil(i / 2) : (half + i / 2);
       var hue = (1.0 * idx/ (1 + num));
@@ -1184,6 +1211,8 @@ Dygraph.prototype.setColors_ = function() {
       this.colorsMap_[labels[1 + i]] = colorStr;
     }
   }
+*/
+
 };
 
 /**
@@ -2153,29 +2182,27 @@ Dygraph.prototype.addXTicks_ = function() {
 };
 
 /**
+ * Returns the correct handler class for the currently set options.
  * @private
- * Returns the correct handler ID for the currently set options. 
- * The actual handler may then be retrieved using the
- * Dygraph.DataHandlers.getHandler() method.
- */
-Dygraph.prototype.getHandlerId_ = function() {
-  var handlerId;
-  if (this.attr_("dataHandlerId")) {
-    handlerId =  this.attr_("dataHandlerId");
-  } else if (this.fractions_){
-    if (this.attr_("errorBars")) {
-      handlerId = "bars-fractions";
+ */
+Dygraph.prototype.getHandlerClass_ = function() {
+  var handlerClass;
+  if (this.attr_('dataHandler')) {
+    handlerClass =  this.attr_('dataHandler');
+  } else if (this.fractions_) {
+    if (this.attr_('errorBars')) {
+      handlerClass = Dygraph.DataHandlers.FractionsBarsHandler;
     } else {
-      handlerId = "default-fractions";
+      handlerClass = Dygraph.DataHandlers.DefaultFractionHandler;
     }
-  } else if (this.attr_("customBars")) {
-    handlerId = "bars-custom";
-  } else if (this.attr_("errorBars")) {
-    handlerId = "bars-error";
+  } else if (this.attr_('customBars')) {
+    handlerClass = Dygraph.DataHandlers.CustomBarsHandler;
+  } else if (this.attr_('errorBars')) {
+    handlerClass = Dygraph.DataHandlers.ErrorBarsHandler;
   } else {
-    handlerId = "default";
+    handlerClass = Dygraph.DataHandlers.DefaultHandler;
   }
-  return handlerId;
+  return handlerClass;
 };
 
 /**
@@ -2190,7 +2217,7 @@ Dygraph.prototype.predraw_ = function() {
   var start = new Date();
   
   // Create the correct dataHandler
-  this.dataHandler_ = new (Dygraph.DataHandlers.getHandler(this.getHandlerId_()))();
+  this.dataHandler_ = new (this.getHandlerClass_())();
 
   this.layout_.computePlotArea();
 
@@ -3631,6 +3658,3 @@ Dygraph.addAnnotationRule = function() {
 
   this.warn("Unable to add default annotation CSS rule; display may be off.");
 };
-
-// Older pages may still use this name.
-var DateGraph = Dygraph;