forked from pumpinger/schartjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2.html
237 lines (198 loc) · 9.27 KB
/
demo2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./js/snap.svg.js"></script>
<script src="./js/jquery-1.4.4.js"></script>
<style>
svg {border: 1px solid red;}
</style>
</head>
<body>
<svg id="svg" viewbox="0 0 640 520" data="0 0 640 520"></svg>
<script type="text/javascript">
/* 传的就是一个对象options = {
title:{'text':'一级标题','subtitle':'二级标题',x:'',y:''},
legend:{'orient':'',x:'',y:'',data:''}, //'horizontal' | 'vertical' 水平或者垂直
tooltip:{formatter:''},
series:{
radius:'',
center:['',''],
data:[{value:'',name:'',sales:''},{},{}]
}
}
}
*/
//画饼的对象
function drawPie(options,callback){
var svg = Snap(options.id); //svg对象
console.log(svg.node.clientWidth);
var docSvg = $(options.id);
docSvg.attr('position','relative');
var viewObj = svg.attr('viewBox');
var viewObj2 = getRadiusCenter(viewObj,options);
var pieData = options.series.data; //数据数组对象
var circleCenterXY = [viewObj2.centerX,viewObj2.centerY];//圆心
var circleCenterX = viewObj2.centerX;
var circleCenterY = viewObj2.centerY;
var radius = viewObj2.radius;//半径
var pieEdgeXY = new Array(new Array(2)); //画到某个坐标点
pieEdgeXY[0][0] = circleCenterX - radius;
pieEdgeXY[0][1] = circleCenterY;
var motionToXY = new Array(new Array(2)); //点击移动后的坐标
var pathTag = []; //画图的参数
var largeArcFlag = 0;//<180 == 0 >180 == 1
var sweepFlag = 1; //1-顺时针 0-逆时针
var totalValue = 0;//销售数量总和总和
var totalSale = 0;//销售金额总和
var currentPercent = 0;//当前百分比
var perPiePercent = [];//每个画饼的百分比
var piePopup = [];//点击弹出后的标志符false/true
var textXY = []; //文字显示在画饼中的数组
//tooltip对象
var tooltip = '';
if(options.tooltip.show){
tooltip = $('<div>',{id:'pie_tooltip'});
tooltip.css('width','150px').css('height','50px').css('opacity','0.6').css('border-radius','4px;').css('background-color','black').hide();
$('body').append(tooltip);
}
//颜色自定义集合--- 1.按照顺序来 2.数组中随机取,但不能重复
//计算lengend
var ulObj = '';
var divObj = '';
if(options.legend.origent == 'horizontal') {
ulObj = $('<ul>',{'id':'pie'});
ulObj.css('position','absolute').css('list-style-type','none').css('font-size',0.075*circleCenterX).css('color','gray').css('font-weight','bold')
.css('top',parseInt(options.legend.top)/100*circleCenterX+'px').css('right',parseInt(options.legend.right)/100*circleCenterY+'px');
docSvg.after(ulObj);
divObj = $('<div>',{'id':'pie2','style':'width:'+0.8*circleCenterX+'px;height:'+0.2*circleCenterY+'px;position:absolute;'});
docSvg.after(divObj);
}
//计算销量总和 销售金额总和
for(var i=0;i<pieData.length;i++){
totalValue+=pieData[i]['value'];
totalSale+=pieData[i]['sale'];
}
//计算每个画饼的百分比
for(var i =0;i<pieData.length;i++){
perPiePercent[i] = new Number((pieData[i]['value']/totalValue).toFixed(4));
paintPie(i);
}
//画文字
for(var i =0;i<options.showNumber;i++){
svg.paper.text(textXY[i][0],textXY[i][1],pieData[i]['name']).attr({fill:'#FCFCFC',fontSize:'12px'});
}
//计算圆心
function getRadiusCenter(viewObj,options){
var centerX = parseInt(options.series.center[0])*(viewObj.width-viewObj.x)/100;
var centerY = parseInt(options.series.center[1])*(viewObj.height-viewObj.y)/100;
var radius = '';
if(viewObj.width>viewObj.height){
radius = viewObj.height * parseInt(options.series.radius)/100;
}else{
radius = viewObj.width * parseInt(options.series.radius)/100;
}
return {centerX:centerX,centerY:centerY,radius:radius};
}
//画饼
function paintPie(index){
piePopup[index] = false;
largeArcFlag = isLargeArc(perPiePercent[index]);
currentPercent += perPiePercent[index];
pieEdgeXY[index+1] = [(circleCenterX - radius * Math.cos(2 * Math.PI * currentPercent)),(circleCenterY - radius * Math.sin(2 * Math.PI * currentPercent))];
var tempPercent = currentPercent - perPiePercent[index]/2.0;
motionToXY[index] = [(circleCenterX - 20 * Math.cos(2 * Math.PI * tempPercent)-circleCenterXY[0]),(circleCenterY - 20 * Math.sin(2 * Math.PI * tempPercent)-circleCenterXY[1])];
//画饼的路径
pathTag[index] = "M " + circleCenterXY[0] + " " + circleCenterXY[1]+ "L " + pieEdgeXY[index][0] + " " + pieEdgeXY[index][1] + " "+ "A " + radius + " " + radius + " " + 0 + " " + largeArcFlag + " "+ sweepFlag + " " + pieEdgeXY[index+1][0] + " " + pieEdgeXY[index+1][1] + " "+ " Z";
//画饼文字坐标
textXY[index] = [(circleCenterX*(1-0.04) - radius* Math.cos(2 * Math.PI * tempPercent)/2),(circleCenterY - radius* Math.sin(2 * Math.PI * tempPercent)/2)];
//随机颜色
var red = Math.round(Math.random() * 255);
var green = Math.round(Math.random() * 255);
var blue = Math.round(Math.random() * 255);
var color = "rgb(" + red + "," + green + "," + blue +")";
svg.paper.path(pathTag[index]).attr({fill:color,stroke:'#FCFCFC',storkeWidth:'3px'}).click(function(event){
if(!piePopup[index]){
piePopup[index] = true;
this.animate({d:"M " + (circleCenterXY[0]+motionToXY[index][0]) + " " + (circleCenterXY[1]+motionToXY[index][1])+ "L " + (pieEdgeXY[index][0]+motionToXY[index][0]) + " " + (pieEdgeXY[index][1]+motionToXY[index][1]) + " "+ "A " + radius + " " + radius + " " + 0 + " " + largeArcFlag + " "+ sweepFlag + " " + (pieEdgeXY[index+1][0]+motionToXY[index][0]) + " " + (pieEdgeXY[index+1][1]+motionToXY[index][1]) + " "+ " Z"},500,mina.easeinout,function(){});
}else{
piePopup[index] = false;
this.animate({d:"M " + (circleCenterXY[0]) + " " + (circleCenterXY[1])+ "L " + (pieEdgeXY[index][0]) + " " + (pieEdgeXY[index][1]) + " "+ "A " + radius + " " + radius + " " + 0 + " " + largeArcFlag + " "+ sweepFlag + " " + (pieEdgeXY[index+1][0]) + " " + (pieEdgeXY[index+1][1]) + " "+ " Z"},
500,mina.easeinout,function(){});
}
callback();
}).mousemove(function(event){
if(options.tooltip.show){
var coor = getMouseXY(event);
tooltip.show();
tooltip.css('position','absolute').css('top',(coor[1]-20)+'px').css('left',(coor[0]+20)+'px').css('color','#FCFCFC').css('font-size','12px').css('padding','10px');
tooltip.html(pieData[index]['name']+'<br>百分比:'+(perPiePercent[index].toFixed(4))*100+'%');
}
});
if(options.legend.origent == 'horizontal'){
if(!options.legend.data){
ulObj.append("<li style='margin-bottom:20px;'><span style='display:inline-block;width:"+0.05*circleCenterX+"px;height:"+0.05*circleCenterX+"px;background-color:"+color+"'></span> <sapn>"+pieData[index]['name']+"</span></li>");
divObj.append("<div style='width:33%;display:inline-block;'><span style='display:inline-block;width:"+0.05*circleCenterX+"px;height:"+0.05*circleCenterX+"px;background-color:"+color+"'></span> <sapn>"+pieData[index]['name']+"</span></div>");
}else{
for(var j=0;j<options.legend.data.length;j++){
if(options.legend.data[j] == pieData[index]['name']){
ulObj.append("<li style='margin-bottom:20px;'><span style='display:inline-block;width:"+0.05*circleCenterX+"px;height:"+0.05*circleCenterX+"px;background-color:"+color+"'></span> <sapn>"+pieData[index]['name']+"</span></li>");
divObj.append("<div style='width:33%;display:inline-block;'><span style='display:inline-block;width:"+0.05*circleCenterX+"px;height:"+0.05*circleCenterX+"px;background-color:"+color+"'></span> <sapn>"+pieData[index]['name']+"</span></div>");
}
}
}
}
}
//判断是否是大图或小图
function isLargeArc(percent) {
if(percent >= 0.5) {
return 1;
}
return 0;
}
//得到鼠标的坐标x,y
function getMouseXY(event) {
var x = event.pageX || (event.clientX
+ (document.documentElement.scrollLeft
|| document.body.scrollLeft
)
);
var y= event.pageY || (event.clientY
+ (document.documentElement.scrollTop
|| document.body.scrollTop
)
);
return [x, y];
}
}
options = {
id:'#svg',
legend:{
origent:'horizontal',
top:'50%',
right:'10%',
//data:['上海市','北京市']
},
series:{
radius:'30%',
center:['50%','30%'],
data:[{value:330,name:'上海市',sale:20000},
{value:260,name:'北京市',sale:30000},
{value:180,name:'天津市',sale:10000},
{value:100,name:'深圳市',sale:40000},
{value:120,name:'南京市',sale:60000}
]
},
tooltip:{
show:false
},
showNumber:3 //显示前面多少位数据的名字
};
drawPie(options,show);
function show(isClick){
console.log('1111');
}
</script>
</body>
</html>