Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
38 changes: 14 additions & 24 deletions src/chart-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand All @@ -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;
},
Expand Down
25 changes: 6 additions & 19 deletions src/chart-tristate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},

Expand All @@ -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');
Expand Down