提交 e8b724a3 编写于 作者: L lang

Map data statistic

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