line.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var Util = require('../../util/common');
  2. var Shape = require('./shape');
  3. var ShapeUtil = require('./util');
  4. var Global = require('../../global'); // 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. Util.mix(style, cfg.style);
  16. return Util.mix({}, Global.shape.line, style);
  17. }
  18. function drawLines(cfg, container, style, smooth) {
  19. var points = cfg.points;
  20. if (points.length && Util.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 = ShapeUtil.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: Util.mix({
  37. points: topPoints,
  38. smooth: smooth
  39. }, style)
  40. });
  41. }
  42. var topShape = container.addShape('Polyline', {
  43. className: 'line',
  44. attrs: Util.mix({
  45. points: topPoints,
  46. smooth: smooth
  47. }, style)
  48. });
  49. var bottomShape = container.addShape('Polyline', {
  50. className: 'line',
  51. attrs: Util.mix({
  52. points: bottomPoints,
  53. smooth: 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: Util.mix({
  64. points: points,
  65. smooth: smooth
  66. }, style)
  67. });
  68. }
  69. var SHAPES = ['line', 'smooth', 'dash'];
  70. Util.each(SHAPES, function (shapeType) {
  71. Shape.registerShape('line', shapeType, {
  72. draw: function 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. module.exports = Line;