-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailyTradeAnalysis.v2.htm
281 lines (261 loc) · 14.7 KB
/
dailyTradeAnalysis.v2.htm
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
<!doctype html >
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>资金流向盘中分价统计图</title>
<script src="libs/util.js" type="text/javascript" charset="GBK"></script>
<script src="libs/crossLines.js" type="text/javascript" charset="GBK"></script>
<script src="libs/tip.js" type="text/javascript" charset="GBK"></script>
<script src="libs/loading.compressed.js" type="text/javascript"></script>
<script src="libs/ajax.js" type="text/javascript"></script>
<script src="libs/chartEventHelper.js" type="text/javascript"></script>
</head>
<body>
<canvas id="canvas" width="400" height="500" style="border:1px solid #69c"></canvas>
<div id="debug"></div>
<script type="text/javascript">
/*
html5行情图库
author:yukaizhao
blog:http://www.cnblogs.com/yukaizhao/ http://weibo.com/yukaizhao/
参与项目或技术交流:[email protected]
*/
//http://vol.stock.hexun.com/charts/now/share/PriceAmount.ashx?code=600036
//http://vol.stock.hexun.com/Now/share/PriceAmount.shtml?rank=3&code=600036
/*
* canvasId: canvasId
* options:{
titleOptions:{position:{x:1,y:1},fontColor:'black',font:'12px Bold Arial'},
priceUnitOptions:{position:{x:20,y:15},fontColor:'black',font:'11px Arial',text:'(元)'},
flowInInstructionOptions :{position:{x:60,y:30},color:'red',text:'流入性资金',font:'11px Arial',fontColor:'black'},
flowOutInstructionOptions:{position:{x:120,y:30},color:'red',text:'流出性资金',font:'11px Arial',fontColor:'black'},
chartOptions:{position:{x:50,y:50},width:300,itemHeight:18,borderColor:'gray',lineColor:'gray',minHeight:200},
//成交单位选项
dealUnitOptions:{color:'red',font:'11px Arial',fontColor:'black',height:20}
}
* data : {title:'招商银行(600036)日成交分析',unit:'万元',items:[{priceRanges:{begin:11.95,to:11.96},flowIn:354.23,flowOut:18.64}]};
*
*/
function moneyFlowAnalysisPainter(canvasId, options, data) {
this.canvas = document.getElementById(canvasId);
if (!this.canvas.getContext) return;
var canvas = this.canvas;
var ctx = this.canvas.getContext('2d');
var chartOptions = options.chartOptions;
var dealUnitOptions = options.dealUnitOptions;
var dataLength = data.items.length;
chartOptions.height = Math.max(chartOptions.minHeight,dataLength * chartOptions.itemHeight);
var canvasHeight = chartOptions.height + chartOptions.position.x + dealUnitOptions.height;
canvas.height = canvasHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
function drawText(ops) {
ctx.fillStyle = ops.fontColor;
ctx.font = ops.font;
if (ops.textBaseline) ctx.textBaseline = ops.textBaseline;
ctx.fillText(ops.text, ops.position.x, ops.position.y);
}
//画标题
if (options.titleOptions) {
var to = options.titleOptions;
to.text = data.title;
drawText(to);
}
//画价格单位
//priceUnitOptions:{position:{x:20,y:15},fontColor:'black',font:'11px Arial',text:'(元)'},
if (options.priceUnitOptions) {
drawText(options.priceUnitOptions);
}
/*
flowInInstructionOptions :{position:{x:60,y:30},color:'red',text:'流入性资金',font:'11px Arial',fontColor:'black'},
flowOutInstructionOptions:{position:{x:120,y:30},color:'red',text:'流出性资金',font:'11px Arial',fontColor:'black'},
*/
if (options.flowInInstructionOptions) {
var op = options.flowInInstructionOptions;
ctx.beginPath();
ctx.rect(op.position.x,op.position.y,op.rectSize,op.rectSize);
ctx.fillStyle = op.color;
ctx.strokeStyle = op.strokeStyle || 'lightGray';
ctx.fill();
ctx.stroke();
op.position = { x: op.position.x+op.fontOffset.left + op.rectSize, y: op.position.y+op.fontOffset.top };
drawText(op);
}
if (options.flowOutInstructionOptions) {
var op = options.flowOutInstructionOptions;
ctx.beginPath();
ctx.rect(op.position.x, op.position.y, op.rectSize, op.rectSize);
ctx.fillStyle = op.color;
ctx.strokeStyle = op.strokeStyle || 'lightGray';
ctx.fill();
ctx.stroke();
op.position = { x: op.position.x + op.fontOffset.left + op.rectSize, y: op.position.y + op.fontOffset.top };
drawText(op);
}
//画主图区
ctx.beginPath();
ctx.lineWidth = 1;
ctx.rect(chartOptions.position.x, chartOptions.position.y,chartOptions.width,chartOptions.height);
ctx.strokeStyle = chartOptions.borderColor;
ctx.closePath();
ctx.stroke();
//画线
ctx.lineWidth = 1;
var lineCount = chartOptions.height / (chartOptions.itemHeight*2);
for (var i=1;i<lineCount ;i++) {
ctx.beginPath();
ctx.fillStyle = chartOptions.borderColor;
var x = chartOptions.position.x;
var y = chartOptions.position.y + i * chartOptions.itemHeight*2;
ctx.rect(x, y, chartOptions.width, 1);
ctx.closePath();
ctx.fill();
}
var verticalLines = [chartOptions.width / 4, chartOptions.width / 2, chartOptions.width / 4 * 3];
for (var i = 0; i < verticalLines.length; i++) {
ctx.beginPath();
ctx.fillStyle = chartOptions.borderColor;
ctx.rect(chartOptions.position.x + verticalLines[i], chartOptions.position.y, 1, chartOptions.height);
ctx.closePath();
ctx.fill();
}
//画线完毕
var items = data.items;
var max = 0;
for (var i = 0; i < items.length; i++) {
max = Math.max(max, items[i].flowIn);
max = Math.max(max, items[i].flowOut);
}
var flowInColor = options.flowInInstructionOptions.color;
var flowOutColor = options.flowOutInstructionOptions.color;
//底部成交额坐标
var txt = '0.00';
var top = chartOptions.height + chartOptions.position.y + 2;
ctx.fillStyle = 'black';
var txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt,chartOptions.width / 2 + chartOptions.position.x - txtWidth / 2, top);
txt = max.toFixed(2);
txtWidth = ctx.measureText(txt).width;
ctx.fillStyle = 'red';
ctx.fillText(txt, chartOptions.width + chartOptions.position.x - txtWidth, top);
txt = (max / 2).toFixed(2);
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, chartOptions.width * 3/4 + chartOptions.position.x - txtWidth/2, top);
txt = '-' + max.toFixed(2);
ctx.fillStyle = 'green';
ctx.fillText(txt, chartOptions.position.x, top);
txt = '-' + (max / 2).toFixed(2);
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, chartOptions.position.x + chartOptions.width / 4 - txtWidth /2, top);
function getY(index) { return chartOptions.position.y + (index * 2 + 1) * chartOptions.barSpace / 2 + index * chartOptions.barHeight; }
for (var i = 0; i < items.length; i++) {
var item = items[i];
var inLength = chartOptions.width / 2 * item.flowIn / max;
var outLength = chartOptions.width / 2 * item.flowOut / max;
var y = getY(i);
//资金流出
ctx.fillStyle = flowInColor;
ctx.beginPath();
ctx.rect(chartOptions.position.x + chartOptions.width / 2, y, inLength, chartOptions.barHeight);
ctx.closePath();
ctx.fill();
//资金流入
ctx.fillStyle = flowOutColor ;
ctx.beginPath();
ctx.rect(chartOptions.position.x + chartOptions.width / 2 - outLength, y, outLength, chartOptions.barHeight);
ctx.closePath();
ctx.fill();
if (i % 2 == 0) {
//左侧价格
var price = new Number(item.priceRanges.to).toFixed(2);
ctx.font = chartOptions.priceScalerTextFont;
var priceWidth = ctx.measureText(price).width;
var x = chartOptions.position.x - priceWidth + chartOptions.priceScalerOffset.right;
y = chartOptions.position.y + chartOptions.itemHeight * (i+1) - chartOptions.priceScalerFontHeight / 2 + chartOptions.priceScalerOffset.top;
ctx.fillStyle = chartOptions.priceScalerColor;
ctx.textBaseline = chartOptions.priceScalerTextBaseline;
ctx.fillText(price, x, y);
}
}
function getIndexByY(pointY) {
var index = Math.ceil((pointY - chartOptions.position.y) / (chartOptions.barSpace + chartOptions.barHeight)) - 1;
if (index >= items.length) return items.length - 1;
return index;
}
addCrossLinesAndTipEvents(canvas, {
getCrossPoint: function (ev) {
var dataIndex = getIndexByY(ev.offsetY);
var y = getY(dataIndex) + chartOptions.barHeight / 2;
return { x: ev.offsetX, y: y };
},
triggerEventRanges: { x: chartOptions.position.x, y: chartOptions.position.y, width: chartOptions.width, height: chartOptions.height },
tipOptions: {
getTipHtml: function (ev) {
var dataIndex = getIndexByY(ev.offsetY);
if (dataIndex >= 0 && dataIndex < items.length) {
var dataItem = items[dataIndex]
var flowInGreatThanOut = Math.abs(dataItem.flowIn) > Math.abs(dataItem.flowOut);
return '价格区间:' + dataItem.priceRanges.begin + ' - ' + dataItem.priceRanges.to + '(元)'
+ '<br/>流入资金:<font color="' + flowInColor + '">' + dataItem.flowIn + '(' + data.unit + ')</font>'
+ '<br/>流出资金:<font color="' + flowOutColor + '">' + dataItem.flowOut + '(' + data.unit + ')</font>'
+ '<br/>差 值:<font color="' + (flowInGreatThanOut ? flowInColor : flowOutColor) + '">'
+ (Math.abs(dataItem.flowIn) - Math.abs(dataItem.flowOut)).toFixed(2)
+ '</font>';
}
},
position: { x: chartOptions.position.x + chartOptions.width/6, y: false }, //position中的值是相对于canvas的左上角的
size: { width: 165, height: 100 },
opacity: 80,
cssClass: null,
offsetToPoint: 30
},
crossLineOptions: {
color: 'red'
}
});
}
//载入数据调用Painter画图
Ajax.get('dailyTradeAnalysis.xml',
function (client) {
if (client.readyState == 4 && (client.status == 200 || client.status == 304)) {
var dom = client.responseXML;
var titleNode = dom.getElementsByTagName('Data')[0].getElementsByTagName('Title')[0];
var title = titleNode.getAttribute('name');
var itemRootNode = dom.getElementsByTagName('Item')[0];
var itemTitleNode = itemRootNode.getElementsByTagName('Title')[0];
var unit = itemTitleNode.getAttribute('unit');
var itemNodes = itemRootNode.getElementsByTagName('Item');
var items = [];
for (var i = 0; i < itemNodes.length; i++) {
var itemNode = itemNodes[i];
var item = { priceRanges: { begin: itemNode.getAttribute('price1'), to: itemNode.getAttribute('price2') },
flowIn: itemNode.getAttribute('inflow'), flowOut: itemNode.getAttribute('outflow')
};
items.push(item);
}
var painter = new moneyFlowAnalysisPainter('canvas',
{
titleOptions: { position: { x: 10, y: 20 }, fontColor: 'black', font: 'Bold 12px Arial' },
priceUnitOptions: { position: { x: 24, y: 38 }, fontColor: 'black', font: '11px Arial', text: '(元)' },
flowInInstructionOptions: { position: { x: 180, y: 33 }, color: 'red', text: '流入性资金', font: '11px Arial', fontColor: 'black', rectSize: 12, textBaseline: 'top', fontOffset: {left:2,top:-2} },
flowOutInstructionOptions: { position: { x: 280, y: 33 }, color: 'green', text: '流出性资金', font: '11px Arial', fontColor: 'black', rectSize: 12, textBaseline: 'top', fontOffset: { left: 2, top: -2} },
chartOptions: { position: { x: 50, y: 50 }, width: 330, itemHeight: 9, barHeight: 5, barSpace: 4, borderColor: 'gray', lineColor: 'gray', minHeight: 200,
priceScalerTextFont: '11px Arial', priceScalerFontHeight: 11, priceScalerColor: 'black', priceScalerOffset: { right: -4, top: 2 }, priceScalerTextBaseline: 'alphabetic'
},
//成交单位选项
dealUnitOptions: { color: 'red', font: '11px Arial', fontColor: 'black', height: 30 }
},
{ title: title, unit: unit, items: items }
);
}
},'canvas');
</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>