1、先引入对应的依赖
<!--用于jfreechart生成图片 -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.17</version>
</dependency>
2、生成柱状图
/**
* 生成柱状图
* @return
*/
public static String DefaultCategoryDataset(){
// List<T> list
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
//添加数据
dataset.addValue(20, "", "瑞安市*");
dataset.addValue(35, "", "三位一体");
dataset.addValue(10, "", "司鉴院都好好的");
dataset.addValue(38, "", "方法反反复复");
dataset.addValue(15, "", "瑞安市*1");
dataset.addValue(23, "", "三位一体1");
dataset.addValue(3, "", "司鉴院都好好的1");
dataset.addValue(78, "", "方法反反复复1");
JFreeChart chart = ChartFactory.createBarChart3D(
"数据支撑业务",
"",//X轴的标签
"",//Y轴的标签
dataset, //图标显示的数据集合
PlotOrientation.VERTICAL, //图像的显示形式(水平或者垂直)
false,//是否显示子标题 就是图例 显示的话需要设置为true把56、99、100行代码取消注释
false,//是否生成提示的标签
true); //是否生成URL链接
//处理图形上的乱码
//处理主标题的乱码
chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));
//处理子标题乱码
// chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,15));
//获取图表区域对象
CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();
//获取X轴的对象
CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();
// 设置X轴文本倾斜45度
categoryAxis3D.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//获取Y轴的对象
NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();
//处理X轴上的乱码
categoryAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15));
//处理X轴外的乱码
categoryAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴上的乱码
numberAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴外的乱码
numberAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴上显示的刻度,以10作为1格
numberAxis3D.setAutoTickUnitSelection(false);
NumberTickUnit unit = new NumberTickUnit(10);
numberAxis3D.setTickUnit(unit);
//获取绘图区域对象
BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();
//设置柱形图的宽度
barRenderer3D.setMaximumBarWidth(0.03);
//在图形上显示数字
barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// 设置为false柱子上就不显示数字
barRenderer3D.setBaseItemLabelsVisible(false);
barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15));
//设定柱子上面的颜色
CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
customBarRenderer.setSeriesPaint(0, Color.decode("#005AB5"));
// 设置背景透明度
plot.setBackgroundAlpha(0.1f);
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.gray);
// 设置网格横线大小
plot.setDomainGridlineStroke(new BasicStroke(0.2F));
plot.setRangeGridlineStroke(new BasicStroke(0.2F));
// 设置图例展示位置
// LegendTitle legend = chart.getLegend();
// legend.setPosition(RectangleEdge.RIGHT);
//bar实例化
BarRenderer renderer = (BarRenderer) plot.getRenderer();
//设置条形图上bar是否显示背景阴影
renderer.setDrawBarOutline(false);
//在D盘目录下生成图片
File file = new File("D:/数据支撑业务.jpeg");
try {
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
System.out.println(getImageBase(file.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
//使用ChartFrame对象显示图像
/*ChartFrame frame = new ChartFrame("xyz",chart);
frame.setVisible(true);
frame.pack();*/
return getImageBase(file.getPath());
}
public static void main(String[] args) {
DefaultCategoryDataset();
}
public static void main(String[] args) {
double[] forwardItems = {12,34,45};//转发量
double[] commentItems = {24,14,35};//评论量
// double[] seeItems = {};//查看量
String[] items = { "残疾人联合会", "财政局", "公用集团" };
String[] series = new String[] { "已归集", "未归集"};
double[][] data = new double[][] { forwardItems, commentItems};
CategoryDataset barDataset = DatasetUtilities.createCategoryDataset(series, items, data);
DefaultCategoryDataset lineDataset = new DefaultCategoryDataset();
lineDataset.addValue(10, "归集数据量", items[0]);
lineDataset.addValue(5, "归集数据量", items[1]);
lineDataset.addValue(25, "归集数据量", items[2]);
getPhoFilePathYulun(barDataset, lineDataset, "图片标题", "pubOpinion");
}
/**
*
* 柱状图(堆叠)+折线图
* @param categoryBarDataset 柱状图数据
* @param categoryLineDataset 折线图数据
* @param title 图片标题
* @return
*/
public static String getPhoFilePathYulun(CategoryDataset categoryBarDataset, CategoryDataset categoryLineDataset, String title, String fileName){
JFreeChart chart = createCharts(categoryBarDataset,categoryLineDataset, title);
// 处理主标题的乱码
chart.getTitle().setFont(new Font("黑体", Font.BOLD, 18));
chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));//图例文字
// 获取图表区域对象
CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
// 获取X轴的对象
CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
categoryAxis.setLabelFont(new Font("黑体", Font.BOLD, 10));
categoryAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));
// 设置X轴文本倾斜45度
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
// 获取Y轴的对象
NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
// 处理X轴上的乱码
categoryAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));
// 处理X轴外的乱码
categoryAxis.setLabelFont(new Font("黑体", Font.BOLD, 10));
// 处理Y轴上的乱码
numberAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));
// 处理Y轴外的乱码
numberAxis.setLabelFont(new Font("黑体", Font.BOLD, 10));
/*String path = System.getProperty("user.dir") + File.separator + "template" + File.separator;
String filePath = path + fileName + ".png";
File file = new File(filePath);
try {
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
} catch (IOException e) {
e.printStackTrace();
}
return filePath;*/
ChartFrame chartFrame=new ChartFrame("汽车口碑排行榜",chart);
CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
BarRenderer customBarRenderer = (BarRenderer) plot.getRenderer();
//设置柱状图颜色
// customBarRenderer.setSeriesPaint(0, Color.decode("#F75000"));
// customBarRenderer.setSeriesPaint(1, Color.decode("#FFD306"));
//设置柱形图的宽度
customBarRenderer.setMaximumBarWidth(0.03);
//在图形上显示数字
customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// 设置为false柱子上就不显示数字
customBarRenderer.setBaseItemLabelsVisible(false);
customBarRenderer.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15));
// 设置背景透明度
plot.setBackgroundAlpha(0.1f);
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.gray);
// 设置网格横线大小
plot.setDomainGridlineStroke(new BasicStroke(0.2F));
plot.setRangeGridlineStroke(new BasicStroke(0.2F));
//以合适的大小展现图形
chartFrame.pack();
//在D盘目录下生成图片
File file = new File("D:/数据归集情况.jpeg");
try {
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
} catch (IOException e) {
e.printStackTrace();
}
//图形是否可见
// chartFrame.setVisible(true);
return getImageBase(file.getPath());
}
/**
* 初始化
* @param categoryBarDataset
* @param categoryLineDataset
* @return
*/
private static JFreeChart createCharts(CategoryDataset categoryBarDataset, CategoryDataset categoryLineDataset, String title) {
//参数: title,x轴说明,y轴说明,图例
JFreeChart chart = ChartFactory.createBarChart(title, "", "", categoryBarDataset, PlotOrientation.VERTICAL,
true, true, true);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置折线图颜色
/*BarRenderer customBarRenderer = (BarRenderer) plot.getRenderer();
customBarRenderer.setSeriesPaint(0, Color.decode("#F75000"));
customBarRenderer.setSeriesPaint(1, Color.decode("#FFD306"));
customBarRenderer.setSeriesPaint(2, Color.decode("#00A600"));*/
// set right margin
plot.getDomainAxis().setUpperMargin(0.1);
// set X axis Label lines
plot.getDomainAxis().setMaximumCategoryLabelLines(2);
//NumberAxis na = (NumberAxis) plot.getRangeAxis();
// stack bar chart
StackedBarRenderer rederer = new StackedBarRenderer();
plot.setRenderer(0, rederer);
rederer.setBaseItemLabelsVisible(false);
rederer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
rederer.setBaseItemLabelFont(new Font("Calibri", Font.ITALIC, 15));
rederer.setMaximumBarWidth(0.07);
rederer.setMinimumBarLength(0.1);
rederer.setBarPainter(new StandardBarPainter());
rederer.setSeriesPaint(0, new Color(65,105,225));
rederer.setSeriesPaint(1, new Color(203,196,52));
rederer.setSeriesPaint(2, new Color(128,7,248));
// line chart
LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
plot.setRenderer(1, lineRenderer);
lineRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineRenderer.setBaseItemLabelFont(new Font("Calibri", Font.ITALIC, 15));
lineRenderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.BASELINE_LEFT));
lineRenderer.setBaseItemLabelsVisible(false);
lineRenderer.setBaseShapesVisible(false);
// point style
lineRenderer.setSeriesShape(0, new Rectangle(5, 5));
lineRenderer.setBaseShapesVisible(false);
NumberAxis rightYAxis = new NumberAxis("");
rightYAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
plot.setRangeAxis(1, rightYAxis);
plot.setDataset(1, categoryLineDataset);
plot.mapDatasetToRangeAxis(1, 1);
// set render order
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
return chart;
}