默认图形类型

horizontalRayLine, horizontalSegment, horizontalStraightLine, verticalRayLine, verticalSegment, verticalStraightLine, rayLine, segment, horizontalSegment, priceLine, priceChannelLine, parallelStraightLine, fibonacciLine

图形模板

创建一个模板,然后通过extension.addShapeTemplate 全局添加,或者通过图表实例方法addShapeTemplate 为单个图表实例添加即可。添加到图表即可和内置图形一样去使用。

属性说明

图形信息

  1. {
  2. // 名称,必须字段,作为图形标记的唯一标识
  3. name: 'xxx',
  4. // 总共需要多少步操作才行绘制完成,必须字段
  5. totalStep: 3,
  6. // 检查鼠标点是否在图形上,是一个回调方法,必须字段
  7. // key 创建数据源的时候给的key
  8. // type 图形类型
  9. // dataSource 图形信息
  10. // eventCoordinate 当前事件坐标
  11. // 需要返回boolean值
  12. checkEventCoordinateOnShape: ({ key, type, dataSource, eventCoordinate }) => {},
  13. // 创建图形数据源,是一个回调方法,必须字段,需要返回图形数据
  14. // step 当前步骤
  15. // points 时间价格点信息
  16. // coordinates 时间价格点对应的坐标轴信息
  17. // viewport 尺寸
  18. // precision 精度信息,包含价格和数量精度
  19. // styles 样式
  20. // xAxis x轴组件
  21. // yAxis y轴组件
  22. // data 外部数据
  23. createShapeDataSource: ({ step, points, coordinates, viewport, precision, styles, xAxis, yAxis, data }) => {},
  24. // 处理在绘制过程中鼠标移动操作,可缺省,鼠标操作绘制过程中触发
  25. // step 当前步骤
  26. // points 图形时间价格点信息
  27. // movePoint 移动对应的点
  28. // xAxis x轴组件
  29. // yAxis y轴组件
  30. performEventMoveForDrawing: ({ step, points, movePoint, xAxis, yAxis }) => {},
  31. // 处理鼠标按住移动操作,可缺省,鼠标按住某个操作点移动过程中触发
  32. // points 图形时间价格点信息
  33. // pressPointIndex 按住点的索引
  34. // pressPoint 按住对应的点
  35. // xAxis x轴组件
  36. // yAxis y轴组件
  37. performEventPressedMove: ({ points, pressPointIndex, pressPoint, xAxis, yAxis }) => {},
  38. // 扩展绘制,可缺省
  39. // ctx 画布上下文
  40. // dataSource 图形数据信息
  41. // styles 图表样式配置
  42. // viewport 尺寸
  43. // precision 精度信息,包含价格和数量精度
  44. // xAxis x轴组件
  45. // yAxis y轴组件
  46. // data 外部数据
  47. drawExtend: ({ ctx, dataSource, styles, viewport, precision, xAxis, yAxis, data }) => {}
  48. }

方法createShapeDataSource返回值子项信息

  1. {
  2. // key
  3. key: 'key',
  4. // 类型,目前支持'line' | 'text' | 'continuous_line' | 'polygon' | 'arc',是必要字段
  5. type: 'line',
  6. // 是否要绘制,可缺省,默认绘制
  7. isDraw: true,
  8. // 是否要检查是否在图形上,可缺省,默认不检查
  9. isCheck: true,
  10. // 样式
  11. styles: {},
  12. // 数据数组,当type是'line'时是二维数组
  13. dataSource: []
  14. }

dataSource子项信息

  1. {
  2. // x坐标,必要字段
  3. x: 123,
  4. // y坐标,必要字段
  5. y: 123,
  6. // 半径,当type为'arc'时需要
  7. radius: 5,
  8. // 起始角度,当type为'arc'时需要
  9. startAngle: 0,
  10. // 结束角度,当type为'arc'时需要
  11. endAngle: 180,
  12. // 文字,当type为'text'时需要
  13. text: '',
  14. }

示例

以一个填充带边框的圆来具体说明如何配置。

  1. {
  2. // 名称
  3. name: 'circle',
  4. // 完成一个圆的绘制需要三个步骤
  5. totalStep: 3,
  6. // 检查事件坐标是否在圆边框上
  7. checkEventCoordinateOnShape: ({ dataSource, eventCoordinate }) => {
  8. const xDis = Math.abs(dataSource.x - eventCoordinate.x)
  9. const yDis = Math.abs(dataSource.y - eventCoordinate.y)
  10. const r = Math.sqrt(xDis * xDis + yDis * yDis)
  11. return Math.abs(r - dataSource.radius) < 2;
  12. },
  13. // 创建图形信息
  14. createShapeDataSource: ({ step, points, coordinates }) => {
  15. if (coordinates.length === 2) {
  16. const xDis = Math.abs(coordinates[0].x - coordinates[1].x)
  17. const yDis = Math.abs(coordinates[0].y - coordinates[1].y)
  18. const radius = Math.sqrt(xDis * xDis + yDis * yDis)
  19. // 通过一个空心圆和一个实心圆来组成一个填充带边框的圆
  20. return [
  21. // 实心圆
  22. {
  23. type: 'arc',
  24. isDraw: true,
  25. // 实心圆,不用去检查鼠标点是否在图形上
  26. isCheck: false,
  27. // 填充
  28. styles: { style: 'fill' },
  29. // 点信息
  30. dataSource: [
  31. { ...coordinates[0], radius, startAngle: 0, endAngle: Math.PI * 2 },
  32. ]
  33. },
  34. // 空心圆
  35. {
  36. type: 'arc',
  37. isDraw: true,
  38. // 需要检查是否在边框上
  39. isCheck: true,
  40. // 点信息
  41. dataSource: [
  42. { ...coordinates[0], radius, startAngle: 0, endAngle: Math.PI * 2 },
  43. ]
  44. }
  45. ]
  46. }
  47. return []
  48. }
  49. }