diff --git a/src/base.js b/src/base.js index f9356da..e43792f 100644 --- a/src/base.js +++ b/src/base.js @@ -201,6 +201,30 @@ } }, + /** + * Setup colorMap from options + */ + initColorMap: function() { + var colorMap = this.options.get('colorMap'); + if ($.isFunction(colorMap)) { + this.colorMapFunction = colorMap; + } else if ($.isArray(colorMap)) { + this.colorMapFunction = function(sparkline, options, index, value) { + if (index < colorMap.length) { + return colorMap[index]; + } + // else undefined + }; + } else if (colorMap) { + if (colorMap.get === undefined) { + colorMap = new RangeMap(colorMap); + } + this.colorMapFunction = function(sparkline, options, index, value) { + return colorMap.get(value); + } + } + }, + /** * Actually render the chart to the canvas */ diff --git a/src/chart-bar.js b/src/chart-bar.js index de0e376..34177e3 100644 --- a/src/chart-bar.js +++ b/src/chart-bar.js @@ -130,17 +130,7 @@ } this.yoffset = yoffset; - if ($.isArray(options.get('colorMap'))) { - this.colorMapByIndex = options.get('colorMap'); - this.colorMapByValue = null; - } else { - this.colorMapByIndex = null; - this.colorMapByValue = options.get('colorMap'); - if (this.colorMapByValue && this.colorMapByValue.get === undefined) { - this.colorMapByValue = new RangeMap(this.colorMapByValue); - } - } - + this.initColorMap(); this.range = range; }, @@ -167,22 +157,22 @@ }, calcColor: function (stacknum, value, valuenum) { - var colorMapByIndex = this.colorMapByIndex, - colorMapByValue = this.colorMapByValue, + var colorMapFunction = this.colorMapFunction, options = this.options, color, newColor; - if (this.stacked) { - color = options.get('stackedBarColor'); - } else { - color = (value < 0) ? options.get('negBarColor') : options.get('barColor'); - } - if (value === 0 && options.get('zeroColor') !== undefined) { - color = options.get('zeroColor'); - } - if (colorMapByValue && (newColor = colorMapByValue.get(value))) { + + if (colorMapFunction && (newColor = colorMapFunction(this, options, valuenum, value))) { color = newColor; - } else if (colorMapByIndex && colorMapByIndex.length > valuenum) { - color = colorMapByIndex[valuenum]; + } + else { + if (this.stacked) { + color = options.get('stackedBarColor'); + } else { + color = (value < 0) ? options.get('negBarColor') : options.get('barColor'); + } + if (value === 0 && options.get('zeroColor') !== undefined) { + color = options.get('zeroColor'); + } } return $.isArray(color) ? color[stacknum % color.length] : color; }, diff --git a/src/chart-tristate.js b/src/chart-tristate.js index 6b2771b..a997614 100644 --- a/src/chart-tristate.js +++ b/src/chart-tristate.js @@ -16,16 +16,7 @@ this.values = $.map(values, Number); this.width = width = (values.length * barWidth) + ((values.length - 1) * barSpacing); - if ($.isArray(options.get('colorMap'))) { - this.colorMapByIndex = options.get('colorMap'); - this.colorMapByValue = null; - } else { - this.colorMapByIndex = null; - this.colorMapByValue = options.get('colorMap'); - if (this.colorMapByValue && this.colorMapByValue.get === undefined) { - this.colorMapByValue = new RangeMap(this.colorMapByValue); - } - } + this.initColorMap(); this.initTarget(); }, @@ -44,19 +35,15 @@ }, calcColor: function (value, valuenum) { - var values = this.values, - options = this.options, - colorMapByIndex = this.colorMapByIndex, - colorMapByValue = this.colorMapByValue, + var options = this.options, + colorMapFunction = this.colorMapFunction, color, newColor; - if (colorMapByValue && (newColor = colorMapByValue.get(value))) { + if (colorMapFunction && (newColor = colorMapFunction(this, options, valuenum, value))) { color = newColor; - } else if (colorMapByIndex && colorMapByIndex.length > valuenum) { - color = colorMapByIndex[valuenum]; - } else if (values[valuenum] < 0) { + } else if (value < 0) { color = options.get('negBarColor'); - } else if (values[valuenum] > 0) { + } else if (value > 0) { color = options.get('posBarColor'); } else { color = options.get('zeroBarColor');