schema.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import Shape from './shape';
  2. import { mix } from '../../util/common';
  3. function _sortValue(value) {
  4. var sorted = value.sort(function (a, b) {
  5. return a < b ? 1 : -1;
  6. });
  7. var length = sorted.length;
  8. if (length < 4) {
  9. var min = sorted[length - 1];
  10. for (var i = 0; i < 4 - length; i++) {
  11. sorted.push(min);
  12. }
  13. }
  14. return sorted;
  15. } // from left bottom corner, and clockwise
  16. function getCandlePoints(x, y, width) {
  17. var yValues = _sortValue(y);
  18. var points = [{
  19. x,
  20. y: yValues[0]
  21. }, {
  22. x,
  23. y: yValues[1]
  24. }, {
  25. x: x - width / 2,
  26. y: yValues[2]
  27. }, {
  28. x: x - width / 2,
  29. y: yValues[1]
  30. }, {
  31. x: x + width / 2,
  32. y: yValues[1]
  33. }, {
  34. x: x + width / 2,
  35. y: yValues[2]
  36. }, {
  37. x,
  38. y: yValues[2]
  39. }, {
  40. x,
  41. y: yValues[3]
  42. }];
  43. return points;
  44. }
  45. var Schema = Shape.registerFactory('schema', {});
  46. Shape.registerShape('schema', 'candle', {
  47. getPoints(cfg) {
  48. return getCandlePoints(cfg.x, cfg.y, cfg.size);
  49. },
  50. draw(cfg, container) {
  51. var points = this.parsePoints(cfg.points);
  52. var style = mix({
  53. stroke: cfg.color,
  54. fill: cfg.color,
  55. lineWidth: 1
  56. }, cfg.style);
  57. return container.addShape('Custom', {
  58. className: 'schema',
  59. attrs: style,
  60. createPath(ctx) {
  61. ctx.beginPath();
  62. ctx.moveTo(points[0].x, points[0].y);
  63. ctx.lineTo(points[1].x, points[1].y);
  64. ctx.moveTo(points[2].x, points[2].y);
  65. for (var i = 3; i < 6; i++) {
  66. ctx.lineTo(points[i].x, points[i].y);
  67. }
  68. ctx.closePath();
  69. ctx.moveTo(points[6].x, points[6].y);
  70. ctx.lineTo(points[7].x, points[7].y);
  71. }
  72. });
  73. }
  74. });
  75. export default Schema;