提交 d17731aa 编写于 作者: 1 100pah

[geo] enable convertToPixel for geo.

上级 505063f5
......@@ -2,6 +2,32 @@ define(function(require) {
'use strict';
var zrUtil = require('zrender/core/util');
/**
* Coordinate Interface
*
* create:
* @param {module:echarts/model/Global} ecModel
* @param {module:echarts/ExtensionAPI} api
* @return {Object} coordinate system instance
*
* update:
* @param {module:echarts/model/Global} ecModel
* @param {module:echarts/ExtensionAPI} api
*
* isTargetCoordinate:
* @param {module:echarts/model/Global} ecModel
* @param {Object} finder key: mainType, value: component model
* e.g., {geo: geoModel, series: seriesModel}
* @return {boolean}
*
* convertToPixel:
* @param {module:echarts/model/Global} ecModel
* @param {Array|number} value
* @return {Array|number} convert result.
*/
var coordinateSystemCreators = {};
function CoordinateSystemManager() {
......@@ -15,21 +41,49 @@ define(function(require) {
create: function (ecModel, api) {
var coordinateSystems = [];
for (var type in coordinateSystemCreators) {
if (coordinateSystemCreators.hasOwnProperty(type)) {
var list = coordinateSystemCreators[type].create(ecModel, api);
list && (coordinateSystems = coordinateSystems.concat(list));
}
}
zrUtil.each(coordinateSystemCreators, function (creater, type) {
var list = creater.create(ecModel, api);
coordinateSystems = coordinateSystems.concat(list || []);
});
this._coordinateSystems = coordinateSystems;
},
update: function (ecModel, api) {
var coordinateSystems = this._coordinateSystems;
for (var i = 0; i < coordinateSystems.length; i++) {
zrUtil.each(this._coordinateSystems, function (coordSys) {
// FIXME MUST have
coordinateSystems[i].update && coordinateSystems[i].update(ecModel, api);
coordSys.update && coordSys.update(ecModel, api);
});
},
/**
* Convert from coordinate system to pixel.
* @param {string|Object} finder
* If string, e.g., 'geo', means {geoIndex: 0}.
* If Object, could contain some of these properties below:
* {
* seriesIndex, seriesId,
* geoIndex, geoId,
* bmapIndex, bmapId,
* xAxisIndex, xAxisId,
* yAxisIndex, yAxisId,
* gridIndex, gridId,
* ... (can be extended)
* }
* @param {Array|number} value
*/
convertToPixel: function (ecModel, finder, value) {
finder = parseFinder(ecModel, finder);
var coordSysList = this._coordinateSystems;
for (var i = 0; i < coordSysList.length; i++) {
var coordSys = coordSysList[i];
if (coordSys.isTargetCoordinateSystem
&& coordSys.isTargetCoordinateSystem(ecModel, finder)
&& coordSys.convertToPixel
) {
return coordSys.convertToPixel(ecModel, value);
}
}
}
};
......@@ -42,5 +96,24 @@ define(function(require) {
return coordinateSystemCreators[type];
};
function parseFinder(ecModel, finder) {
if (zrUtil.isString(finder)) {
var obj = {};
obj[finder + 'Index'] = 0;
finder = obj;
}
var result = {};
zrUtil.each(finder, function (value, key) {
key = key.match(/^(\w+)(Index|Id|Name)$/);
var queryParam = {mainType: key[1]};
queryParam[key[2].toLowerCase()] = value;
result[key[1]] = ecModel.queryComponents(queryParam)[0];
});
return result;
}
return CoordinateSystemManager;
});
\ No newline at end of file
......@@ -406,11 +406,7 @@ define(function(require, factory) {
*/
function findAxesModels(seriesModel, ecModel) {
return zrUtil.map(axesTypes, function (axisType) {
var axisModel = ecModel.queryComponents({
mainType: axisType,
index: seriesModel.get(axisType + 'Index'),
id: seriesModel.get(axisType + 'Id')
})[0];
var axisModel = seriesModel.getReferringComponents(axisType)[0];
if (__DEV__) {
if (!axisModel) {
......
......@@ -221,7 +221,30 @@ define(function (require) {
if (data) {
return View.prototype.dataToPoint.call(this, data);
}
},
/**
* @implements
* see {module:echarts/CoodinateSystem}
*/
isTargetCoordinateSystem: function (ecModel, finder) {
var geoModel = finder.geo
? finder.geo
: finder.series
? finder.series.getReferringComponents('geo')[0]
: null;
return geoModel && geoModel.coordinateSystem === this;
},
/**
* @implements
* see {module:echarts/CoodinateSystem}
*/
convertToPixel: function (ecModel, value) {
return this.dataToPoint(value);
}
};
zrUtil.mixin(Geo, View);
......
......@@ -417,6 +417,16 @@ define(function (require) {
}
};
/**
* Convert from coordinate system to pixel.
* See CoordinateSystem#convertToPixel.
* @param {string|Object} finder
* @param {Array|number} value
*/
echartsProto.convertToPixel = function (finder, value) {
return this._coordSysMgr.convertToPixel(this._model, finder, value);
};
var updateMethods = {
......
......@@ -140,6 +140,14 @@ define(function(require) {
this.__defaultOption = defaultOption;
}
return this.__defaultOption;
},
getReferringComponents: function (mainType) {
return this.ecModel.queryComponents({
mainType: mainType,
index: this.get(mainType + 'Index', true),
id: this.get(mainType + 'Id', true)
});
}
});
......
......@@ -262,9 +262,9 @@ define(function (require) {
* @param {Object} condition
* @param {string} condition.mainType
* @param {string} [condition.subType] If ignore, only query by mainType
* @param {number} [condition.index] Either input index or id or name.
* @param {string} [condition.id] Either input index or id or name.
* @param {string} [condition.name] Either input index or id or name.
* @param {number|Array.<number>} [condition.index] Either input index or id or name.
* @param {string|Array.<string>} [condition.id] Either input index or id or name.
* @param {string|Array.<string>} [condition.name] Either input index or id or name.
* @return {Array.<module:echarts/model/Component>}
*/
queryComponents: function (condition) {
......
......@@ -135,6 +135,14 @@
for (var i = 0; i < context.chartCount || 0; i++) {
var el = document.createElement('div');
document.body.appendChild(el);
el.style.cssText = [
'width:200px',
'height:150px',
'position:absolute',
'bottom:0',
'right:0',
'visibility:hidden'
].join(';');
els.push(el);
charts.push(echarts.init(el, null, {renderer: 'canvas'}));
}
......
describe('dataZoom/helper', function() {
var utHelper = window.utHelper;
var helper;
beforeAll(function (done) { // jshint ignore:line
utHelper.resetPackageLoader(function () {
window.require(['echarts/component/dataZoom/helper'], function (h) {
helper = h;
done();
});
});
});
function makeRecords(result) {
var o = {};
helper.eachAxisDim(function (dimNames) {
o[dimNames.name] = {};
var r = result[dimNames.name] || [];
for (var i = 0; i < r.length; i++) {
o[dimNames.name][r[i]] = true;
}
});
return o;
}
describe('findLinkedNodes', function () {
function forEachModel(models, callback) {
for (var i = 0; i < models.length; i++) {
callback(models[i]);
}
}
function axisIndicesGetter(model, dimNames) {
return model[dimNames.axisIndex];
}
it('findLinkedNodes_base', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [1]},
{xAxisIndex: [5], yAxisIndex: []},
{xAxisIndex: [2, 5], yAxisIndex: []}
];
var result = helper.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
helper.eachAxisDim,
axisIndicesGetter
)(models[0]);
expect(result).toEqual({
nodes: [models[0], models[3], models[2]],
records: makeRecords({x: [1, 2, 5], y: [0]})
});
done();
});
it('findLinkedNodes_crossXY', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [3, 0]},
{xAxisIndex: [6, 3], yAxisIndex: [9]},
{xAxisIndex: [5, 3], yAxisIndex: []},
{xAxisIndex: [8], yAxisIndex: [4]}
];
var result = helper.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
helper.eachAxisDim,
axisIndicesGetter
)(models[0]);
expect(result).toEqual({
nodes: [models[0], models[1], models[2], models[3]],
records: makeRecords({x: [1, 2, 3, 5, 6], y: [0, 3, 9]})
});
done();
});
it('findLinkedNodes_emptySourceModel', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [3, 0]},
{xAxisIndex: [6, 3], yAxisIndex: [9]},
{xAxisIndex: [5, 3], yAxisIndex: []},
{xAxisIndex: [8], yAxisIndex: [4]}
];
var result = helper.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
helper.eachAxisDim,
axisIndicesGetter
)();
expect(result).toEqual({
nodes: [],
records: makeRecords({x: [], y: []})
});
done();
});
});
});
\ No newline at end of file
describe('coord/converter', function() {
var utHelper = window.utHelper;
var DELTA = 1E-4;
function pointEquals(p1, p2) {
return Math.abs(p1[0] - p2[0]) < DELTA && Math.abs(p1[1] - p2[1]) < DELTA;
}
var testCase = utHelper.prepare([
'echarts/chart/map',
'echarts/chart/scatter',
'echarts/chart/graph',
'echarts/component/geo',
'echarts/component/grid'
]);
var testGeoJson1 = {
'type': 'FeatureCollection',
'features': [
{
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[
2000,
3000
],
[
5000,
3000
],
[
5000,
8000
],
[
2000,
8000
]
]
]
},
'properties': {
'name': 'Afghanistan',
'childNum': 1
}
}
]
};
var testGeoJson2 = {
'type': 'FeatureCollection',
'features': [
{
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[
200,
300
],
[
500,
300
],
[
500,
800
],
[
200,
800
]
]
]
},
'properties': {
'name': 'Afghanistan',
'childNum': 1
}
}
]
};
testCase.createChart()('geo', function () {
this.echarts.registerMap('test1', testGeoJson1);
this.echarts.registerMap('test2', testGeoJson2);
var chart = this.chart;
chart.setOption({
geo: [
{
id: 'aa',
left: 0,
right: 0,
top: 0,
bottom: 0,
map: 'test1'
},
{
id: 'bb',
left: 0,
right: 0,
top: 0,
bottom: 0,
map: 'test2'
}
],
series: [
{id: 'k1', type: 'scatter', coordinateSystem: 'geo', geoIndex: 1},
{id: 'k2', type: 'scatter', coordinateSystem: 'geo'}
]
});
var width = chart.getWidth();
var height = chart.getHeight();
expect(pointEquals(chart.convertToPixel('geo', [5000, 3000]), [width, height])).toEqual(true);
expect(pointEquals(chart.convertToPixel({geoIndex: 1}, [500, 800]), [width, 0])).toEqual(true);
expect(pointEquals(chart.convertToPixel({geoId: 'bb'}, [200, 300]), [0, height])).toEqual(true);
expect(pointEquals(chart.convertToPixel({seriesIndex: 0}, [200, 800]), [0, 0])).toEqual(true);
expect(pointEquals(chart.convertToPixel({seriesId: 'k2'}, [2000, 8000]), [0, 0])).toEqual(true);
});
});
\ No newline at end of file
......@@ -24,79 +24,6 @@ describe('util/model', function() {
return o;
}
describe('findLinkedNodes', function () {
function forEachModel(models, callback) {
for (var i = 0; i < models.length; i++) {
callback(models[i]);
}
}
function axisIndicesGetter(model, dimNames) {
return model[dimNames.axisIndex];
}
it('findLinkedNodes_base', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [1]},
{xAxisIndex: [5], yAxisIndex: []},
{xAxisIndex: [2, 5], yAxisIndex: []}
];
var result = modelUtil.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
modelUtil.eachAxisDim,
axisIndicesGetter
)(models[0]);
expect(result).toEqual({
nodes: [models[0], models[3], models[2]],
records: makeRecords({x: [1, 2, 5], y: [0]})
});
done();
});
it('findLinkedNodes_crossXY', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [3, 0]},
{xAxisIndex: [6, 3], yAxisIndex: [9]},
{xAxisIndex: [5, 3], yAxisIndex: []},
{xAxisIndex: [8], yAxisIndex: [4]}
];
var result = modelUtil.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
modelUtil.eachAxisDim,
axisIndicesGetter
)(models[0]);
expect(result).toEqual({
nodes: [models[0], models[1], models[2], models[3]],
records: makeRecords({x: [1, 2, 3, 5, 6], y: [0, 3, 9]})
});
done();
});
it('findLinkedNodes_emptySourceModel', function (done) {
var models = [
{xAxisIndex: [1, 2], yAxisIndex: [0]},
{xAxisIndex: [3], yAxisIndex: [3, 0]},
{xAxisIndex: [6, 3], yAxisIndex: [9]},
{xAxisIndex: [5, 3], yAxisIndex: []},
{xAxisIndex: [8], yAxisIndex: [4]}
];
var result = modelUtil.createLinkedNodesFinder(
utHelper.curry(forEachModel, models),
modelUtil.eachAxisDim,
axisIndicesGetter
)();
expect(result).toEqual({
nodes: [],
records: makeRecords({x: [], y: []})
});
done();
});
});
describe('compressBatches', function () {
function item(seriesId, dataIndex) {
......
......@@ -7,7 +7,9 @@ document.write('<script src="spec/util/number.js"><\/script>');
document.write('<script src="spec/model/Component.js"><\/script>');
document.write('<script src="spec/model/Global.js"><\/script>');
document.write('<script src="spec/model/timelineOptions.js"><\/script>');
document.write('<script src="spec/coord/converter.js"><\/script>');
document.write('<script src="spec/data/List.js"><\/script>');
document.write('<script src="spec/component/visualMap/setOption.js"><\/script>');
document.write('<script src="spec/component/dataZoom/helper.js"><\/script>');
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册