PolarAxisPointer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 formatUtil = require("../../util/format");
  20. var BaseAxisPointer = require("./BaseAxisPointer");
  21. var graphic = require("../../util/graphic");
  22. var viewHelper = require("./viewHelper");
  23. var matrix = require("zrender/lib/core/matrix");
  24. var AxisBuilder = require("../axis/AxisBuilder");
  25. var AxisView = require("../axis/AxisView");
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  44. var PolarAxisPointer = BaseAxisPointer.extend({
  45. /**
  46. * @override
  47. */
  48. makeElOption: function (elOption, value, axisModel, axisPointerModel, api) {
  49. var axis = axisModel.axis;
  50. if (axis.dim === 'angle') {
  51. this.animationThreshold = Math.PI / 18;
  52. }
  53. var polar = axis.polar;
  54. var otherAxis = polar.getOtherAxis(axis);
  55. var otherExtent = otherAxis.getExtent();
  56. var coordValue;
  57. coordValue = axis['dataTo' + formatUtil.capitalFirst(axis.dim)](value);
  58. var axisPointerType = axisPointerModel.get('type');
  59. if (axisPointerType && axisPointerType !== 'none') {
  60. var elStyle = viewHelper.buildElStyle(axisPointerModel);
  61. var pointerOption = pointerShapeBuilder[axisPointerType](axis, polar, coordValue, otherExtent, elStyle);
  62. pointerOption.style = elStyle;
  63. elOption.graphicKey = pointerOption.type;
  64. elOption.pointer = pointerOption;
  65. }
  66. var labelMargin = axisPointerModel.get('label.margin');
  67. var labelPos = getLabelPosition(value, axisModel, axisPointerModel, polar, labelMargin);
  68. viewHelper.buildLabelElOption(elOption, axisModel, axisPointerModel, api, labelPos);
  69. } // Do not support handle, utill any user requires it.
  70. });
  71. function getLabelPosition(value, axisModel, axisPointerModel, polar, labelMargin) {
  72. var axis = axisModel.axis;
  73. var coord = axis.dataToCoord(value);
  74. var axisAngle = polar.getAngleAxis().getExtent()[0];
  75. axisAngle = axisAngle / 180 * Math.PI;
  76. var radiusExtent = polar.getRadiusAxis().getExtent();
  77. var position;
  78. var align;
  79. var verticalAlign;
  80. if (axis.dim === 'radius') {
  81. var transform = matrix.create();
  82. matrix.rotate(transform, transform, axisAngle);
  83. matrix.translate(transform, transform, [polar.cx, polar.cy]);
  84. position = graphic.applyTransform([coord, -labelMargin], transform);
  85. var labelRotation = axisModel.getModel('axisLabel').get('rotate') || 0;
  86. var labelLayout = AxisBuilder.innerTextLayout(axisAngle, labelRotation * Math.PI / 180, -1);
  87. align = labelLayout.textAlign;
  88. verticalAlign = labelLayout.textVerticalAlign;
  89. } else {
  90. // angle axis
  91. var r = radiusExtent[1];
  92. position = polar.coordToPoint([r + labelMargin, coord]);
  93. var cx = polar.cx;
  94. var cy = polar.cy;
  95. align = Math.abs(position[0] - cx) / r < 0.3 ? 'center' : position[0] > cx ? 'left' : 'right';
  96. verticalAlign = Math.abs(position[1] - cy) / r < 0.3 ? 'middle' : position[1] > cy ? 'top' : 'bottom';
  97. }
  98. return {
  99. position: position,
  100. align: align,
  101. verticalAlign: verticalAlign
  102. };
  103. }
  104. var pointerShapeBuilder = {
  105. line: function (axis, polar, coordValue, otherExtent, elStyle) {
  106. return axis.dim === 'angle' ? {
  107. type: 'Line',
  108. shape: viewHelper.makeLineShape(polar.coordToPoint([otherExtent[0], coordValue]), polar.coordToPoint([otherExtent[1], coordValue]))
  109. } : {
  110. type: 'Circle',
  111. shape: {
  112. cx: polar.cx,
  113. cy: polar.cy,
  114. r: coordValue
  115. }
  116. };
  117. },
  118. shadow: function (axis, polar, coordValue, otherExtent, elStyle) {
  119. var bandWidth = Math.max(1, axis.getBandWidth());
  120. var radian = Math.PI / 180;
  121. return axis.dim === 'angle' ? {
  122. type: 'Sector',
  123. shape: viewHelper.makeSectorShape(polar.cx, polar.cy, otherExtent[0], otherExtent[1], // In ECharts y is negative if angle is positive
  124. (-coordValue - bandWidth / 2) * radian, (-coordValue + bandWidth / 2) * radian)
  125. } : {
  126. type: 'Sector',
  127. shape: viewHelper.makeSectorShape(polar.cx, polar.cy, coordValue - bandWidth / 2, coordValue + bandWidth / 2, 0, Math.PI * 2)
  128. };
  129. }
  130. };
  131. AxisView.registerAxisPointerClass('PolarAxisPointer', PolarAxisPointer);
  132. var _default = PolarAxisPointer;
  133. module.exports = _default;