schema.js 1.8 KB

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