1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| const boxDataScope = 300
const goldenSectionA = 0.191 const goldenSectionB = 0.382 const goldenSectionC = 0.5 const goldenSectionD = 0.618 const goldenSectionE = 0.809 this.chart.addTechnicalIndicatorTemplate({ name: 'custom_box_solid', shortName: '箱体', precision: 2, plots: [ { key: 'max', title: '最高:' }, { key: 'min', title: '最低:' } ], calcParams: [boxDataScope, goldenSectionA, goldenSectionB, goldenSectionC, goldenSectionD, goldenSectionE], calcTechnicalIndicator: (dataList, { params, plots }) => { let allDatas = [] let selectedDatas = [] for (let i = 0; i < dataList.length; i++) { let kLineData = dataList[i] if (new Date(dateConvert(kLineData.timestamp)).getTime() === getGlobalObject('boxId')) { const size = params[0] let startData = i - size + 1 if (startData < 0) { startData = 0 } let endData = i + 1 if (endData > dataList.length) { endData = dataList.length } selectedDatas = dataList.slice(startData, endData) } allDatas.push({ timestamp: kLineData.timestamp }) } let max, min selectedDatas.forEach(function (item) { let value = item.close - item.close2 if (!max || value > max) { max = value } if (!min || value < min) { min = value } }) return allDatas.map((data, i) => { let item = { } selectedDatas.map((selected, j) => { if (data.timestamp === selected.timestamp) { item.timestamp = selected.timestamp item.max = max item.min = min } }) return item }) }, render: ({ ctx, dataSource, viewport, styles, xAxis, yAxis }) => { if (dataSource.technicalIndicatorDataList.length <= 0) { return } let x = xAxis.convertToPixel(0) let start let end = dataSource.technicalIndicatorDataList.length - 1 dataSource.technicalIndicatorDataList.forEach(function (kLineData, i) { if (kLineData.timestamp) { if (!start) { start = i } if (i < end && !dataSource.technicalIndicatorDataList[i + 1].timestamp) { end = i } let max = kLineData.max let min = kLineData.min ctx.fillStyle = '#fff' ctx.textBaseline = 'middle' ctx.textAlign = 'center' ctx.strokeStyle = '#DC143C' let yHigh = yAxis.convertToPixel(max) let yLow = yAxis.convertToPixel(min) ctx.beginPath() ctx.moveTo(x, yHigh) if (i === start) { ctx.lineTo(x, yLow) ctx.moveTo(x, yHigh) } if (i === end) { ctx.lineTo(x, yLow) ctx.fillText(max, x + viewport.dataSpace, yHigh) ctx.moveTo(x, yLow) ctx.fillText(min, x + viewport.dataSpace, yLow) } else { ctx.lineTo(x + viewport.dataSpace, yHigh) ctx.moveTo(x, yLow) ctx.lineTo(x + viewport.dataSpace, yLow) } ctx.stroke() ctx.closePath() ctx.strokeStyle = '#ffffff' let goldenSectionLineA = (max - min) * goldenSectionA + min let goldenSectionLineB = (max - min) * goldenSectionB + min let goldenSectionLineC = (max - min) * goldenSectionC + min let goldenSectionLineD = (max - min) * goldenSectionD + min let goldenSectionLineE = (max - min) * goldenSectionE + min let yA = yAxis.convertToPixel(goldenSectionLineA) let yB = yAxis.convertToPixel(goldenSectionLineB) let yC = yAxis.convertToPixel(goldenSectionLineC) let yD = yAxis.convertToPixel(goldenSectionLineD) let yE = yAxis.convertToPixel(goldenSectionLineE) ctx.beginPath() ctx.moveTo(x, yA) ctx.lineTo(x + viewport.barSpace / 2, yA) if (i === end) { ctx.fillText(goldenSectionLineA.toFixed(2), x + viewport.dataSpace, yA) } ctx.moveTo(x, yB) ctx.lineTo(x + viewport.barSpace / 2, yB) if (i === end) { ctx.fillText(goldenSectionLineB.toFixed(2), x + viewport.dataSpace, yB) } ctx.moveTo(x, yC) ctx.lineTo(x + viewport.barSpace / 2, yC) if (i === end) { ctx.fillText(goldenSectionLineC.toFixed(2), x + viewport.dataSpace, yC) } ctx.moveTo(x, yD) ctx.lineTo(x + viewport.barSpace / 2, yD) if (i === end) { ctx.fillText(goldenSectionLineD.toFixed(2), x + viewport.dataSpace, yD) } ctx.moveTo(x, yE) ctx.lineTo(x + viewport.barSpace / 2, yE) if (i === end) { ctx.fillText(goldenSectionLineE.toFixed(2), x + viewport.dataSpace, yE) } ctx.stroke() ctx.closePath() } x += viewport.dataSpace }) } })
|