关于柱状图大部分参数都写了.直接上代码
package liu.cn.ixj.util;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.RangeType;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
public class BarImage {
public static void main(String[] args) throws IOException {
DefaultCategoryDataset dateSet = new DefaultCategoryDataset();
dateSet.setValue(100,"好","苹果");
dateSet.setValue(10,"坏","苹果");
dateSet.setValue(100,"好","橘子");
dateSet.setValue(10,"坏","橘子");
dateSet.setValue(100,"好","大香蕉");
dateSet.setValue(10,"坏","大香蕉");
dateSet.setValue(100,"好","石榴");
dateSet.setValue(10,"坏","石榴");
dateSet.setValue(100,"好","榴莲");
dateSet.setValue(10,"坏","榴莲");
dateSet.setValue(100,"好","碧\n根\n果");
dateSet.setValue(10,"坏","碧\n根\n果");
dateSet.setValue(100,"好","大\n梨");
dateSet.setValue(10,"坏","大\n梨");
JFreeChart chart = ChartFactory.createBarChart("水果柱状图",//标题
"水果",//目录轴的显示标签
"单位:百万",//数值的显示标签
dateSet,//数据
PlotOrientation.VERTICAL,//图标方向 水平/垂直
true,//是否显示图例
false,//是否生成工具
false); //是否生成URL链接
CategoryPlot categoryPlot = chart.getCategoryPlot();//图部分
CategoryAxis domainAxis = categoryPlot.getDomainAxis();//X轴
/**
* 注释:若想字符竖着显示:
* 比如:
* 我
* 爱
* 中
* 国
* 在添加数据的时候这样:
* dateSet.setValue(10,"www.ixj.com","我\n爱\n中\n国");
*/
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);//X轴下标 90°显示
domainAxis.setMaximumCategoryLabelLines(10);//自动换行 最多显示多少行
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,20));//下标
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//X轴标题
ValueAxis rangeAxis = categoryPlot.getRangeAxis();//Y轴
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,20));//下标
rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//Y轴标题
NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
numberAxis.setAutoTickUnitSelection(false);//取消自动设置Y轴刻度
numberAxis.setTickUnit(new NumberTickUnit(10));//刻度大小
numberAxis.setAutoRangeStickyZero(true);//和下面一行搭配使用 设置Y轴都是正数
numberAxis.setRangeType(RangeType.POSITIVE);
numberAxis.setNumberFormatOverride(new DecimalFormat("0.00"));//设置Y轴上的数值精度
chart.getLegend().setItemFont( new Font("黑体",Font.BOLD,20));//图标字体
chart.getTitle().setFont( new Font("黑体",Font.BOLD,20));
BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();//图形修改
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.00")));//设置柱状图上的数值精度
renderer.setItemMargin(0);//设置柱子之间的距离
renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
renderer.setDefaultItemLabelFont(new Font("黑体",Font.BOLD,20));
renderer.setDrawBarOutline(false);
renderer.setMaximumBarWidth(0.4); //设置柱子宽度
renderer.setMinimumBarLength(0.00); //设置柱子高度
renderer.setDefaultItemLabelsVisible(true);//柱状体上的数值显示
File file = new File("E:/aaaa.png");
if (file.exists()){
file.delete();
file.createNewFile();
}
ChartUtils.saveChartAsPNG(file,chart,3000,1000);
}
}
直接上图:
爱雪瑾
20190228