jFreeChart图形显示设置(柱状图)
说明表
序号 1 构造JfreeChart图 JfreeChart 的各种图形都是由各种构造工厂根据数据源构造的; CategoryDataset 是数据源。 例如://3D柱状图 JferrChart chart = ChartFactory.createBarChart3D (getTitle(),//图形总标题 getXAxisTitle(), //X轴的标题 getYAxisTitle(), //y轴的标题 dcd, //数据源 PlotOrientation.VERTICAL,//未知 true, //是否显示图例 true, // false);// 2 Chart图的图例对象 LegendTitle 包括Chart图图例的所有属性,修改其属性就可设置图例显示的方式。 LegendTitle //取得Chart图的图例 LegendTitle legtitle = chart.getLegend(); 例如://设置图例字体 legtitle.setItemFont(new Font(\黑体\, Font.ROMAN_BASELINE, 15)); 名称 说明 3 Chart图总标题对象 TextTitle 为Chart图的总标题对象,可设置位置、字体、颜色 TextTitle 例如://取得chart图的标题 TextTitle title = chart.getTitle(); //设置chart图标题的字体 title.setFont(new Font(\黑体\, Font.CENTER_BASELINE, 24)); 4 图形参数设置对象 CategoryPlot CategoryPlot 包含chart图数据区的所有显示设置,由其内部各属性组成. 例如://获得chart图的图形设置对象 CategoryPlot plot = (CategoryPlot)chart.getPlot(); 5 Chart图 X轴对象 CategoryAxis CategoryAxis 是 CategoryPlot 对象的一个属性。包含图形区x轴(横轴)的各种属性,包括各项目字体、颜色、间距、对齐方式等等; 例如://取得X轴对象 CategoryAxis domainAxisTemp = plot.getDomainAxis(); //设置x轴标题的字体 domainAxisTemp .setLabelFont(new Font(\黑体\, Font.ROMAN_BASELINE, 12)); //设置各项目标题显示颜色 domainAxisTemp.setTickLabelPaint(Color.BLUE); 6 Chart图 Y轴对象 ValueAxis ValueAxis 是 CategoryPlot 对象的一个属性。包含图形区Y轴(横轴)的各种属性,包括标题字体、颜色、刻度粒度、最大刻度最小刻度等等; 例如://取得Y轴对象 ValueAxis rangeAxis = plot.getRangeAxis(); //Y轴标题字体 rangeAxis.setLabelFont(new Font(\黑体\, Font.ROMAN_BASELINE, 12)); 7 3D柱状图的设置对象 BarRenderer3D BarRenderer3D 是 CategoryPlot 的一个属性,该属性为一个接口Renderer。BarRenderer3D 是Renderer 的一个实现类。在这个类中包含对3D柱状图的数据区的显示设置。BarRenderer3D 含有3D背景的设置、柱体的设置、柱体间距的设置等等,例如: //3D柱状图显示设置对象 BarRenderer3D render3d = new BarRenderer3D(); // 3D坐标轴的墙体颜色 render3d.setWallPaint(Color.lightGray); // 柱体最大宽度 render3d.setMaximumBarWidth(0.05); // 显示每个柱的数值,并修改该数值的字体属性
custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); // //是否显示柱的数值标签 custombarrenderer3d.setBaseItemLabelsVisible(true); // 每个柱的数值,标签显示的方式及位置 custombarrenderer3d.setBasePositiveItemLabelPosition(new custombarrenderer3d.setBaseItemLabelFont(new Font(\黑体\custombarrenderer3d.setBaseItemLabelFont(new Font(\黑体\, Font.ROMAN_BASELINE, 12)); Font.CENTER_BASELINE, 15)); ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); custombarrenderer3d.setSeriesPaint(0,Color.getHSBColor(3.5f, // 设置柱的 Outline 颜色 custombarrenderer3d.setSeriesOutlinePaint(0, 2.3f, 4.5f)); // 标签显示与设定位置的距离 custombarrenderer3d.setItemLabelAnchorOffset(10D); // 设置每个基站所包含的平行柱的之间距离 custombarrenderer3d.setItemMargin(0.05D); // 设置柱的颜色 Color.BLACK); //为单个柱体设置字体,\为柱体的序号 custombarrenderer3d.setSeriesItemLabelFont(1, new Font(\// 宋体\ plot.setRenderer(custombarrenderer3d);
源代码:
//BarRenderer3D
JFreeChart chart = ChartFactory.createBarChart3D(getTitle(),
getXAxisTitle(), getYAxisTitle(), dcd,
PlotOrientation.VERTICAL, true, true, false);
private JFreeChart createChartPole(CategoryDataset dcd) {
// 构造基本的JFreeChart setTitle(Title); setXAxisTitle(x); setYAxisTitle(y);
CategoryPlot plot = (CategoryPlot)chart.getPlot(); BarRenderer3D custombarrenderer3d = new BarRenderer3D();
//设置图例
LegendTitle legtitle = chart.getLegend(); //设置图例字体
legtitle.setItemFont(new Font(\黑体\, Font.ROMAN_BASELINE, 15)); //取得chart图的标题
TextTitle title = chart.getTitle(); //设置chart图标题的字体
title.setFont(new Font(\黑体\, Font.CENTER_BASELINE, 24));
//X轴字体
CategoryAxis domainAxisTemp = plot.getDomainAxis();
domainAxisTemp.setLabelFont(new Font(\黑体\, Font.ROMAN_BASELINE,
//设置x轴各项标题的字体
domainAxisTemp.setTickLabelFont(new Font(\黑体\, domainAxisTemp.setTickLabelPaint(Color.BLUE); //Y轴字体
ValueAxis rangeAxis = plot.getRangeAxis();
12));
Font.ROMAN_BASELINE, 12));
rangeAxis.setLabelFont(new Font(\黑体\, Font.ROMAN_BASELINE,
custombarrenderer3d.setBaseOutlinePaint(Color.blue);
custombarrenderer3d.setWallPaint(Color.lightGray);// 3D坐标轴的
12));
//
墙体颜色 custombarrenderer3d.setMinimumBarLength(0.01);
chart.getCategoryPlot().getDomainAxis().setCategoryLabelPositions
设置坐标点标记与坐标轴之间的距离
custombarrenderer3d.setMaximumBarWidth(0.05);
//控制X轴坐标点文字角度 当X轴的坐标点超过15个时,文字角度调整为45度向上 if(dcd.getColumnCount() > 8){
(CategoryLabelPositions.DOWN_45); // //
jfreechart.getCategoryPlot().getDomainAxis().setCategoryLabelPosi
tionOffset(50); //
jf.getCategoryPlot().getDomainAxis().setCategoryLabelPositions(Ca
}
// 显示每个柱的数值,并修改该数值的字体属性
tegoryLabelPositions.createUpRotationLabelPositions(Math.PI/6));
custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //
custombarrenderer3d.setBaseItemLabelFont(new Font(\黑体\custombarrenderer3d.setBaseItemLabelFont(new Font(\黑体\,
Font.ROMAN_BASELINE, 12)); Font.CENTER_BASELINE, 15));
//是否显示柱的数值标签
custombarrenderer3d.setBaseItemLabelsVisible(true); // 每个柱的数值,标签显示的方式及位置
custombarrenderer3d.setBasePositiveItemLabelPosition(new
ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
// 标签显示与设定位置的距离
custombarrenderer3d.setItemLabelAnchorOffset(10D);
// 设置每个基站所包含的平行柱的之间距离
custombarrenderer3d.setItemMargin(0.05D); // 设置柱的颜色
custombarrenderer3d.setSeriesPaint(0,Color.getHSBColor(3.5f, // 设置柱的 Outline 颜色
custombarrenderer3d.setSeriesOutlinePaint(0, Color.BLACK); //为单个柱体设置字体,\为柱体的序号
custombarrenderer3d.setSeriesItemLabelFont(1, new Font(\宋体\
// 各柱体的宽度
custombarrenderer3d.setMaximumBarWidth(0.050000000000000003D);
plot.setRenderer(custombarrenderer3d);
2.3f, 4.5f));
//
}
Font.CENTER_BASELINE, 72));
return chart;