-
Notifications
You must be signed in to change notification settings - Fork 0
/
mins-painter-v3.html
402 lines (372 loc) · 18.5 KB
/
mins-painter-v3.html
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>html 5 minutes chart painter</title>
<script src="libs/loading.js" type="text/javascript" charset="GBK"></script>
<script src="libs/util.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/absPainter.js" type="text/javascript" charset="GBK"></script>
<script src="libs/ajax.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/crossLines.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/axis-x.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/linepainter.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/volumePainter.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/axis-y.js?a" type="text/javascript" charset="GBK"></script>
<script src="libs/chartEventHelper.js?a" type="text/javascript" charset="GBK"></script>
</head>
<body>
<canvas id="canvas" width="414" height="310" style="z-index: 3; background: transparent;
border: 1px solid #69c">
<p>
你的浏览器不支持html5哟</p>
</canvas>
<div id="debug"></div>
<!--
options:{
fallColor:'green',riseColor:'red',normalColor:'black',
topText:{font:'11px 宋体',color:'black',region:{x:.5,y:.5,width:213,height:20}},
minsChart:{region:{x:2,y:20,width:180,height:150},priceLineColor:'blue',middleLineColor:'red',otherSplitLineColor:'lightgray',borderColor:'gray'},
xScaler:{font:'11px 宋体',color:black,region:{x:5,y:130,width:180,height:20}},
bottomText:{font:'11px 宋体',color:'black',region:{x:5,y:130,width:213,height:20}}
}
-->
<script src="mins-data.js"></script><!---->
<script type="text/javascript">
/*
html5行情图库
author:yukaizhao
blog:http://www.cnblogs.com/yukaizhao/ http://weibo.com/yukaizhao/
参与项目或技术交流:[email protected]
*/
function line(ctx, x0, y0, x1, y1, color, width) {
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
ctx.strokeStyle = color;
ctx.lineWidth = width || 1;
ctx.stroke();
}
function getMinTime(minIndex) {
//上午09:30-11:30
//下午13:00-15:00
var d = new Date();
if (minIndex <= 120) {
d.setHours(9, 30, 30);
d = new Date(d.getTime() + (minIndex) * 60 * 1000);
} else {
d.setHours(13, 0, 0);
d = new Date(d.getTime() + (minIndex - 120) * 60 * 1000);
}
var hour = d.getHours() > 9 ? new String(d.getHours()) : '0' + d.getHours();
var minutes = d.getMinutes() > 9 ? new String(d.getMinutes()) : '0' + d.getMinutes();
var seconds = '30';
return hour + '' + minutes + seconds;
}
function Tip(options) {
this.options = options;
this.canvas = options.canvas;
this.canvas.tip = this;
}
Tip.prototype = {
show: function (relativePoint, html) {
var dc = this.dataContext;
var painter = this.canvas.painter;
if (dc) {
if (dc.isNewQuote) painter.fillTopText(dc.data);
else painter.fillTopText(dc.data, dc.index);
}
},
update: function (relativePoint, html) {
this.show(relativePoint, html);
},
hide: function () {
var dc = this.dataContext;
var painter = this.canvas.painter;
if (dc) {
painter.fillTopText(dc.data);
}
}
};
function minsChart(canvasId, options) {
extendObject(options, this);
this.canvas = $id(canvasId);
this.ctx = this.canvas.getContext('2d');
this.canvas.painter = this;
}
minsChart.prototype = {
/*
data format like :{
quote: {
time: 20111214150106,
open: 2241.390,
preClose: 2248.590,
highest: 2256.740,
lowest: 2224.730,
price: 2228.530,
volume: 4407982200,
amount: 38621178573
},
mins: [
{price:2239.45,volume:49499299,amount:459279327}
]
}
*/
paint: function (data) {
this.fillTopText(data);
this.paintChart(data);
this.paintxAxis();
this.fillBottomText(data);
this.paintVolume(data);
},
paintVolume: function (data) {
var ctx = this.ctx;
var options = this.volume;
ctx.beginPath();
ctx.rect(options.region.x, options.region.y, options.region.width, options.region.height);
ctx.strokeStyle = options.borderColor;
ctx.stroke();
line(ctx, options.region.x, options.region.y + options.region.height / 2, options.region.x + options.region.width, options.region.y + options.region.height / 2, options.splitLineColor);
options.getDataLength = function () { return this.data.items.length; };
options.maxDotsCount = this.maxDotsCount;
var volumePainterImp = new volumePainter(options);
var painter = new Painter(this.canvas.id, volumePainterImp, { items: data.mins });
painter.paint();
var max = painter.maxVolume;
var unit;
if (max / 1000000 > 1000) {
max = max / 1000000;
unit = '百万';
} else {
max = max / 10000;
unit = '万';
}
var scalers = [max.toFixed(2), (max / 2).toFixed(2), '(' + unit + ')'];
var yscaler = new yAxis(this.volume.yScaler);
var painter = new Painter(this.canvas.id, yscaler, scalers);
painter.paint();
},
fillBottomText: function (data) {
if (!this.bottomText) return;
//高9999 低9999 成交888999
var ctx = this.ctx;
var txt = '高';
var options = this.bottomText;
ctx.font = options.font;
ctx.fillStyle = options.color;
var w = ctx.measureText(txt).width;
ctx.fillText(txt, options.region.x, options.region.y);
var x = options.region.x + w;
var quote = data.quote;
var me = this;
function getTxtColor(val) { return val > quote.preClose ? me.riseColor : (val == quote.preClose ? me.normalColor : me.fallColor); }
var highColor = getTxtColor(quote.highest);
var high = toMoney(quote.highest);
ctx.fillStyle = highColor;
w = ctx.measureText(high).width;
ctx.fillText(high, x, options.region.y);
x += w;
txt = ' 低';
ctx.fillStyle = options.color;
w = ctx.measureText(txt).width;
ctx.fillText(txt, x, options.region.y);
x += w;
var lowColor = getTxtColor(quote.lowest);
var low = toMoney(quote.lowest);
w = ctx.measureText(low).width;
ctx.fillStyle = lowColor;
ctx.fillText(low, x, options.region.y);
x += w;
ctx.fillStyle = options.color;
var amount = ' 成交' + bigNumberToText(quote.amount);
ctx.fillText(amount, x, options.region.y);
},
paintxAxis: function () {
var xAxisImpl = new xAxis(this.xScaler);
var xAxisPainter = new Painter(this.canvas.id, xAxisImpl, this.xScaler.data);
xAxisPainter.paint();
},
paintChart: function (data) {
var minsChartOptions = this.minsChart;
var region = this.minsChart.region;
var ctx = this.ctx;
ctx.beginPath();
ctx.strokeStyle = minsChartOptions.borderColor;
ctx.rect(region.x, region.y, region.width, region.height);
ctx.stroke();
//水平线
var middleIndex = (this.minsChart.horizontalLineCount + this.minsChart.horizontalLineCount % 2) / 2;
var splitCount = this.minsChart.horizontalLineCount + 1;
for (var i = 1; i <= this.minsChart.horizontalLineCount; i++) {
var color = (i == middleIndex ? minsChartOptions.middleLineColor : minsChartOptions.otherSplitLineColor);
var y = region.y + region.height * i / splitCount;
line(ctx, region.x, y, region.x + region.width, y, color);
}
//垂直线
splitCount = this.minsChart.verticalLineCount + 1;
for (var i = 1; i <= this.minsChart.verticalLineCount; i++) {
var x = region.x + region.width * i / splitCount;
line(ctx, x, region.y, x, region.y + region.height, minsChartOptions.otherSplitLineColor);
}
//价格线
var lineOptions = {
region: region,
maxDotsCount: this.maxDotsCount,
getDataLength: function () { return this.data.items.length; },
getItemValue: function (item) { return item.price; },
middleValue: data.quote.preClose, //通常是昨收
lineColor: minsChartOptions.priceLineColor
};
var linePainterImp = new linePainter(lineOptions);
var priceLinePainter = new Painter(this.canvas.id, linePainterImp, { items: data.mins });
priceLinePainter.paint();
//y轴
var yOptions = this.minsChart.yScalerLeft;
var preClose = data.quote.preClose;
var me = this;
yOptions.color = function (val) {
return val > preClose ? me.riseColor : (val == preClose ? me.normalColor : me.fallColor);
};
var scalersLeft = [];
var scalersRight = [];
var min = preClose - priceLinePainter.maxDiff;
var space = priceLinePainter.maxDiff * 2 / (this.minsChart.horizontalLineCount + 1);
for (var i = this.minsChart.horizontalLineCount + 1; i >= 0; i--) {
var val = min + i * space;
scalersLeft.push(val.toFixed(2));
var percent = (val - preClose) * 100 / preClose;
scalersRight.push(percent.toFixed(2) + '%');
}
var yx = new yAxis(yOptions);
var yAxisPainter = new Painter(this.canvas.id, yx, scalersLeft);
yAxisPainter.paint();
var yPercentOptions = this.minsChart.yScalerRight;
yPercentOptions.color = function (val) {
return (val == '0.00%' ? 'black' : (val.charAt(0) == '-' ? 'green' : 'red'));
};
var yxPercent = new yAxis(yPercentOptions);
var yxPercentPainter = new Painter(this.canvas.id, yxPercent, scalersRight);
yxPercentPainter.paint();
//均线
if (this.needPaintAvgPriceLine) {
//生成移动均线数据
var items = [];
var totalVolume = 0;
var totalAmount = 0;
data.mins.each(function (item) {
totalVolume += item.volume;
totalAmount += item.amount;
items.push(totalAmount / totalVolume);
});
lineOptions.lineColor = minsChartOptions.avgPriceLineColor;
lineOptions.getItemValue = function (item) { return item; };
linePainterImp = new linePainter(lineOptions);
var painterAvg = new Painter(this.canvas.id, linePainterImp, { items: items });
painterAvg.paint();
}
var me = this;
var chartRegion = me.minsChart.region;
function getY(x) {
var index = Math.ceil((x - me.minsChart.region.x) * me.maxDotsCount / me.minsChart.region.width);
var val;
var isNewQuote;
if (index >= 0 && index < data.mins.length) {
val = data.mins[index].price;
isNewQuote = false;
} else {
val = data.quote.price;
isNewQuote = true;
}
if (me.canvas.tip) me.canvas.tip.dataContext = { data: data, isNewQuote: isNewQuote, index: index };
var diff = val - preClose;
var middleY = (me.minsChart.region.y + me.minsChart.region.height / 2);
return middleY - diff * me.minsChart.region.height / 2 / priceLinePainter.maxDiff;
}
//添加鼠标事件
addCrossLinesAndTipEvents(this.canvas, {
getCrossPoint: function (ev) { return { x: ev.offsetX, y: getY(ev.offsetX) }; },
triggerEventRanges: { x: chartRegion.x, y: chartRegion.y, width: chartRegion.width, height: me.volume.region.y + me.volume.region.height - chartRegion.y },
tipOptions: {
getTipHtml: function (ev) { return null; },
position: { x: false, y: false }
},
crossLineOptions: {
color: 'black'
}
});
},
fillTopText: function (data, minIndex) {
var quote = data.quote;
var ctx = this.ctx;
var topText = this.topText;
var region = topText.region;
ctx.clearRect(region.x, region.y, region.width, region.height);
var price;
var time;
if (typeof minIndex == 'undefined') {
price = quote.price;
time = quote.time;
} else {
price = data.mins[minIndex].price;
time = quote.time.toString().substr(0, 8) + getMinTime(minIndex);
}
ctx.fillStyle = topText.color;
ctx.font = topText.font;
if (topText.textBaseline) ctx.textBaseline = topText.textBaseline;
var txt = '最新' + toMoney(price);
var width = ctx.measureText(txt).width;
ctx.fillText(txt, topText.region.x, topText.region.y);
var isRise = price > quote.preClose;
var isEqual = price == quote.preClose;
var isFall = price < quote.preClose;
var diff = toMoney(price - quote.preClose);
var txtRiseFall = (isRise ? '↑' : (isFall ? '↓' : '')) + diff
+ ('(')
+ toMoney(diff * 100 / quote.preClose)
+ '%)';
var x = topText.region.x + width;
ctx.fillStyle = isRise ? this.riseColor : (isFall ? this.fallColor : this.normalColor);
ctx.fillText(txtRiseFall, x, topText.region.y);
var temp = new String(time);
var txtTime = temp.charAt(8) + temp.charAt(9) + ':' + temp.charAt(10) + temp.charAt(11);
ctx.fillStyle = topText.color;
var timeWidth = ctx.measureText(txtTime).width;
ctx.fillText(txtTime, topText.region.x + topText.region.width - timeWidth, topText.region.y);
}
};
var chart = new minsChart('canvas', {
fallColor: 'green', riseColor: 'red', normalColor: 'black', maxDotsCount: 241, needPaintAvgPriceLine: false,
backgroundColor:'white',
topText: { font: '12px 宋体', color: 'black', region: { x: 58.5, y: 5.5, width: 305, height: 14 }, textBaseline: 'top' },
minsChart: {
region: { x: 56.5, y: 21.5, width: 310, height: 200 },
priceLineColor: 'blue', avgPriceLineColor: 'red', middleLineColor: 'red', otherSplitLineColor: 'lightgray', borderColor: 'gray',
horizontalLineCount: 7, verticalLineCount: 3,
yScalerLeft: { font: '12px Arial', region: { x: .5, y: 20, width: 50.5, height: 200 }, align: 'right', fontHeight: 9, textBaseline: 'top' },
yScalerRight: { font: '12px Arial', region: { x: 368.5, y: 20, width: 40.5, height: 200 }, align: 'right', fontHeight: 9, textBaseline: 'top' }
},
xScaler: {
font: '12px Arial', color: 'black',
region: { x: 56.5, y: 225, width: 310, height: 20 },
data: ['09:30', '10:30', '11:30/13:00', '14:00', '15:00']
},
//bottomText: { font: '11px 宋体', color: 'black', region: { x: 5.5, y: 260, width: 400, height: 20} },
volume: {
region: { x: 56.5, y: 245.5, width: 310, height: 60 },
bar: { color: 'green', width: 2 },
borderColor: 'lightgray', splitLineColor: 'lightgray',
yScaler: { font: '12px Arial', region: { x: .5, y: 242.5, width: 50.5, height: 60 }, color: 'black', align: 'right', fontHeight: 12, textBaseline: 'top' }
}
});
var data = getQuote();
chart.paint(data);
</script>
<style>
pre{padding: 60px 0 0 30px;color: gray;font-size: .8em;line-height: 20px;}
</style>
<p><a href="./index.html">返回列表页</a></p>
<pre>
Author:yukaizhao http://www.cnblogs.com/yukaizhao/ http://weibo.com/yukaizhao/
参与项目或技术交流:<a href="mailto:[email protected]">[email protected]</a>
</pre>
</body>
</html>