123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- var _config = require("../config");
- var __DEV__ = _config.__DEV__;
- var echarts = require("../echarts");
- var zrUtil = require("zrender/lib/core/util");
- var modelUtil = require("../util/model");
- var graphicUtil = require("../util/graphic");
- var layoutUtil = require("../util/layout");
- var _number = require("../util/number");
- var parsePercent = _number.parsePercent;
- var _nonShapeGraphicElements = {
-
- path: null,
- compoundPath: null,
-
- group: graphicUtil.Group,
- image: graphicUtil.Image,
- text: graphicUtil.Text
- };
- echarts.registerPreprocessor(function (option) {
- var graphicOption = option.graphic;
-
-
-
-
-
- if (zrUtil.isArray(graphicOption)) {
- if (!graphicOption[0] || !graphicOption[0].elements) {
- option.graphic = [{
- elements: graphicOption
- }];
- } else {
-
-
- option.graphic = [option.graphic[0]];
- }
- } else if (graphicOption && !graphicOption.elements) {
- option.graphic = [{
- elements: [graphicOption]
- }];
- }
- });
- var GraphicModel = echarts.extendComponentModel({
- type: 'graphic',
- defaultOption: {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- elements: [],
- parentId: null
- },
-
- _elOptionsToUpdate: null,
-
- mergeOption: function (option) {
-
- var elements = this.option.elements;
- this.option.elements = null;
- GraphicModel.superApply(this, 'mergeOption', arguments);
- this.option.elements = elements;
- },
-
- optionUpdated: function (newOption, isInit) {
- var thisOption = this.option;
- var newList = (isInit ? thisOption : newOption).elements;
- var existList = thisOption.elements = isInit ? [] : thisOption.elements;
- var flattenedList = [];
- this._flatten(newList, flattenedList);
- var mappingResult = modelUtil.mappingToExists(existList, flattenedList);
- modelUtil.makeIdAndName(mappingResult);
- var elOptionsToUpdate = this._elOptionsToUpdate = [];
- zrUtil.each(mappingResult, function (resultItem, index) {
- var newElOption = resultItem.option;
- if (!newElOption) {
- return;
- }
- elOptionsToUpdate.push(newElOption);
- setKeyInfoToNewElOption(resultItem, newElOption);
- mergeNewElOptionToExist(existList, index, newElOption);
- setLayoutInfoToExist(existList[index], newElOption);
- }, this);
- for (var i = existList.length - 1; i >= 0; i--) {
- if (existList[i] == null) {
- existList.splice(i, 1);
- } else {
-
-
- delete existList[i].$action;
- }
- }
- },
-
- _flatten: function (optionList, result, parentOption) {
- zrUtil.each(optionList, function (option) {
- if (!option) {
- return;
- }
- if (parentOption) {
- option.parentOption = parentOption;
- }
- result.push(option);
- var children = option.children;
- if (option.type === 'group' && children) {
- this._flatten(children, result, option);
- }
- delete option.children;
- }, this);
- },
-
-
- useElOptionsToUpdate: function () {
- var els = this._elOptionsToUpdate;
- this._elOptionsToUpdate = null;
- return els;
- }
- });
- echarts.extendComponentView({
- type: 'graphic',
-
- init: function (ecModel, api) {
-
- this._elMap = zrUtil.createHashMap();
-
- this._lastGraphicModel;
- },
-
- render: function (graphicModel, ecModel, api) {
-
-
-
-
-
-
-
-
-
- if (graphicModel !== this._lastGraphicModel) {
- this._clear();
- }
- this._lastGraphicModel = graphicModel;
- this._updateElements(graphicModel);
- this._relocate(graphicModel, api);
- },
-
- _updateElements: function (graphicModel) {
- var elOptionsToUpdate = graphicModel.useElOptionsToUpdate();
- if (!elOptionsToUpdate) {
- return;
- }
- var elMap = this._elMap;
- var rootGroup = this.group;
- zrUtil.each(elOptionsToUpdate, function (elOption) {
- var $action = elOption.$action;
- var id = elOption.id;
- var existEl = elMap.get(id);
- var parentId = elOption.parentId;
- var targetElParent = parentId != null ? elMap.get(parentId) : rootGroup;
- var elOptionStyle = elOption.style;
- if (elOption.type === 'text' && elOptionStyle) {
-
-
- if (elOption.hv && elOption.hv[1]) {
- elOptionStyle.textVerticalAlign = elOptionStyle.textBaseline = null;
- }
-
- !elOptionStyle.hasOwnProperty('textFill') && elOptionStyle.fill && (elOptionStyle.textFill = elOptionStyle.fill);
- !elOptionStyle.hasOwnProperty('textStroke') && elOptionStyle.stroke && (elOptionStyle.textStroke = elOptionStyle.stroke);
- }
- var elOptionCleaned = getCleanedElOption(elOption);
- if (!$action || $action === 'merge') {
- existEl ? existEl.attr(elOptionCleaned) : createEl(id, targetElParent, elOptionCleaned, elMap);
- } else if ($action === 'replace') {
- removeEl(existEl, elMap);
- createEl(id, targetElParent, elOptionCleaned, elMap);
- } else if ($action === 'remove') {
- removeEl(existEl, elMap);
- }
- var el = elMap.get(id);
- if (el) {
- el.__ecGraphicWidthOption = elOption.width;
- el.__ecGraphicHeightOption = elOption.height;
- setEventData(el, graphicModel, elOption);
- }
- });
- },
-
- _relocate: function (graphicModel, api) {
- var elOptions = graphicModel.option.elements;
- var rootGroup = this.group;
- var elMap = this._elMap;
- var apiWidth = api.getWidth();
- var apiHeight = api.getHeight();
- for (var i = 0; i < elOptions.length; i++) {
- var elOption = elOptions[i];
- var el = elMap.get(elOption.id);
- if (!el || !el.isGroup) {
- continue;
- }
- var parentEl = el.parent;
- var isParentRoot = parentEl === rootGroup;
- el.__ecGraphicWidth = parsePercent(el.__ecGraphicWidthOption, isParentRoot ? apiWidth : parentEl.__ecGraphicWidth) || 0;
- el.__ecGraphicHeight = parsePercent(el.__ecGraphicHeightOption, isParentRoot ? apiHeight : parentEl.__ecGraphicHeight) || 0;
- }
- for (var i = elOptions.length - 1; i >= 0; i--) {
- var elOption = elOptions[i];
- var el = elMap.get(elOption.id);
- if (!el) {
- continue;
- }
- var parentEl = el.parent;
- var containerInfo = parentEl === rootGroup ? {
- width: apiWidth,
- height: apiHeight
- } : {
- width: parentEl.__ecGraphicWidth,
- height: parentEl.__ecGraphicHeight
- };
-
-
-
- layoutUtil.positionElement(el, elOption, containerInfo, null, {
- hv: elOption.hv,
- boundingMode: elOption.bounding
- });
- }
- },
-
- _clear: function () {
- var elMap = this._elMap;
- elMap.each(function (el) {
- removeEl(el, elMap);
- });
- this._elMap = zrUtil.createHashMap();
- },
-
- dispose: function () {
- this._clear();
- }
- });
- function createEl(id, targetElParent, elOption, elMap) {
- var graphicType = elOption.type;
- var Clz = _nonShapeGraphicElements.hasOwnProperty(graphicType)
-
- ? _nonShapeGraphicElements[graphicType] : graphicUtil.getShapeClass(graphicType);
- var el = new Clz(elOption);
- targetElParent.add(el);
- elMap.set(id, el);
- el.__ecGraphicId = id;
- }
- function removeEl(existEl, elMap) {
- var existElParent = existEl && existEl.parent;
- if (existElParent) {
- existEl.type === 'group' && existEl.traverse(function (el) {
- removeEl(el, elMap);
- });
- elMap.removeKey(existEl.__ecGraphicId);
- existElParent.remove(existEl);
- }
- }
- function getCleanedElOption(elOption) {
- elOption = zrUtil.extend({}, elOption);
- zrUtil.each(['id', 'parentId', '$action', 'hv', 'bounding'].concat(layoutUtil.LOCATION_PARAMS), function (name) {
- delete elOption[name];
- });
- return elOption;
- }
- function isSetLoc(obj, props) {
- var isSet;
- zrUtil.each(props, function (prop) {
- obj[prop] != null && obj[prop] !== 'auto' && (isSet = true);
- });
- return isSet;
- }
- function setKeyInfoToNewElOption(resultItem, newElOption) {
- var existElOption = resultItem.exist;
- newElOption.id = resultItem.keyInfo.id;
- !newElOption.type && existElOption && (newElOption.type = existElOption.type);
- if (newElOption.parentId == null) {
- var newElParentOption = newElOption.parentOption;
- if (newElParentOption) {
- newElOption.parentId = newElParentOption.id;
- } else if (existElOption) {
- newElOption.parentId = existElOption.parentId;
- }
- }
- newElOption.parentOption = null;
- }
- function mergeNewElOptionToExist(existList, index, newElOption) {
-
- var newElOptCopy = zrUtil.extend({}, newElOption);
- var existElOption = existList[index];
- var $action = newElOption.$action || 'merge';
- if ($action === 'merge') {
- if (existElOption) {
-
-
- zrUtil.merge(existElOption, newElOptCopy, true);
- layoutUtil.mergeLayoutParam(existElOption, newElOptCopy, {
- ignoreSize: true
- });
- layoutUtil.copyLayoutParams(newElOption, existElOption);
- } else {
- existList[index] = newElOptCopy;
- }
- } else if ($action === 'replace') {
- existList[index] = newElOptCopy;
- } else if ($action === 'remove') {
-
- existElOption && (existList[index] = null);
- }
- }
- function setLayoutInfoToExist(existItem, newElOption) {
- if (!existItem) {
- return;
- }
- existItem.hv = newElOption.hv = [
- isSetLoc(newElOption, ['left', 'right']),
- isSetLoc(newElOption, ['top', 'bottom'])];
- if (existItem.type === 'group') {
- existItem.width == null && (existItem.width = newElOption.width = 0);
- existItem.height == null && (existItem.height = newElOption.height = 0);
- }
- }
- function setEventData(el, graphicModel, elOption) {
- var eventData = el.eventData;
- if (!el.silent && !el.ignore && !eventData) {
- eventData = el.eventData = {
- componentType: 'graphic',
- componentIndex: graphicModel.componentIndex,
- name: el.name
- };
- }
-
- if (eventData) {
- eventData.info = el.info;
- }
- }
|