line.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import Global from '../../global';
  2. import Shape from './shape';
  3. import { mix, each, isArray } from '../../util/common';
  4. import { splitPoints } from './util'; // register line geom
  5. var Line = Shape.registerFactory('line', {
  6. defaultShapeType: 'line'
  7. });
  8. function getStyle(cfg) {
  9. var style = {
  10. strokeStyle: cfg.color
  11. };
  12. if (cfg.size >= 0) {
  13. style.lineWidth = cfg.size;
  14. }
  15. mix(style, cfg.style);
  16. return mix({}, Global.shape.line, style);
  17. }
  18. function drawLines(cfg, container, style, smooth) {
  19. var points = cfg.points;
  20. if (points.length && isArray(points[0].y)) {
  21. var topPoints = [];
  22. var bottomPoints = [];
  23. for (var i = 0, len = points.length; i < len; i++) {
  24. var point = points[i];
  25. var tmp = splitPoints(point);
  26. bottomPoints.push(tmp[0]);
  27. topPoints.push(tmp[1]);
  28. }
  29. if (cfg.isInCircle) {
  30. topPoints.push(topPoints[0]);
  31. bottomPoints.push(bottomPoints[0]);
  32. }
  33. if (cfg.isStack) {
  34. return container.addShape('Polyline', {
  35. className: 'line',
  36. attrs: mix({
  37. points: topPoints,
  38. smooth
  39. }, style)
  40. });
  41. }
  42. var topShape = container.addShape('Polyline', {
  43. className: 'line',
  44. attrs: mix({
  45. points: topPoints,
  46. smooth
  47. }, style)
  48. });
  49. var bottomShape = container.addShape('Polyline', {
  50. className: 'line',
  51. attrs: mix({
  52. points: bottomPoints,
  53. smooth
  54. }, style)
  55. });
  56. return [topShape, bottomShape];
  57. }
  58. if (cfg.isInCircle) {
  59. points.push(points[0]);
  60. }
  61. return container.addShape('Polyline', {
  62. className: 'line',
  63. attrs: mix({
  64. points,
  65. smooth
  66. }, style)
  67. });
  68. }
  69. var SHAPES = ['line', 'smooth', 'dash'];
  70. each(SHAPES, function (shapeType) {
  71. Shape.registerShape('line', shapeType, {
  72. draw(cfg, container) {
  73. var smooth = shapeType === 'smooth';
  74. var style = getStyle(cfg);
  75. if (shapeType === 'dash') {
  76. style.lineDash = Global.lineDash;
  77. }
  78. return drawLines(cfg, container, style, smooth);
  79. }
  80. });
  81. });
  82. export default Line;