Java动态生成SVG饼图与JFreeChart超强效能生成SVG图表.

SVG生成饼图,固然代码兑现,但是感觉太复杂,太消耗时间,还倒不如用JFreeChart,

但是听同人说,JfreeChart很多Bug,有时还需要批改它的源代码,哎.网上找打了JfreeChart生成SVG图形步骤,当然也作出了个Demo.我对JfreeChart研究不深,同人谈道:这个货色,不是一会就能研究出来的,要学好,嘿嘿,也得2年吧.呜呜..

自我感觉没那么难吧,不就是学个他的API呀.缓缓探寻呗.何况开源的这货色,网上资源很多相干的.

网上看到有用JAVA生成SVG线条图的,再有一个是柱状图的.

自个儿写了个饼图的,感觉实在是劳驾.尤甚我在生成饼图的立体成效时分,忧闷了许久.

Jfreechart自身不能生成SVG图形,但是可以凭借此外一个货色,匡助生成.好像是这个:batik ,不会用网上用它做基本词,搜下即可



下部诀别贴出一些代码:

1、java生成svg饼图.平面的.

Java代码
一.package com.xj.svg;
2.
三.import java.io.File;
四.import java.io.FileOutputStream;
5.
6.
七.public class CakySvg {
8. static String[] colors ={ "#f二e692", "#fef195", "#ce九a31",
9. "#22FF22", "#aaffaa", "green",
10. "#799AE一", "#九aabEe", "#三e941b",
11. "#f二e692", "#66cc00", "#297110",
12. "#d六a97b", "#82522b", "#aaaaff",
13. "#ff2222", "#ffaaaa", "#aa1111" };
14. static String initialize(double [] fenshu){
15. StringBuffer sfile = new StringBuffer();
16. sfile.append("

");
17. sfile.append("/n");
18. sfile.append("19. sfile.append("/n");
20. sfile.append("xmlns='http://www.w三.org/2000/svg'");
21. sfile.append("/n");
22. sfile.append("xmlns:xlink='http://www.w三.org/1999/xlink'");
23. sfile.append("/n");
24. sfile.append("xml:space='default'");
25. sfile.append("/n");
26. sfile.append("version='1.1' width='100%' height='100%' viewBox='零 零 2024 570'>");
27. sfile.append("/n");
28. sfile.append("");
29. sfile.append("/n");
30. sfile.append("");
31. sfile.append("/n");
32. //循环创办path标签.
33. String path =creatPath(502, 300, 300, fenshu);
34. sfile.append(path);
35. sfile.append("");
36. sfile.append("/n");
37. sfile.append("");
38. return sfile.toString();
39. }
40.
41. public static String creatPath(double x零,double y零,double r,double[]fenshu){
42. StringBuffer sfile =new StringBuffer();
43. double x一=零;
44. double y一=零;
45. double middleX= 零;
46. double middleY=零;
47. double radian =零;
48. double textRadian=零;
49. double k=零;
50. for(int i=零;i51. if(i==零){
52. radian =getRadian(fenshu[零]);
53. textRadian=radian/二;
54. x一 = (x零+getCos(radian)*r);
55. y一 = (y零-getSin(radian)*r);
56. middleX=(x零+getCos(textRadian)*r);
57. middleY=(y零-getSin(textRadian)*r);
58. double percent = Math.round(fenshu[零]*100)/100.0;
59. k=Math.abs((middleY-y零)/(middleX-x零));
60. double sita= Math.atan(k);
61. double lineLen=50;
62. double textLen=70;
63. if(radian<六){
64. lineLen=90;
65. textLen=110;
66. }
67. if((textRadian<(Math.PI/二))){
68. sfile.append("");
69. sfile.append("/n");
70. sfile.append(""+percent+"%");
71. }else if ((textRadian>(Math.PI/二)&&textRadian72. sfile.append("");
73. sfile.append("/n");
74. sfile.append(""+percent+"%");
75. }else if ((textRadian>(Math.PI)&&textRadian<(Math.PI*3/2))){
76. sfile.append("");
77. sfile.append("/n");
78. sfile.append(""+percent+"%");
79. }else if((textRadian>(Math.PI*3/2)&&textRadian<(Math.PI*二))){
80. sfile.append("");
81. sfile.append("/n");
82. sfile.append(""+percent+"%");
83. }
84. sfile.append("/n");
85. if(getRadian(fenshu[零])>Math.PI){
86. sfile.append("");
87. }else{
88. sfile.append("");
89. }
90. sfile.append("/n");
91. }else{
92. textRadian = radian+(getRadian(fenshu[i])/二);
93. radian =radian+getRadian(fenshu[i]);//弧度.
94. System.out.println("弧度:"+radian);
95. middleX=(x零+getCos(textRadian)*r);
96. middleY=(y零-getSin(textRadian)*r);
97. double percent = Math.round(fenshu[i]*100)/100.0;
98. k=Math.abs((middleY-y零)/(middleX-x零));
99. double lineLen=50;
100. double textLen=70;
101. if(radian<六){
102. lineLen=90;
103. textLen=110;
104. }
105. double sita= Math.atan(k);
106. if((textRadian<(Math.PI/二))){
107. sfile.append("");
108. sfile.append("/n");
109. sfile.append(""+percent+"%");
110. }else if ((textRadian>(Math.PI/二)&&textRadian111. sfile.append("");
112. sfile.append("/n");
113. sfile.append(""+percent+"%");
114. }else if ((textRadian>(Math.PI)&&textRadian<(Math.PI*3/2))){
115. sfile.append("");
116. sfile.append("/n");
117. sfile.append(""+percent+"%");
118. }else if((textRadian>(Math.PI*3/2)&&textRadian<(Math.PI*二))){
119. sfile.append("");
120. sfile.append("/n");
121. sfile.append(""+percent+"%");
122. }
123. sfile.append("/n");
124. // 参数 一 示意 画大于180的弧, 零 示意画小于180的弧
125. if(getRadian(fenshu[i])>Math.PI){
126. sfile.append("");
127. }else{
128. sfile.append("");
129. }
130. sfile.append("/n");
131. }
132.
133. }
134. return sfile.toString();
135. }
136. //回来弧度
137. public static double getRadian(double fenshu){
138. return (fenshu*Math.PI)/50;
139. }
140. //回来正弦
141. public static double getSin(double radian){
142. return Math.sin(radian);
143. }
144.
145. //回到余弦
146. public static double getCos(double radian){
147. return Math.cos(radian);
148. }
149.
150. public static void main(String[] args) {
151. double[] data= {1000,320,410,200,990,430,400,300,340,423,243,200,430,210};
152. create(data);
153. }
154. private static void create(double[] data) {
155. try {
156. createSVG("f:/a.svg",getPercent(data));
157. } catch (Exception e) {
158. e.printStackTrace();
159. }
160. }
161. private static double[] getPercent(double data[]){
162. double sum=零;
163. double percents[] = new double[data.length];
164. for(int i=零;i165. sum+=data[i];
166. }
167. for(int i=零;i168. percents[i] =(data[i]/sum)*100;
169. }
170. return percents;
171. }
172. public static void createSVG(String fileRealPath, double[] percents) throws Exception {
173. String sFile = initialize(percents);
174. try {
175. byte[] byteFil = sFile.getBytes("UTF-八");
176. File svgFile = new File(fileRealPath);
177. if (svgFile.exists()) {
178. svgFile.delete();
179. }
180. FileOutputStream fos = new FileOutputStream(svgFile);
181. fos.write(byteFil);
182. fos.close();
183. } catch (Exception ex) {
184. System.out.print(ex.getMessage());
185. }
186. }
187.}
package com.xj.svg;

import java.io.File;
import java.io.FileOutputStream;


public class CakySvg {
static String[] colors ={ "#f二e692", "#fef195", "#ce九a31",
"#22FF22", "#aaffaa", "green",
"#799AE一", "#九aabEe", "#三e941b",
"#f二e692", "#66cc00", "#297110",
"#d六a97b", "#82522b", "#aaaaff",
"#ff2222", "#ffaaaa", "#aa1111" };
static String initialize(double [] fenshu){
StringBuffer sfile = new StringBuffer();
sfile.append("

");
sfile.append("/n");
sfile.append(" sfile.append("/n");
sfile.append("xmlns='http://www.w三.org/2000/svg'");
sfile.append("/n");
sfile.append("xmlns:xlink='http://www.w三.org/1999/xlink'");
sfile.append("/n");
sfile.append("xml:space='default'");
sfile.append("/n");
sfile.append("version='1.1' width='100%' height='100%' viewBox='零 零 2024 570'>");
sfile.append("/n");
sfile.append("");
sfile.append("/n");
sfile.append("");
sfile.append("/n");
//循环创办path标签.
String path =creatPath(502, 300, 300, fenshu);
sfile.append(path);
sfile.append("");
sfile.append("/n");
sfile.append("");
return sfile.toString();
}

public static String creatPath(double x零,double y零,double r,double[]fenshu){
StringBuffer sfile =new StringBuffer();
double x一=零;
double y一=零;
double middleX= 零;
double middleY=零;
double radian =零;
double textRadian=零;
double k=零;
for(int i=零;i if(i==零){
radian =getRadian(fenshu[零]);
textRadian=radian/二;
x一 = (x零+getCos(radian)*r);
y一 = (y零-getSin(radian)*r);
middleX=(x零+getCos(textRadian)*r);
middleY=(y零-getSin(textRadian)*r);
double percent = Math.round(fenshu[零]*100)/100.0;
k=Math.abs((middleY-y零)/(middleX-x零));
double sita= Math.atan(k);
double lineLen=50;
double textLen=70;
if(radian<六){
lineLen=90;
textLen=110;
}
if((textRadian<(Math.PI/二))){
sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}else if ((textRadian>(Math.PI/二)&&textRadian sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}else if ((textRadian>(Math.PI)&&textRadian<(Math.PI*3/2))){
sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}else if((textRadian>(Math.PI*3/2)&&textRadian<(Math.PI*二))){
sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}
sfile.append("/n");
if(getRadian(fenshu[零])>Math.PI){
sfile.append("");
}else{
sfile.append("");
}
sfile.append("/n");
}else{
textRadian = radian+(getRadian(fenshu[i])/二);
radian =radian+getRadian(fenshu[i]);//弧度.
System.out.println("弧度:"+radian);
middleX=(x零+getCos(textRadian)*r);
middleY=(y零-getSin(textRadian)*r);
double percent = Math.round(fenshu[i]*100)/100.0;
k=Math.abs((middleY-y零)/(middleX-x零));
double lineLen=50;
double textLen=70;
if(radian<六){
lineLen=90;
textLen=110;
}
double sita= Math.atan(k);
if((textRadian<(Math.PI/二))){
sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}else if ((textRadian>(Math.PI/二)&&textRadian sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");
}else if ((textRadian>(Math.PI)&&textRadian<(Math.PI*3/2))){
sfile.append("");
sfile.append("/n");
sfile.append(""+percent+"%");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值