AngleAxisView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import * as graphic from '../../util/graphic.js';
  43. import { createTextStyle } from '../../label/labelStyle.js';
  44. import Model from '../../model/Model.js';
  45. import AxisView from './AxisView.js';
  46. import AxisBuilder from './AxisBuilder.js';
  47. import { getECData } from '../../util/innerStore.js';
  48. import { getTickValueOutermost } from '../../coord/axisHelper.js';
  49. var elementList = ['axisLine', 'axisLabel', 'axisTick', 'minorTick', 'splitLine', 'minorSplitLine', 'splitArea'];
  50. function getAxisLineShape(polar, rExtent, angle) {
  51. rExtent[1] > rExtent[0] && (rExtent = rExtent.slice().reverse());
  52. var start = polar.coordToPoint([rExtent[0], angle]);
  53. var end = polar.coordToPoint([rExtent[1], angle]);
  54. return {
  55. x1: start[0],
  56. y1: start[1],
  57. x2: end[0],
  58. y2: end[1]
  59. };
  60. }
  61. function getRadiusIdx(polar) {
  62. var radiusAxis = polar.getRadiusAxis();
  63. return radiusAxis.inverse ? 0 : 1;
  64. }
  65. // Remove the last tick which will overlap the first tick
  66. function fixAngleOverlap(list) {
  67. var firstItem = list[0];
  68. var lastItem = list[list.length - 1];
  69. if (firstItem && lastItem && Math.abs(Math.abs(firstItem.coord - lastItem.coord) - 360) < 1e-4) {
  70. list.pop();
  71. }
  72. }
  73. var AngleAxisView = /** @class */function (_super) {
  74. __extends(AngleAxisView, _super);
  75. function AngleAxisView() {
  76. var _this = _super !== null && _super.apply(this, arguments) || this;
  77. _this.type = AngleAxisView.type;
  78. _this.axisPointerClass = 'PolarAxisPointer';
  79. return _this;
  80. }
  81. AngleAxisView.prototype.render = function (angleAxisModel, ecModel) {
  82. this.group.removeAll();
  83. if (!angleAxisModel.get('show')) {
  84. return;
  85. }
  86. var angleAxis = angleAxisModel.axis;
  87. var polar = angleAxis.polar;
  88. var radiusExtent = polar.getRadiusAxis().getExtent();
  89. var ticksAngles = angleAxis.getTicksCoords({
  90. breakTicks: 'none'
  91. });
  92. var minorTickAngles = angleAxis.getMinorTicksCoords();
  93. var labels = [];
  94. zrUtil.each(angleAxis.getViewLabels(), function (labelItem) {
  95. if (labelItem.tick.offInterval) {
  96. return;
  97. }
  98. labelItem = zrUtil.clone(labelItem);
  99. var scale = angleAxis.scale;
  100. labelItem.coord = angleAxis.dataToCoord(getTickValueOutermost(scale, labelItem.tick));
  101. labels.push(labelItem);
  102. });
  103. fixAngleOverlap(labels);
  104. fixAngleOverlap(ticksAngles);
  105. zrUtil.each(elementList, function (name) {
  106. if (angleAxisModel.get([name, 'show']) && (!angleAxis.scale.isBlank() || name === 'axisLine')) {
  107. angelAxisElementsBuilders[name](this.group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent, labels);
  108. }
  109. }, this);
  110. };
  111. AngleAxisView.type = 'angleAxis';
  112. return AngleAxisView;
  113. }(AxisView);
  114. var angelAxisElementsBuilders = {
  115. axisLine: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
  116. var lineStyleModel = angleAxisModel.getModel(['axisLine', 'lineStyle']);
  117. var angleAxis = polar.getAngleAxis();
  118. var RADIAN = Math.PI / 180;
  119. var angleExtent = angleAxis.getExtent();
  120. // extent id of the axis radius (r0 and r)
  121. var rId = getRadiusIdx(polar);
  122. var r0Id = rId ? 0 : 1;
  123. var shape;
  124. var shapeType = Math.abs(angleExtent[1] - angleExtent[0]) === 360 ? 'Circle' : 'Arc';
  125. if (radiusExtent[r0Id] === 0) {
  126. shape = new graphic[shapeType]({
  127. shape: {
  128. cx: polar.cx,
  129. cy: polar.cy,
  130. r: radiusExtent[rId],
  131. startAngle: -angleExtent[0] * RADIAN,
  132. endAngle: -angleExtent[1] * RADIAN,
  133. clockwise: angleAxis.inverse
  134. },
  135. style: lineStyleModel.getLineStyle(),
  136. z2: 1,
  137. silent: true
  138. });
  139. } else {
  140. shape = new graphic.Ring({
  141. shape: {
  142. cx: polar.cx,
  143. cy: polar.cy,
  144. r: radiusExtent[rId],
  145. r0: radiusExtent[r0Id]
  146. },
  147. style: lineStyleModel.getLineStyle(),
  148. z2: 1,
  149. silent: true
  150. });
  151. }
  152. shape.style.fill = null;
  153. group.add(shape);
  154. },
  155. axisTick: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
  156. var tickModel = angleAxisModel.getModel('axisTick');
  157. var tickLen = (tickModel.get('inside') ? -1 : 1) * tickModel.get('length');
  158. var radius = radiusExtent[getRadiusIdx(polar)];
  159. var lines = zrUtil.map(ticksAngles, function (tickAngleItem) {
  160. return new graphic.Line({
  161. shape: getAxisLineShape(polar, [radius, radius + tickLen], tickAngleItem.coord)
  162. });
  163. });
  164. group.add(graphic.mergePath(lines, {
  165. style: zrUtil.defaults(tickModel.getModel('lineStyle').getLineStyle(), {
  166. stroke: angleAxisModel.get(['axisLine', 'lineStyle', 'color'])
  167. })
  168. }));
  169. },
  170. minorTick: function (group, angleAxisModel, polar, tickAngles, minorTickAngles, radiusExtent) {
  171. if (!minorTickAngles.length) {
  172. return;
  173. }
  174. var tickModel = angleAxisModel.getModel('axisTick');
  175. var minorTickModel = angleAxisModel.getModel('minorTick');
  176. var tickLen = (tickModel.get('inside') ? -1 : 1) * minorTickModel.get('length');
  177. var radius = radiusExtent[getRadiusIdx(polar)];
  178. var lines = [];
  179. for (var i = 0; i < minorTickAngles.length; i++) {
  180. for (var k = 0; k < minorTickAngles[i].length; k++) {
  181. lines.push(new graphic.Line({
  182. shape: getAxisLineShape(polar, [radius, radius + tickLen], minorTickAngles[i][k].coord)
  183. }));
  184. }
  185. }
  186. group.add(graphic.mergePath(lines, {
  187. style: zrUtil.defaults(minorTickModel.getModel('lineStyle').getLineStyle(), zrUtil.defaults(tickModel.getLineStyle(), {
  188. stroke: angleAxisModel.get(['axisLine', 'lineStyle', 'color'])
  189. }))
  190. }));
  191. },
  192. axisLabel: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent, labels) {
  193. var rawCategoryData = angleAxisModel.getCategories(true);
  194. var commonLabelModel = angleAxisModel.getModel('axisLabel');
  195. var labelMargin = commonLabelModel.get('margin');
  196. var triggerEvent = angleAxisModel.get('triggerEvent');
  197. // Use length of ticksAngles because it may remove the last tick to avoid overlapping
  198. zrUtil.each(labels, function (labelItem, idx) {
  199. var labelModel = commonLabelModel;
  200. var tickValue = labelItem.tick.value;
  201. var r = radiusExtent[getRadiusIdx(polar)];
  202. var p = polar.coordToPoint([r + labelMargin, labelItem.coord]);
  203. var cx = polar.cx;
  204. var cy = polar.cy;
  205. var labelTextAlign = Math.abs(p[0] - cx) / r < 0.3 ? 'center' : p[0] > cx ? 'left' : 'right';
  206. var labelTextVerticalAlign = Math.abs(p[1] - cy) / r < 0.3 ? 'middle' : p[1] > cy ? 'top' : 'bottom';
  207. if (rawCategoryData && rawCategoryData[tickValue]) {
  208. var rawCategoryItem = rawCategoryData[tickValue];
  209. if (zrUtil.isObject(rawCategoryItem) && rawCategoryItem.textStyle) {
  210. labelModel = new Model(rawCategoryItem.textStyle, commonLabelModel, commonLabelModel.ecModel);
  211. }
  212. }
  213. var textEl = new graphic.Text({
  214. silent: AxisBuilder.isLabelSilent(angleAxisModel),
  215. style: createTextStyle(labelModel, {
  216. x: p[0],
  217. y: p[1],
  218. fill: labelModel.getTextColor() || angleAxisModel.get(['axisLine', 'lineStyle', 'color']),
  219. text: labelItem.formattedLabel,
  220. align: labelTextAlign,
  221. verticalAlign: labelTextVerticalAlign
  222. })
  223. });
  224. group.add(textEl);
  225. graphic.setTooltipConfig({
  226. el: textEl,
  227. componentModel: angleAxisModel,
  228. itemName: labelItem.formattedLabel,
  229. formatterParamsExtra: {
  230. isTruncated: function () {
  231. return textEl.isTruncated;
  232. },
  233. value: labelItem.rawLabel,
  234. tickIndex: idx
  235. }
  236. });
  237. // Pack data for mouse event
  238. if (triggerEvent) {
  239. var eventData = AxisBuilder.makeAxisEventDataBase(angleAxisModel);
  240. eventData.targetType = 'axisLabel';
  241. eventData.value = labelItem.rawLabel;
  242. getECData(textEl).eventData = eventData;
  243. }
  244. }, this);
  245. },
  246. splitLine: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
  247. var splitLineModel = angleAxisModel.getModel('splitLine');
  248. var lineStyleModel = splitLineModel.getModel('lineStyle');
  249. var lineColors = lineStyleModel.get('color');
  250. var lineCount = 0;
  251. lineColors = lineColors instanceof Array ? lineColors : [lineColors];
  252. var splitLines = [];
  253. for (var i = 0; i < ticksAngles.length; i++) {
  254. var colorIndex = lineCount++ % lineColors.length;
  255. splitLines[colorIndex] = splitLines[colorIndex] || [];
  256. splitLines[colorIndex].push(new graphic.Line({
  257. shape: getAxisLineShape(polar, radiusExtent, ticksAngles[i].coord)
  258. }));
  259. }
  260. // Simple optimization
  261. // Batching the lines if color are the same
  262. for (var i = 0; i < splitLines.length; i++) {
  263. group.add(graphic.mergePath(splitLines[i], {
  264. style: zrUtil.defaults({
  265. stroke: lineColors[i % lineColors.length]
  266. }, lineStyleModel.getLineStyle()),
  267. silent: true,
  268. z: angleAxisModel.get('z')
  269. }));
  270. }
  271. },
  272. minorSplitLine: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
  273. if (!minorTickAngles.length) {
  274. return;
  275. }
  276. var minorSplitLineModel = angleAxisModel.getModel('minorSplitLine');
  277. var lineStyleModel = minorSplitLineModel.getModel('lineStyle');
  278. var lines = [];
  279. for (var i = 0; i < minorTickAngles.length; i++) {
  280. for (var k = 0; k < minorTickAngles[i].length; k++) {
  281. lines.push(new graphic.Line({
  282. shape: getAxisLineShape(polar, radiusExtent, minorTickAngles[i][k].coord)
  283. }));
  284. }
  285. }
  286. group.add(graphic.mergePath(lines, {
  287. style: lineStyleModel.getLineStyle(),
  288. silent: true,
  289. z: angleAxisModel.get('z')
  290. }));
  291. },
  292. splitArea: function (group, angleAxisModel, polar, ticksAngles, minorTickAngles, radiusExtent) {
  293. if (!ticksAngles.length) {
  294. return;
  295. }
  296. var splitAreaModel = angleAxisModel.getModel('splitArea');
  297. var areaStyleModel = splitAreaModel.getModel('areaStyle');
  298. var areaColors = areaStyleModel.get('color');
  299. var lineCount = 0;
  300. areaColors = areaColors instanceof Array ? areaColors : [areaColors];
  301. var splitAreas = [];
  302. var RADIAN = Math.PI / 180;
  303. var prevAngle = -ticksAngles[0].coord * RADIAN;
  304. var r0 = Math.min(radiusExtent[0], radiusExtent[1]);
  305. var r1 = Math.max(radiusExtent[0], radiusExtent[1]);
  306. var clockwise = angleAxisModel.get('clockwise');
  307. for (var i = 1, len = ticksAngles.length; i <= len; i++) {
  308. var coord = i === len ? ticksAngles[0].coord : ticksAngles[i].coord;
  309. var colorIndex = lineCount++ % areaColors.length;
  310. splitAreas[colorIndex] = splitAreas[colorIndex] || [];
  311. splitAreas[colorIndex].push(new graphic.Sector({
  312. shape: {
  313. cx: polar.cx,
  314. cy: polar.cy,
  315. r0: r0,
  316. r: r1,
  317. startAngle: prevAngle,
  318. endAngle: -coord * RADIAN,
  319. clockwise: clockwise
  320. },
  321. silent: true
  322. }));
  323. prevAngle = -coord * RADIAN;
  324. }
  325. // Simple optimization
  326. // Batching the lines if color are the same
  327. for (var i = 0; i < splitAreas.length; i++) {
  328. group.add(graphic.mergePath(splitAreas[i], {
  329. style: zrUtil.defaults({
  330. fill: areaColors[i % areaColors.length]
  331. }, areaStyleModel.getAreaStyle()),
  332. silent: true
  333. }));
  334. }
  335. }
  336. };
  337. export default AngleAxisView;