SingleAxisView.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var zrUtil = require("zrender/lib/core/util");
  2. var AxisBuilder = require("./AxisBuilder");
  3. var graphic = require("../../util/graphic");
  4. var singleAxisHelper = require("./singleAxisHelper");
  5. var AxisView = require("./AxisView");
  6. var getInterval = AxisBuilder.getInterval;
  7. var ifIgnoreOnTick = AxisBuilder.ifIgnoreOnTick;
  8. var axisBuilderAttrs = ['axisLine', 'axisTickLabel', 'axisName'];
  9. var selfBuilderAttr = 'splitLine';
  10. var SingleAxisView = AxisView.extend({
  11. type: 'singleAxis',
  12. axisPointerClass: 'SingleAxisPointer',
  13. render: function (axisModel, ecModel, api, payload) {
  14. var group = this.group;
  15. group.removeAll();
  16. var layout = singleAxisHelper.layout(axisModel);
  17. var axisBuilder = new AxisBuilder(axisModel, layout);
  18. zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
  19. group.add(axisBuilder.getGroup());
  20. if (axisModel.get(selfBuilderAttr + '.show')) {
  21. this['_' + selfBuilderAttr](axisModel, layout.labelInterval);
  22. }
  23. SingleAxisView.superCall(this, 'render', axisModel, ecModel, api, payload);
  24. },
  25. _splitLine: function (axisModel, labelInterval) {
  26. var axis = axisModel.axis;
  27. if (axis.scale.isBlank()) {
  28. return;
  29. }
  30. var splitLineModel = axisModel.getModel('splitLine');
  31. var lineStyleModel = splitLineModel.getModel('lineStyle');
  32. var lineWidth = lineStyleModel.get('width');
  33. var lineColors = lineStyleModel.get('color');
  34. var lineInterval = getInterval(splitLineModel, labelInterval);
  35. lineColors = lineColors instanceof Array ? lineColors : [lineColors];
  36. var gridRect = axisModel.coordinateSystem.getRect();
  37. var isHorizontal = axis.isHorizontal();
  38. var splitLines = [];
  39. var lineCount = 0;
  40. var ticksCoords = axis.getTicksCoords();
  41. var p1 = [];
  42. var p2 = [];
  43. var showMinLabel = axisModel.get('axisLabel.showMinLabel');
  44. var showMaxLabel = axisModel.get('axisLabel.showMaxLabel');
  45. for (var i = 0; i < ticksCoords.length; ++i) {
  46. if (ifIgnoreOnTick(axis, i, lineInterval, ticksCoords.length, showMinLabel, showMaxLabel)) {
  47. continue;
  48. }
  49. var tickCoord = axis.toGlobalCoord(ticksCoords[i]);
  50. if (isHorizontal) {
  51. p1[0] = tickCoord;
  52. p1[1] = gridRect.y;
  53. p2[0] = tickCoord;
  54. p2[1] = gridRect.y + gridRect.height;
  55. } else {
  56. p1[0] = gridRect.x;
  57. p1[1] = tickCoord;
  58. p2[0] = gridRect.x + gridRect.width;
  59. p2[1] = tickCoord;
  60. }
  61. var colorIndex = lineCount++ % lineColors.length;
  62. splitLines[colorIndex] = splitLines[colorIndex] || [];
  63. splitLines[colorIndex].push(new graphic.Line(graphic.subPixelOptimizeLine({
  64. shape: {
  65. x1: p1[0],
  66. y1: p1[1],
  67. x2: p2[0],
  68. y2: p2[1]
  69. },
  70. style: {
  71. lineWidth: lineWidth
  72. },
  73. silent: true
  74. })));
  75. }
  76. for (var i = 0; i < splitLines.length; ++i) {
  77. this.group.add(graphic.mergePath(splitLines[i], {
  78. style: {
  79. stroke: lineColors[i % lineColors.length],
  80. lineDash: lineStyleModel.getLineDash(lineWidth),
  81. lineWidth: lineWidth
  82. },
  83. silent: true
  84. }));
  85. }
  86. }
  87. });
  88. var _default = SingleAxisView;
  89. module.exports = _default;