对比习惯于JFreeChart开发的人,这家伙显得简单、明了。
ChartDirector画饼状图方法非常简单,只需简单的几个部署,下面是代码:
Java
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
|
// 设置Chart大小、背景色
PieChart pc =
new
PieChart(
750
,
500
,
0xddddff
,-
1
,
1
);
pc.setPieSize(
390
,
245
,
150
);
// 设置图表标题
// 只要将文字转成UTF-8编码就可以解决中文乱码问题
// 另外如果你将程序部署到Linux下就需要copy相应的字体文件,比如simsun.ttc文件到/usr/share/font下
TextBox t =
null
;
pc.setDefaultFonts(
"SIMSUN.TTC"
,
"simhei.ttf"
);
t = pc.addTitle(rb.get(
"REPORT_TOTAL_PURCHASED"
),
""
,
14
);
// 中文就从resource.properties中取吧
t.setBackground(
0xaaaaff
);
// 字体也要有背景的
t.setBackground(
0xaaaaff
,
0xaaffaa
);
// 需要加边框吗?加上第二个参数就可以了
t.setHeight(
30
);
StringBuffer sb =
new
StringBuffer(RetrainingConstant.get(
"total_join"
))
.append(data[data.length-
1
]).append(rb.get(
"rein"
));
TextBox total = pc.addText(
10
,
35
, sb.toString());
// 加个文字吧,随便显示什么都行了
total.setFontColor(
0x000000
);
total.setFontSize(
10
);
double
[] data =
new
double
[]{
100
,
90
,
80
,
70
};
String[] labels =
new
String[]{
"ColumnA"
,
"ColumnB"
,
"ColumnC"
,
"ColumnD"
};
pc.setData(dt,labels);
// 放数据
double
[] depths1 = {
45
,
10
,
35
,
20
,
25
,
30
,
15
,
40
};
pc.set3D2(depths1);
pc.setLabelLayout(Chart.SideLayout);
// 每一块饼都得给个说明
TextBox t1 = pc.setLabelStyle();
t1.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect());
t1.setRoundedCorners(
5
);
pc.setLineColor(Chart.SameAsMainColor,
0x000000
);
pc.setStartAngle(
225
);
// 最终的img.src
getRequest().setAttribute(
"chartURL"
, pc.makeSession(getRequest(),
"chartURL"
));
// 链接的href,其中变量{dataSetName}和{value}分别对应了data和lables中的值
getRequest().setAttribute(
"imageMap"
,pc.getHTMLImageMap(
"searchStudents.html"
,
"chartType=5&subject={dataSetName}"
,
"title='{value}人'"
));
// 至此,饼图就被搞定了~~
|