-
Notifications
You must be signed in to change notification settings - Fork 0
/
moneyFlow.html
142 lines (131 loc) · 6.23 KB
/
moneyFlow.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
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<script src="libs/loading.js" type="text/javascript"></script>
<script src="libs/ajax.js" type="text/javascript"></script>
<title>资金流向</title>
</head>
<body>
<canvas id="canvas" width="200" height="200" style="border:1px solid #69c"></canvas>
<script type="text/javascript">
//持仓比例图
function moneyFlowPainter(canvasId, options) {
this.canvas = document.getElementById(canvasId);
var canvas = this.canvas;
if (this.canvas && this.canvas.getContext) {
var ctx = this.canvas.getContext('2d');
var rate = options.rate;
var rotateTo = Math.PI * 4 / 5 * rate / 100;
var rotateStep = rotateTo / 60;
var me = this;
var currentRotate = rotateStep;
me.intervalHandler = setInterval(function () {
ctx.save();
//现在center是原点
var radius = options.radius || Math.min(canvas.width, canvas.height) / 2;
var center = options.center || { x: canvas.width/2, y: canvas.height/2 };
ctx.translate(center.x, center.y);
center = { x: 0, y: 0 };
//画出背景
var startAngle = Math.PI * 1.4;
var endAngle = startAngle + Math.PI / 5;
//顶部扇形区域
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.arc(center.x, center.y, radius, startAngle, endAngle);
ctx.closePath();
var color = options.normalColor || 'rgb(255,204,51)'; ;
ctx.fillStyle = color;
ctx.fill();
//大量流入扇形区
ctx.shadowColor = "lightgray";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 3;
ctx.shadowOffsetY = 3;
startAngle = endAngle;
endAngle += Math.PI / 10 * 7;
color = 'rgb(204,51,51)';
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.arc(center.x, center.y, radius, startAngle, endAngle);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
//大量流出扇形区
ctx.shadowColor = null;
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
startAngle = Math.PI / 10 * 7;
endAngle = Math.PI * 1.4;
color = 'rgb(0,153,51)'; //'green';
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.arc(center.x, center.y, radius, startAngle, endAngle);
ctx.closePath();
ctx.shadowOffsetX = 0;
ctx.fillStyle = color;
ctx.fill();
//画白线
var step = Math.PI * .1;
var start = Math.PI * .7;
var end = Math.PI * 2.3;
for (; start < end; start += step) {
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.lineTo(center.x + radius * Math.cos(start), center.y + radius * Math.sin(start));
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.closePath();
ctx.stroke();
}
//画百分比文字
var textPoint = { x: 0, y: radius / 2 };
ctx.fillStyle = options.fontColor || 'black';
ctx.font = options.font || '12px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(rate + '%', textPoint.x, textPoint.y);
//画指针
ctx.rotate(currentRotate);
ctx.moveTo(0, 0);
ctx.lineTo(-3, -3);
ctx.lineTo(0, 0 - radius);
ctx.lineTo(3, -3);
ctx.closePath();
ctx.strokeStyle = 'gray';
ctx.fillStyle = '#333333';
ctx.stroke();
ctx.fill();
ctx.restore();
var wiseClock = rotateStep > 0;
if (wiseClock) {
if (currentRotate < rotateTo) currentRotate += rotateStep;
else clearInterval(me.intervalHandler);
} else {
if (currentRotate > rotateTo) currentRotate += rotateStep;
else clearInterval(me.intervalHandler);
}
}, 20);
}
}
var dataUrl = 'moneyFlow.xml';
Ajax.get(dataUrl,function(client){
var xml = client.responseXML;
var rate = xml.getElementsByTagName('Item')[0].getElementsByTagName('Item')[0].getAttribute('rate');
var painter = new moneyFlowPainter('canvas', { rate: rate, font: '12px Arial', fontColor: (rate>0?'red':(rate==0?'black':'green')), radius: 60 });
},'canvas',true);
//var rate = 50;
//var painter = new moneyFlowPainter('canvas', { rate: rate, font: '12px Arial', fontColor: (rate > 0 ? 'red' : (rate == 0 ? 'black' : 'green')), radius: 60 });
</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>