Skip to content

Commit

Permalink
Color palette enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Jun 2, 2016
1 parent 802b34e commit 76922b9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/echarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ define(function (require) {
* @private
*/
function doVisualCoding(ecModel, payload) {
ecModel.clearColorPalette();
ecModel.eachSeries(function (seriesModel) {
seriesModel.clearColorPalette();
});
each(VISUAL_CODING_STAGES, function (stage) {
each(visualCodingFuncs[stage] || [], function (visualCoding) {
visualCoding(ecModel, payload);
Expand Down
2 changes: 2 additions & 0 deletions src/model/Global.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,5 +748,7 @@ define(function (require) {
}
}

zrUtil.mixin(GlobalModel, require('./mixin/colorPalette'));

return GlobalModel;
});
12 changes: 12 additions & 0 deletions src/model/Series.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define(function(require) {
var formatUtil = require('../util/format');
var modelUtil = require('../util/model');
var ComponentModel = require('./Component');
var colorPaletteMixin = require('./mixin/colorPalette');

var encodeHTML = formatUtil.encodeHTML;
var addCommas = formatUtil.addCommas;
Expand Down Expand Up @@ -219,10 +220,21 @@ define(function(require) {
this._data = this._dataBeforeProcessed.cloneShallow();
},

getColorFromPalette: function (name, scope) {
var ecModel = this.ecModel;
// PENDING
var color = colorPaletteMixin.getColorFromPalette.call(this, name, scope);
if (!color) {
color = ecModel.getColorFromPalette(name, scope);
}
return color;
},

getAxisTooltipDataIndex: null
});

zrUtil.mixin(SeriesModel, modelUtil.dataFormatMixin);
zrUtil.mixin(SeriesModel, colorPaletteMixin);

return SeriesModel;
});
29 changes: 29 additions & 0 deletions src/model/mixin/colorPalette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define(function () {
return {
clearColorPalette: function () {
this._colorIdx = 0;
this._colorNameMap = {};
},

getColorFromPalette: function (name, scope) {
scope = scope || this;
var colorIdx = scope._colorIdx || 0;
var colorNameMap = scope._colorNameMap || (scope._colorNameMap = {});
if (colorNameMap[name]) {
return colorNameMap[name];
}
var colorPalette = this.get('color', true) || [];
if (!colorPalette.length) {
return;
}

var color = colorPalette[colorIdx];
if (name) {
colorNameMap[name] = color;
}
scope._colorIdx = (colorIdx + 1) % colorPalette.length;

return color;
}
};
});
24 changes: 14 additions & 10 deletions src/visual/dataColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@
define(function (require) {

return function (seriesType, ecModel) {
var globalColorList = ecModel.get('color');
var offset = 0;
// Pie and funnel may use diferrent scope
var paletteScope = {};
ecModel.eachRawSeriesByType(seriesType, function (seriesModel) {
var colorList = seriesModel.get('color', true);
var dataAll = seriesModel.getRawData();
var idxMap = {};
if (!ecModel.isSeriesFiltered(seriesModel)) {
var data = seriesModel.getData();
data.each(function (idx) {
var itemModel = data.getItemModel(idx);
var rawIdx = data.getRawIndex(idx);
idxMap[rawIdx] = idx;
});
dataAll.each(function (rawIdx) {
// FIXME Performance
var itemModel = dataAll.getItemModel(rawIdx);
var filteredIdx = idxMap[rawIdx];
// If series.itemStyle.normal.color is a function. itemVisual may be encoded
var singleDataColor = data.getItemVisual(idx, 'color', true);
var singleDataColor = data.getItemVisual(filteredIdx, 'color', true);

if (!singleDataColor) {
var paletteColor = colorList ? colorList[rawIdx % colorList.length]
: globalColorList[(rawIdx + offset) % globalColorList.length];
var color = itemModel.get('itemStyle.normal.color') || paletteColor;
var color = itemModel.get('itemStyle.normal.color')
|| seriesModel.getColorFromPalette(dataAll.getName(rawIdx), paletteScope);
// Legend may use the visual info in data before processed
dataAll.setItemVisual(rawIdx, 'color', color);
data.setItemVisual(idx, 'color', color);
data.setItemVisual(filteredIdx, 'color', color);
}
else {
// Set data all color for legend
dataAll.setItemVisual(rawIdx, 'color', singleDataColor);
}
});
}
offset += dataAll.count();
});
};
});
7 changes: 3 additions & 4 deletions src/visual/seriesColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ define(function (require) {
return function (seriesType, styleType, ecModel) {
function encodeColor(seriesModel) {
var colorAccessPath = [styleType, 'normal', 'color'];
var colorList = ecModel.get('color');
var data = seriesModel.getData();
var color = seriesModel.get(colorAccessPath) // Set in itemStyle
|| colorList[seriesModel.seriesIndex % colorList.length]; // Default color
|| seriesModel.getColorFromPalette(seriesModel.get('name')); // Default color

// FIXME Set color function or use the platte color
data.setVisual('color', color);
Expand All @@ -30,7 +29,7 @@ define(function (require) {
});
}
}
seriesType ? ecModel.eachSeriesByType(seriesType, encodeColor)
: ecModel.eachSeries(encodeColor);
seriesType ? ecModel.eachRawSeriesByType(seriesType, encodeColor)
: ecModel.eachRawSeries(encodeColor);
};
});

0 comments on commit 76922b9

Please sign in to comment.