mapDataStorage.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var _util = require("zrender/lib/core/util");
  22. var createHashMap = _util.createHashMap;
  23. var isString = _util.isString;
  24. var isArray = _util.isArray;
  25. var each = _util.each;
  26. var assert = _util.assert;
  27. var _parseSVG = require("zrender/lib/tool/parseSVG");
  28. var parseXML = _parseSVG.parseXML;
  29. /*
  30. * Licensed to the Apache Software Foundation (ASF) under one
  31. * or more contributor license agreements. See the NOTICE file
  32. * distributed with this work for additional information
  33. * regarding copyright ownership. The ASF licenses this file
  34. * to you under the Apache License, Version 2.0 (the
  35. * "License"); you may not use this file except in compliance
  36. * with the License. You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing,
  41. * software distributed under the License is distributed on an
  42. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  43. * KIND, either express or implied. See the License for the
  44. * specific language governing permissions and limitations
  45. * under the License.
  46. */
  47. var storage = createHashMap(); // For minimize the code size of common echarts package,
  48. // do not put too much logic in this module.
  49. var _default = {
  50. // The format of record: see `echarts.registerMap`.
  51. // Compatible with previous `echarts.registerMap`.
  52. registerMap: function (mapName, rawGeoJson, rawSpecialAreas) {
  53. var records;
  54. if (isArray(rawGeoJson)) {
  55. records = rawGeoJson;
  56. } else if (rawGeoJson.svg) {
  57. records = [{
  58. type: 'svg',
  59. source: rawGeoJson.svg,
  60. specialAreas: rawGeoJson.specialAreas
  61. }];
  62. } else {
  63. // Backward compatibility.
  64. if (rawGeoJson.geoJson && !rawGeoJson.features) {
  65. rawSpecialAreas = rawGeoJson.specialAreas;
  66. rawGeoJson = rawGeoJson.geoJson;
  67. }
  68. records = [{
  69. type: 'geoJSON',
  70. source: rawGeoJson,
  71. specialAreas: rawSpecialAreas
  72. }];
  73. }
  74. each(records, function (record) {
  75. var type = record.type;
  76. type === 'geoJson' && (type = record.type = 'geoJSON');
  77. var parse = parsers[type];
  78. parse(record);
  79. });
  80. return storage.set(mapName, records);
  81. },
  82. retrieveMap: function (mapName) {
  83. return storage.get(mapName);
  84. }
  85. };
  86. var parsers = {
  87. geoJSON: function (record) {
  88. var source = record.source;
  89. record.geoJSON = !isString(source) ? source : typeof JSON !== 'undefined' && JSON.parse ? JSON.parse(source) : new Function('return (' + source + ');')();
  90. },
  91. // Only perform parse to XML object here, which might be time
  92. // consiming for large SVG.
  93. // Although convert XML to zrender element is also time consiming,
  94. // if we do it here, the clone of zrender elements has to be
  95. // required. So we do it once for each geo instance, util real
  96. // performance issues call for optimizing it.
  97. svg: function (record) {
  98. record.svgXML = parseXML(record.source);
  99. }
  100. };
  101. module.exports = _default;