line.js 2.2 KB

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