axisSplitHelper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 zrUtil = require("zrender/lib/core/util");
  20. var graphic = require("../../util/graphic");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. function rectCoordAxisBuildSplitArea(axisView, axisGroup, axisModel, gridModel) {
  40. var axis = axisModel.axis;
  41. if (axis.scale.isBlank()) {
  42. return;
  43. }
  44. var splitAreaModel = axisModel.getModel('splitArea');
  45. var areaStyleModel = splitAreaModel.getModel('areaStyle');
  46. var areaColors = areaStyleModel.get('color');
  47. var gridRect = gridModel.coordinateSystem.getRect();
  48. var ticksCoords = axis.getTicksCoords({
  49. tickModel: splitAreaModel,
  50. clamp: true
  51. });
  52. if (!ticksCoords.length) {
  53. return;
  54. } // For Making appropriate splitArea animation, the color and anid
  55. // should be corresponding to previous one if possible.
  56. var areaColorsLen = areaColors.length;
  57. var lastSplitAreaColors = axisView.__splitAreaColors;
  58. var newSplitAreaColors = zrUtil.createHashMap();
  59. var colorIndex = 0;
  60. if (lastSplitAreaColors) {
  61. for (var i = 0; i < ticksCoords.length; i++) {
  62. var cIndex = lastSplitAreaColors.get(ticksCoords[i].tickValue);
  63. if (cIndex != null) {
  64. colorIndex = (cIndex + (areaColorsLen - 1) * i) % areaColorsLen;
  65. break;
  66. }
  67. }
  68. }
  69. var prev = axis.toGlobalCoord(ticksCoords[0].coord);
  70. var areaStyle = areaStyleModel.getAreaStyle();
  71. areaColors = zrUtil.isArray(areaColors) ? areaColors : [areaColors];
  72. for (var i = 1; i < ticksCoords.length; i++) {
  73. var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
  74. var x;
  75. var y;
  76. var width;
  77. var height;
  78. if (axis.isHorizontal()) {
  79. x = prev;
  80. y = gridRect.y;
  81. width = tickCoord - x;
  82. height = gridRect.height;
  83. prev = x + width;
  84. } else {
  85. x = gridRect.x;
  86. y = prev;
  87. width = gridRect.width;
  88. height = tickCoord - y;
  89. prev = y + height;
  90. }
  91. var tickValue = ticksCoords[i - 1].tickValue;
  92. tickValue != null && newSplitAreaColors.set(tickValue, colorIndex);
  93. axisGroup.add(new graphic.Rect({
  94. anid: tickValue != null ? 'area_' + tickValue : null,
  95. shape: {
  96. x: x,
  97. y: y,
  98. width: width,
  99. height: height
  100. },
  101. style: zrUtil.defaults({
  102. fill: areaColors[colorIndex]
  103. }, areaStyle),
  104. silent: true
  105. }));
  106. colorIndex = (colorIndex + 1) % areaColorsLen;
  107. }
  108. axisView.__splitAreaColors = newSplitAreaColors;
  109. }
  110. function rectCoordAxisHandleRemove(axisView) {
  111. axisView.__splitAreaColors = null;
  112. }
  113. exports.rectCoordAxisBuildSplitArea = rectCoordAxisBuildSplitArea;
  114. exports.rectCoordAxisHandleRemove = rectCoordAxisHandleRemove;