提交 e8b724a3 编写于 作者: L lang

Map data statistic

上级 c7c9d570
define(function (require) { define(function (require) {
// var SeriesModel = require('../../model/Series');
var List = require('../../data/List'); var List = require('../../data/List');
return require('../../echarts').extendSeriesModel({ var echarts = require('../../echarts');
function fillData(dataOpt, geoJson) {
var dataNameMap = {};
var features = geoJson.features;
for (var i = 0; i < dataOpt.length; i++) {
dataNameMap[dataOpt[i].name] = dataOpt[i];
}
for (var i = 0; i < features.length; i++) {
var name = features[i].properties.name;
if (!dataNameMap[name]) {
dataOpt.push({
value: NaN,
name: name
});
}
}
return dataOpt;
}
return echarts.extendSeriesModel({
type: 'series.map', type: 'series.map',
...@@ -13,7 +32,20 @@ define(function (require) { ...@@ -13,7 +32,20 @@ define(function (require) {
name: 'value' name: 'value'
}], this); }], this);
list.initData(option.data); var geoJson = echarts.getMap(option.mapType);
var dataOpt = option.data || [];
// https://jsperf.com/try-catch-performance-overhead
if (geoJson) {
try {
dataOpt = fillData(dataOpt, geoJson);
}
catch (e) {
throw 'Invalid geoJson format\n' + e;
}
}
list.initData(dataOpt);
return list; return list;
}, },
......
...@@ -2,6 +2,8 @@ define(function (require) { ...@@ -2,6 +2,8 @@ define(function (require) {
var zrUtil = require('zrender/core/util'); var zrUtil = require('zrender/core/util');
var List = require('../../data/List');
// FIXME 公用? // FIXME 公用?
/** /**
* @param {Array.<module:echarts/data/List>} datas * @param {Array.<module:echarts/data/List>} datas
...@@ -9,15 +11,30 @@ define(function (require) { ...@@ -9,15 +11,30 @@ define(function (require) {
* @inner * @inner
*/ */
function dataStatistics(datas, statisticsType) { function dataStatistics(datas, statisticsType) {
var len = datas.length; var dataNameMap = {};
return datas[0].map(['value'], function (value, idx) { var dims = ['value'];
for (var i = 1; i < len; i++) {
value += datas[i].get('value', idx); for (var i = 0; i < datas.length; i++) {
datas[i].each(dims, function (value, idx) {
var name = datas[i].getName(idx);
dataNameMap[name] = dataNameMap[name] || [];
if (!isNaN(value)) {
dataNameMap[name].push(value);
}
});
}
return datas[0].map(dims, function (value, idx) {
var name = datas[0].getName(idx);
var sum = 0;
var len = dataNameMap[name].length;
for (var i = 0; i < len; i++) {
sum += dataNameMap[name][i];
} }
if (statisticsType === 'average') { if (statisticsType === 'average') {
value /= len; sum /= len;
} }
return value; return sum;
}); });
} }
......
...@@ -12,7 +12,13 @@ define(function (require) { ...@@ -12,7 +12,13 @@ define(function (require) {
this.name = name; this.name = name;
this.regions = geoJson ? parseGeoJson(geoJson) : []; // https://jsperf.com/try-catch-performance-overhead
try {
this.regions = geoJson ? parseGeoJson(geoJson) : [];
}
catch (e) {
throw 'Invalid geoJson format\n' + e;
}
/** /**
* @param {Array.<string>} * @param {Array.<string>}
......
...@@ -5,7 +5,6 @@ define(function (require) { ...@@ -5,7 +5,6 @@ define(function (require) {
var Geo = require('./Geo'); var Geo = require('./Geo');
var numberUtil = require('../../util/number'); var numberUtil = require('../../util/number');
var zrUtil = require('zrender/core/util');
var mapDataStores = {}; var mapDataStores = {};
...@@ -99,15 +98,30 @@ define(function (require) { ...@@ -99,15 +98,30 @@ define(function (require) {
return geoList; return geoList;
}, },
/**
* @param {string} mapName
* @param {Object} geoJson
*/
registerMap: function (mapName, geoJson) { registerMap: function (mapName, geoJson) {
mapDataStores[mapName] = geoJson; mapDataStores[mapName] = geoJson;
},
/**
* @param {string} mapName
* @return {Object}
*/
getMap: function (mapName) {
return mapDataStores[mapName];
} }
}; };
// Inject methods into echarts // Inject methods into echarts
var echarts = require('../../echarts'); var echarts = require('../../echarts');
echarts.registerMap = geoCreator.registerMap; echarts.registerMap = geoCreator.registerMap;
echarts.getMap = geoCreator.getMap;
// TODO // TODO
echarts.loadMap = function () { echarts.loadMap = function () {
......
...@@ -657,20 +657,27 @@ define(function (require) { ...@@ -657,20 +657,27 @@ define(function (require) {
* It will create a temporary model if value on idx is not an option. * It will create a temporary model if value on idx is not an option.
* *
* @param {number} idx * @param {number} idx
* @param {boolean} [createNew=false]
*/ */
// FIXME Model proxy ? // FIXME Model proxy ?
listProto.getItemModel = function (idx) { listProto.getItemModel = function (idx, createNew) {
var storage = this._storage; var storage = this._storage;
var optionModelIndices = storage.$optionModelIndices; var optionModelIndices = storage.$optionModelIndices;
var modelIndex = optionModelIndices && optionModelIndices[this.indices[idx]]; var modelIndex = optionModelIndices && optionModelIndices[this.indices[idx]];
var model = this._optionModels[modelIndex]; var model = this._optionModels[modelIndex];
var hostModel = this.hostModel;
if (!model) { if (!model) {
// Use a temporary model proxy if value on idx is not an option. // Use a temporary model proxy if value on idx is not an option.
// FIXME Create a new one may cause memory leak // FIXME Create a new one may cause memory leak
model = temporaryModel; if (createNew) {
model.parentModel = this.hostModel; model = new Model(null, hostModel);
}
else {
model = temporaryModel;
model.parentModel = hostModel;
}
} }
return model; return model;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册