|
在这里要注意,如果柱子上的数字位数过大,而图的宽度又不够,数字就不会显示了。
要注意调整~
参考代码:
List<UserCount> list = manager.getTotalStatus(region.getId(), 0);
double[][] data = new double[2][7];
String[] rowKeys =
{ "能瀪母猪", "奶牛" };
String[] columnKeys =
{ "承保员", "录入员", "查勘员", "理算员", "核赔员", "审核员", "管理员" };
// 填充数据源
for (int i = 0; i < list.size(); i++)
{
UserCount uc = list.get(i);
// id为0为统计记录
if (uc.getRegionId() == 0)
{
data[0][0] = uc.getPigUnderWriter();
data[0][1] = uc.getPigRecorder();
data[0][2] = uc.getPigSurveyor();
data[0][3] = uc.getPigClaimer();
data[0][4] = uc.getPigVerifier();
data[0][5] = uc.getPigAssessor();
data[0][6] = uc.getPigAdmin();
data[1][0] = uc.getOxUnderWriter();
data[1][1] = uc.getOxRecorder();
data[1][2] = uc.getOxSurveyor();
data[1][3] = uc.getOxClaimer();
data[1][4] = uc.getOxVerifier();
data[1][5] = uc.getOxAssessor();
data[1][6] = uc.getOxAdmin();
break;
}
}
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("人员总体状况图表", "",
"数量(人)", dataset, PlotOrientation.VERTICAL, true, false, false);
// 背景色
chart.setBackgroundPaint(Color.WHITE);
// 设置标题内容及样式
chart
.setTitle(new TextTitle("人员总体状况图表", new Font("黑体", Font.BOLD,
14)));
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.LIGHT_GRAY);
plot.setRangeGridlinePaint(Color.black);
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
// 设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置 Wall 的颜色
renderer.setWallPaint(Color.LIGHT_GRAY);
// 设置每种角色代表的柱的颜色
// 对于自定义颜色也可以用 Color.decode("#124f21");
renderer.setSeriesPaint(0, new Color(255, 102, 0));
renderer.setSeriesPaint(1, Color.YELLOW);
// 设置每种角色代表的柱的 Outline 颜色
renderer.setSeriesOutlinePaint(0, Color.BLACK);
renderer.setSeriesOutlinePaint(1, Color.BLACK);
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
// 设置是否显示数字(柱子上)
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// 柱子上字体的样式
renderer.setBaseItemLabelFont(new Font("黑体", Font.PLAIN, 9));
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
// 设置柱的透明度
plot.setForegroundAlpha(0.9f);
// 设置地区、数量的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
Random r = new Random();
this.imagePath = "/images/chart/userCountBarChart" + r.nextInt()
+ ".png";
String path = this.getFullPath() + imagePath;
ChartUtilities.saveChartAsPNG(new File(path), chart, 700, 300);
|