|
| 1 | +<!-- |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +--> |
| 14 | + |
| 15 | +<!DOCTYPE html> |
| 16 | +<html lang="en" > |
| 17 | + <head> |
| 18 | + <meta charset="utf-8" /> |
| 19 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 20 | + <meta name="theme-color" content="#000000" /> |
| 21 | + <meta name="keywords" content="kline time-line candlestick stock chart canvas k线 行情 蜡烛图 分时图 技术指标 图表"/> |
| 22 | + <meta name="description" content="shape test"/> |
| 23 | + <script type="text/javascript" src="../dist/klinecharts.min.js"></script> |
| 24 | + <script type="text/javascript" src="./js/dataSource.js"></script> |
| 25 | + <link rel="stylesheet" type="text/css" href="./css/chart.css"/> |
| 26 | + <title>技术指标模板自定义绘制</title> |
| 27 | + </head> |
| 28 | + <body> |
| 29 | + <noscript>You need to enable JavaScript to run this app.</noscript> |
| 30 | + <div id="chart"></div> |
| 31 | + <script> |
| 32 | + window.onload = function () { |
| 33 | + var chart = klinecharts.init('chart') |
| 34 | + // 添加一个指标模板 |
| 35 | + chart.addTechnicalIndicatorTemplate({ |
| 36 | + name: 'TEST-Render', |
| 37 | + precision: 2, |
| 38 | + calcTechnicalIndicator: function (kLineDataList, { params, plots }) { |
| 39 | + return kLineDataList.map(kLineData => ({ volume: kLineData.volume })) |
| 40 | + }, |
| 41 | + render: function ({ ctx, dataSource, viewport }) { |
| 42 | + let max = 0 |
| 43 | + const result = dataSource.technicalIndicatorDataList |
| 44 | + for (let i = dataSource.from; i < dataSource.to; i++) { |
| 45 | + max = Math.max(max, result[i].volume) |
| 46 | + } |
| 47 | + const length = dataSource.to - dataSource.from |
| 48 | + const barHeight = (viewport.height) / length |
| 49 | + const realBarHeight = barHeight * 0.8 |
| 50 | + |
| 51 | + ctx.globalCompositeOperation = 'destination-over' |
| 52 | + ctx.fillStyle = 'rgba(65, 20, 69, .35)' |
| 53 | + let startY = (barHeight - realBarHeight) / 2 |
| 54 | + for (let i = dataSource.from; i < dataSource.to; i++) { |
| 55 | + const barWidth = Math.floor(result[i].volume / max * 150) |
| 56 | + ctx.fillRect(viewport.width - barWidth, startY, barWidth, realBarHeight) |
| 57 | + startY += barHeight |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' }) |
| 62 | + chart.createTechnicalIndicator('TEST-Render', true, { id: 'candle_pane' }) |
| 63 | + chart.createTechnicalIndicator('KDJ') |
| 64 | + chart.applyNewData(generated()) |
| 65 | + } |
| 66 | + </script> |
| 67 | + </body> |
| 68 | +</html> |
0 commit comments