aaac43146ab6c7af5444a81caebf73d17edccf01.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * echarts组件: 网格
  3. *
  4. * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
  5. * @author Kener (@Kener-林峰, kener.linfeng@gmail.com)
  6. *
  7. */
  8. define(function (require) {
  9. var Base = require('./base');
  10. // 图形依赖
  11. var RectangleShape = require('zrender/shape/Rectangle');
  12. var ecConfig = require('../config');
  13. // 网格
  14. ecConfig.grid = {
  15. zlevel: 0, // 一级层叠
  16. z: 0, // 二级层叠
  17. x: 80,
  18. y: 60,
  19. x2: 80,
  20. y2: 60,
  21. // width: {totalWidth} - x - x2,
  22. // height: {totalHeight} - y - y2,
  23. backgroundColor: 'rgba(0,0,0,0)',
  24. borderWidth: 1,
  25. borderColor: '#ccc'
  26. };
  27. var zrUtil = require('zrender/tool/util');
  28. /**
  29. * 构造函数
  30. * @param {Object} messageCenter echart消息中心
  31. * @param {ZRender} zr zrender实例
  32. * @param {Object} option 图表选项
  33. * @param {number=} option.grid.x 直角坐标系内绘图网格起始横坐标,数值单位px
  34. * @param {number=} option.grid.y 直角坐标系内绘图网格起始纵坐标,数值单位px
  35. * @param {number=} option.grid.width 直角坐标系内绘图网格宽度,数值单位px
  36. * @param {number=} option.grid.height 直角坐标系内绘图网格高度,数值单位px
  37. */
  38. function Grid(ecTheme, messageCenter, zr, option, myChart) {
  39. Base.call(this, ecTheme, messageCenter, zr, option, myChart);
  40. this.refresh(option);
  41. }
  42. Grid.prototype = {
  43. type: ecConfig.COMPONENT_TYPE_GRID,
  44. getX: function () {
  45. return this._x;
  46. },
  47. getY: function () {
  48. return this._y;
  49. },
  50. getWidth: function () {
  51. return this._width;
  52. },
  53. getHeight: function () {
  54. return this._height;
  55. },
  56. getXend: function () {
  57. return this._x + this._width;
  58. },
  59. getYend: function () {
  60. return this._y + this._height;
  61. },
  62. getArea: function () {
  63. return {
  64. x: this._x,
  65. y: this._y,
  66. width: this._width,
  67. height: this._height
  68. };
  69. },
  70. getBbox: function() {
  71. return [
  72. [ this._x, this._y ],
  73. [ this.getXend(), this.getYend() ]
  74. ];
  75. },
  76. /**
  77. * 实在找不到合适的地方做了,各种粗暴的写法~ -_-
  78. */
  79. refixAxisShape: function(component) {
  80. var zeroX;
  81. var zeroY;
  82. var axisList = component.xAxis._axisList.concat(
  83. component.yAxis ? component.yAxis._axisList : []
  84. );
  85. var len = axisList.length;
  86. var axis;
  87. while (len--) {
  88. axis = axisList[len];
  89. if (axis.type == ecConfig.COMPONENT_TYPE_AXIS_VALUE
  90. && axis._min < 0
  91. && axis._max >= 0
  92. ) {
  93. axis.isHorizontal()
  94. ? (zeroX = axis.getCoord(0))
  95. : (zeroY = axis.getCoord(0));
  96. }
  97. }
  98. if (typeof zeroX != 'undefined' || typeof zeroY != 'undefined') {
  99. len = axisList.length;
  100. while (len--) {
  101. axisList[len].refixAxisShape(zeroX, zeroY);
  102. }
  103. }
  104. },
  105. refresh: function (newOption) {
  106. if (newOption
  107. || this._zrWidth != this.zr.getWidth()
  108. || this._zrHeight != this.zr.getHeight()
  109. ) {
  110. this.clear();
  111. this.option = newOption || this.option;
  112. this.option.grid = this.reformOption(this.option.grid);
  113. var gridOption = this.option.grid;
  114. this._zrWidth = this.zr.getWidth();
  115. this._zrHeight = this.zr.getHeight();
  116. this._x = this.parsePercent(gridOption.x, this._zrWidth);
  117. this._y = this.parsePercent(gridOption.y, this._zrHeight);
  118. var x2 = this.parsePercent(gridOption.x2, this._zrWidth);
  119. var y2 = this.parsePercent(gridOption.y2, this._zrHeight);
  120. if (typeof gridOption.width == 'undefined') {
  121. this._width = this._zrWidth - this._x - x2;
  122. }
  123. else {
  124. this._width = this.parsePercent(gridOption.width, this._zrWidth);
  125. }
  126. this._width = this._width <= 0 ? 10 : this._width;
  127. if (typeof gridOption.height == 'undefined') {
  128. this._height = this._zrHeight - this._y - y2;
  129. }
  130. else {
  131. this._height = this.parsePercent(gridOption.height, this._zrHeight);
  132. }
  133. this._height = this._height <= 0 ? 10 : this._height;
  134. this._x = this.subPixelOptimize(this._x, gridOption.borderWidth);
  135. this._y = this.subPixelOptimize(this._y, gridOption.borderWidth);
  136. this.shapeList.push(new RectangleShape({
  137. zlevel: this.getZlevelBase(),
  138. z: this.getZBase(),
  139. hoverable: false,
  140. style: {
  141. x: this._x,
  142. y: this._y,
  143. width: this._width,
  144. height: this._height,
  145. brushType: gridOption.borderWidth > 0 ? 'both' : 'fill',
  146. color: gridOption.backgroundColor,
  147. strokeColor: gridOption.borderColor,
  148. lineWidth: gridOption.borderWidth
  149. // type: this.option.splitArea.areaStyle.type,
  150. }
  151. }));
  152. this.zr.addShape(this.shapeList[0]);
  153. }
  154. }
  155. };
  156. zrUtil.inherits(Grid, Base);
  157. require('../component').define('grid', Grid);
  158. return Grid;
  159. });