backwardCompat.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Compatitable with 2.0
  20. import {each, isArray, isObject} from 'zrender/src/core/util';
  21. import compatStyle from './helper/compatStyle';
  22. import {normalizeToArray} from '../util/model';
  23. function get(opt, path) {
  24. path = path.split(',');
  25. var obj = opt;
  26. for (var i = 0; i < path.length; i++) {
  27. obj = obj && obj[path[i]];
  28. if (obj == null) {
  29. break;
  30. }
  31. }
  32. return obj;
  33. }
  34. function set(opt, path, val, overwrite) {
  35. path = path.split(',');
  36. var obj = opt;
  37. var key;
  38. for (var i = 0; i < path.length - 1; i++) {
  39. key = path[i];
  40. if (obj[key] == null) {
  41. obj[key] = {};
  42. }
  43. obj = obj[key];
  44. }
  45. if (overwrite || obj[path[i]] == null) {
  46. obj[path[i]] = val;
  47. }
  48. }
  49. function compatLayoutProperties(option) {
  50. each(LAYOUT_PROPERTIES, function (prop) {
  51. if (prop[0] in option && !(prop[1] in option)) {
  52. option[prop[1]] = option[prop[0]];
  53. }
  54. });
  55. }
  56. var LAYOUT_PROPERTIES = [
  57. ['x', 'left'], ['y', 'top'], ['x2', 'right'], ['y2', 'bottom']
  58. ];
  59. var COMPATITABLE_COMPONENTS = [
  60. 'grid', 'geo', 'parallel', 'legend', 'toolbox', 'title', 'visualMap', 'dataZoom', 'timeline'
  61. ];
  62. export default function (option, isTheme) {
  63. compatStyle(option, isTheme);
  64. // Make sure series array for model initialization.
  65. option.series = normalizeToArray(option.series);
  66. each(option.series, function (seriesOpt) {
  67. if (!isObject(seriesOpt)) {
  68. return;
  69. }
  70. var seriesType = seriesOpt.type;
  71. if (seriesType === 'line') {
  72. if (seriesOpt.clipOverflow != null) {
  73. seriesOpt.clip = seriesOpt.clipOverflow;
  74. }
  75. }
  76. else if (seriesType === 'pie' || seriesType === 'gauge') {
  77. if (seriesOpt.clockWise != null) {
  78. seriesOpt.clockwise = seriesOpt.clockWise;
  79. }
  80. }
  81. else if (seriesType === 'gauge') {
  82. var pointerColor = get(seriesOpt, 'pointer.color');
  83. pointerColor != null
  84. && set(seriesOpt, 'itemStyle.color', pointerColor);
  85. }
  86. compatLayoutProperties(seriesOpt);
  87. });
  88. // dataRange has changed to visualMap
  89. if (option.dataRange) {
  90. option.visualMap = option.dataRange;
  91. }
  92. each(COMPATITABLE_COMPONENTS, function (componentName) {
  93. var options = option[componentName];
  94. if (options) {
  95. if (!isArray(options)) {
  96. options = [options];
  97. }
  98. each(options, function (option) {
  99. compatLayoutProperties(option);
  100. });
  101. }
  102. });
  103. }