100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JFreeChart 生成5种图表(饼图 柱状图 堆叠柱状图 折线图 散点图)

JFreeChart 生成5种图表(饼图 柱状图 堆叠柱状图 折线图 散点图)

时间:2022-03-13 14:17:08

相关推荐

JFreeChart 生成5种图表(饼图 柱状图 堆叠柱状图 折线图 散点图)

1 Maven依赖

<!-- Hutool工具包 --> <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.2</version></dependency><!-- JFreeChart图表库 --><dependency><groupId>org.jfree</groupId><artifactId>jfreechart</artifactId><version>1.5.3</version></dependency>

2 默认字体

2.1 系统字体

2.2 默认字体位置

3JFreeChartUtil

JFreeChart工具类。

package com.util;import cn.hutool.core.util.StrUtil;import org.jfree.chart.*;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.*;import org.jfree.chart.plot.*;import org.jfree.chart.renderer.category.*;import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;import org.jfree.chart.ui.*;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DefaultPieDataset;import org.springframework.core.io.ClassPathResource;import java.awt.*;import java.io.File;import java.io.IOException;import java.text.NumberFormat;import java.util.List;/*** JFreeChart工具类*/public class JFreeChartUtil {public static String NO_DATA_MSG = "数据加载失败";/*** 生成主题** @param fontName 字体名称(默认为宋体)* @return*/public static StandardChartTheme createChartTheme(String fontName) throws Exception {StandardChartTheme theme = new StandardChartTheme("unicode") {public void apply(JFreeChart chart) {chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);super.apply(chart);}};theme.setExtraLargeFont(getDefaultFont(Font.PLAIN, 20f));theme.setLargeFont(getDefaultFont(Font.PLAIN, 14f));theme.setRegularFont(getDefaultFont(Font.PLAIN, 12f));theme.setSmallFont(getDefaultFont(Font.PLAIN, 10f));return theme;}/*** 获取默认字体** @param style* @param size 字体大小* @return* @throws Exception*/public static Font getDefaultFont(int style, Float size) throws Exception {//获取宋体文件File defaultFontFile = new ClassPathResource("/font/simsun.ttc").getFile();Font defaultFont = Font.createFont(Font.TRUETYPE_FONT, defaultFontFile);defaultFont = defaultFont.deriveFont(style, size);return defaultFont;}/*** 创建饼图数据集合** @param legendNameList 图例名称列表* @param dataList 数据列表* @return*/public static DefaultPieDataset createDefaultPieDataset(List<String> legendNameList, List<Object> dataList) {DefaultPieDataset dataset = new DefaultPieDataset();//图例名称列表或数据列表为空if (legendNameList == null || legendNameList.size() <= 0 || dataList == null || dataList.size() <= 0) {return dataset;}for (int i = 0; i < legendNameList.size() && legendNameList.size() == dataList.size(); i++) {String value = dataList.get(i).toString();dataset.setValue(legendNameList.get(i), Double.valueOf(value));}return dataset;}/*** 设置饼状图渲染*/public static void setPieRender(Plot plot) {plot.setNoDataMessage(NO_DATA_MSG);plot.setInsets(new RectangleInsets(10, 10, 5, 10));PiePlot piePlot = (PiePlot) plot;piePlot.setInsets(new RectangleInsets(0, 0, 0, 0));piePlot.setCircular(true);// 圆形// 简单标签piePlot.setLabelGap(0.01);piePlot.setInteriorGap(0.05D);// 图例形状piePlot.setLegendItemShape(new Rectangle(10, 10));piePlot.setIgnoreNullValues(true);// 去掉标签背景色piePlot.setLabelBackgroundPaint(null);//去掉图表背景颜色piePlot.setBackgroundPaint(null);// 去掉阴影piePlot.setLabelShadowPaint(null);// 去掉边框piePlot.setLabelOutlinePaint(null);piePlot.setShadowPaint(null);// 显示标签数据piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}"));}/*** 创建类别数据集合(柱形图、折线图)** @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @return*/public static DefaultCategoryDataset createDefaultCategoryDataset(List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList) {DefaultCategoryDataset dataset = new DefaultCategoryDataset();//图例名称列表、x轴名称列表或数据列表为空if (xAxisNameList == null || xAxisNameList.size() <= 0 || legendNameList == null || legendNameList.size() <= 0|| dataList == null || dataList.size() <= 0) {return dataset;}for (int yAxisIndex = 0; yAxisIndex < legendNameList.size() && legendNameList.size() == dataList.size(); yAxisIndex++) {String legendName = legendNameList.get(yAxisIndex);List<Object> rowList = dataList.get(yAxisIndex);//该组数据不存在或该组数据总数不等于x轴数据数量if (rowList == null || rowList.size() != xAxisNameList.size()) {continue;}for (int xAxisIndex = 0; xAxisIndex < rowList.size(); xAxisIndex++) {String value = rowList.get(xAxisIndex).toString();dataset.setValue(Double.parseDouble(value), legendName, xAxisNameList.get(xAxisIndex));}}return dataset;}/*** 设置柱状图渲染** @param plot* @param isShowDataLabels 显示数据值标记*/public static void setBarRenderer(CategoryPlot plot, boolean isShowDataLabels) {plot.setNoDataMessage(NO_DATA_MSG);plot.setInsets(new RectangleInsets(10, 10, 5, 10));BarRenderer renderer = (BarRenderer) plot.getRenderer();// 设置柱子最大宽度renderer.setMaximumBarWidth(0.175);//设置图表背景颜色(透明)plot.setBackgroundPaint(null);//显示数据值标记if (isShowDataLabels) {renderer.setDefaultItemLabelsVisible(true);}renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));setXAixs(plot);setYAixs(plot);}/*** 设置折线图样式** @param plot* @param isShowDataLabels 是否显示数据标签* @param isShapesVisible 是否显示数据点*/public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels, boolean isShapesVisible) {plot.setNoDataMessage(NO_DATA_MSG);plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();//设置图表背景颜色(透明)plot.setBackgroundPaint(null);renderer.setDefaultStroke(new BasicStroke(1.5F));//显示数据标签if (isShowDataLabels) {renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,NumberFormat.getInstance()));// 位置renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));}// 数据点绘制形状renderer.setDefaultShapesVisible(isShapesVisible);setXAixs(plot);setYAixs(plot);}/*** 设置散点图样式** @param plot*/public static void setScatterRender(XYPlot plot) {plot.setNoDataMessage(NO_DATA_MSG);plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);//设置图表背景颜色(透明)plot.setBackgroundPaint(null);setXAixs(plot);setYAixs(plot);}/*** 设置类别图表(CategoryPlot) X坐标轴线条颜色和样式** @param plot*/public static void setXAixs(CategoryPlot plot) {Color lineColor = new Color(31, 121, 170);// X坐标轴颜色plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴标记|竖线颜色plot.getDomainAxis().setTickMarkPaint(lineColor);}/*** 设置图表(XYPlot) X坐标轴线条颜色和样式** @param plot*/public static void setXAixs(XYPlot plot) {Color lineColor = new Color(31, 121, 170);// X坐标轴颜色plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴标记|竖线颜色plot.getDomainAxis().setTickMarkPaint(lineColor);// x轴网格线条plot.setDomainGridlinePaint(new Color(192, 192, 192));}/*** 设置类别图表(CategoryPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示** @param plot*/public static void setYAixs(CategoryPlot plot) {Color lineColor = new Color(192, 208, 224);ValueAxis axis = plot.getRangeAxis();// Y坐标轴颜色axis.setAxisLinePaint(lineColor);// Y坐标轴标记|竖线颜色axis.setTickMarkPaint(lineColor);// 隐藏Y刻度axis.setAxisLineVisible(false);axis.setTickMarksVisible(false);// Y轴网格线条plot.setRangeGridlinePaint(new Color(192, 192, 192));plot.setRangeGridlineStroke(new BasicStroke(1));// 设置顶部Y坐标轴间距,防止数据无法显示plot.getRangeAxis().setUpperMargin(0.1);// 设置底部Y坐标轴间距plot.getRangeAxis().setLowerMargin(0.1);}/*** 设置图表(XYPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示** @param plot*/public static void setYAixs(XYPlot plot) {Color lineColor = new Color(192, 208, 224);ValueAxis axis = plot.getRangeAxis();// Y坐标轴颜色axis.setAxisLinePaint(lineColor);// Y坐标轴标记|竖线颜色axis.setTickMarkPaint(lineColor);// 隐藏Y刻度axis.setAxisLineVisible(false);axis.setTickMarksVisible(false);// Y轴网格线条plot.setRangeGridlinePaint(new Color(192, 192, 192));// 设置顶部Y坐标轴间距,防止数据无法显示plot.getRangeAxis().setUpperMargin(0.1);// 设置底部Y坐标轴间距plot.getRangeAxis().setLowerMargin(0.1);}}

4GenerateChartUtil

图表生成工具类。

package com.jfreechart;import cn.hutool.core.collection.CollectionUtil;import cn.hutool.core.util.StrUtil;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartUtils;import org.jfree.chart.JFreeChart;import org.jfree.chart.StandardChartTheme;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.block.BlockBorder;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.PiePlot3D;import org.jfree.chart.renderer.category.CategoryItemRenderer;import org.jfree.chart.ui.RectangleEdge;import org.jfree.chart.ui.TextAnchor;import org.jfree.chart.util.Rotation;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.xy.XYDataset;import java.awt.*;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.List;/*** 图表生成工具类*/public class GenerateChartUtil {/*** 生成柱状图(返回JFreeChart)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param yAxisMinValue y轴最小值(可以为空)* @param yAxisMaxValue y轴最大值(可以为空)* @param legendColorList 图例背景颜色(可以为空)* @param barLabelVisible 是否显示柱体标签(可以为空)* @param barLabelFormat 柱体标签格式(可以为空)* @return*/public static JFreeChart createBarChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, Double yAxisMinValue, Double yAxisMaxValue, List<Color> legendColorList, Boolean barLabelVisible, String barLabelFormat) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建柱状图JFreeChart chart = ChartFactory.createBarChart(chartTitle, xAxisTitle, yAxisTitle, JFreeChartUtil.createDefaultCategoryDataset(legendNameList, xAxisNameList, dataList));// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);// 对柱子进行渲染JFreeChartUtil.setBarRenderer(chart.getCategoryPlot(), true);CategoryPlot plot = (CategoryPlot) chart.getPlot();CategoryAxis categoryAxis = plot.getDomainAxis();// 最大换行数categoryAxis.setMaximumCategoryLabelLines(10);//y轴ValueAxis valueAxis = chart.getCategoryPlot().getRangeAxis();if (yAxisMinValue != null) {valueAxis.setLowerBound(yAxisMinValue);}if (yAxisMaxValue != null) {valueAxis.setUpperBound(yAxisMaxValue);}CategoryItemRenderer customBarRenderer = plot.getRenderer();//显示每个柱的数值if (barLabelVisible != null) {customBarRenderer.setDefaultItemLabelsVisible(barLabelVisible);//柱体数值格式if (StrUtil.isNotEmpty(barLabelFormat)) {customBarRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(barLabelFormat, NumberFormat.getInstance()));}}//设置系列柱体背景颜色if (CollectionUtil.isNotEmpty(legendColorList)) {for (int i = 0; i < legendNameList.size() && i < legendColorList.size(); i++) {Color color = legendColorList.get(i);if (color == null) {continue;}customBarRenderer.setSeriesPaint(i, color);}}// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于上侧chart.getLegend().setPosition(RectangleEdge.TOP);return chart;}/*** 生成柱状图(返回outputStream)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param outputStream 输出流* @param width宽度* @param height 高度* @param yAxisMinValue y轴最小值(可以为空)* @param yAxisMaxValue y轴最大值(可以为空)* @param legendColorList 图例背景颜色(可以为空)* @param barLabelVisible 是否显示柱体标签(可以为空)* @param barLabelFormat 柱体标签格式(可以为空)* @return*/public static void createBarChart(OutputStream outputStream, String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height, Double yAxisMinValue, Double yAxisMaxValue, List<Color> legendColorList, Boolean barLabelVisible, String barLabelFormat) {JFreeChart chart = createBarChart(chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle, yAxisMinValue, yAxisMaxValue, legendColorList, barLabelVisible, barLabelFormat);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}/*** 生成柱状图(返回byte[])** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param width宽度* @param height 高度* @param yAxisMinValue y轴最小值(可以为空)* @param yAxisMaxValue y轴最大值(可以为空)* @param legendColorList 图例背景颜色(可以为空)* @param barLabelVisible 是否显示柱体标签(可以为空)* @param barLabelFormat 柱体标签格式(可以为空)* @return*/public static byte[] createBarChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height, Double yAxisMinValue , Double yAxisMaxValue, List<Color> legendColorList, Boolean barLabelVisible, String barLabelFormat) {ByteArrayOutputStream bas = new ByteArrayOutputStream();createBarChart(bas, chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle, width, height, yAxisMinValue, yAxisMaxValue, legendColorList, barLabelVisible, barLabelFormat);byte[] byteArray = bas.toByteArray();return byteArray;}/*** 生成柱堆叠状图(返回JFreeChart)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @return*/public static JFreeChart createStackedBarChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建堆叠柱状图JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, xAxisTitle, yAxisTitle, JFreeChartUtil.createDefaultCategoryDataset(legendNameList, xAxisNameList, dataList));// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);// 对柱子进行渲染JFreeChartUtil.setBarRenderer(chart.getCategoryPlot(), true);// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于上侧chart.getLegend().setPosition(RectangleEdge.TOP);return chart;}/*** 生成堆叠柱状图(返回outputStream)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param outputStream 输出流* @param width宽度* @param height 高度* @return*/public static void createStackedBarChart(OutputStream outputStream, String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {JFreeChart chart = createStackedBarChart(chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}/*** 生成堆叠柱状图(返回byte[])** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param width宽度* @param height 高度* @return*/public static byte[] createStackedBarChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {ByteArrayOutputStream bas = new ByteArrayOutputStream();createStackedBarChart(bas, chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle, width, height);byte[] byteArray = bas.toByteArray();return byteArray;}/*** 生成折线图(返回JFreeChart)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @return*/public static JFreeChart createLineChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建折线图JFreeChart chart = ChartFactory.createLineChart(chartTitle, xAxisTitle, yAxisTitle, JFreeChartUtil.createDefaultCategoryDataset(legendNameList, xAxisNameList, dataList));// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);// 对折现进行渲染JFreeChartUtil.setLineRender(chart.getCategoryPlot(), true, true);// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于上侧chart.getLegend().setPosition(RectangleEdge.TOP);return chart;}/*** 生成折线图(返回outputStream)** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param outputStream 输出流* @param width宽度* @param height 高度* @return*/public static void createLineChart(OutputStream outputStream, String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {JFreeChart chart = createLineChart(chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}/*** 生成折线图(返回byte[])** @param chartTitle图表标题* @param legendNameList 图例名称列表* @param xAxisNameList x轴名称列表* @param dataList 数据列表* @param theme主题(null代表默认主题)* @param yAxisTitley轴标题* @param xAxisTitlex轴标题* @param width宽度* @param height 高度* @return*/public static byte[] createLineChart(String chartTitle, List<String> legendNameList, List<String> xAxisNameList, List<List<Object>> dataList, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {ByteArrayOutputStream bas = new ByteArrayOutputStream();createLineChart(bas, chartTitle, legendNameList, xAxisNameList, dataList, theme, yAxisTitle, xAxisTitle, width, height);byte[] byteArray = bas.toByteArray();return byteArray;}/*** 生成散点图(返回JFreeChart)** @param chartTitle 图表标题* @param dataset 数据集* @param theme主题(null代表默认主题)* @param yAxisTitle y轴标题* @param xAxisTitle x轴标题* @return*/public static JFreeChart createScatterPlot(String chartTitle, XYDataset dataset, StandardChartTheme theme, String yAxisTitle, String xAxisTitle) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建散点图JFreeChart chart = ChartFactory.createScatterPlot(chartTitle, xAxisTitle, yAxisTitle, dataset);// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);//散点图渲染JFreeChartUtil.setScatterRender(chart.getXYPlot());// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于上侧chart.getLegend().setPosition(RectangleEdge.TOP);return chart;}/*** 生成散点图(返回outputStream)** @param chartTitle 图表标题* @param dataset数据集* @param theme 主题(null代表默认主题)* @param yAxisTitle y轴标题* @param xAxisTitle x轴标题* @param outputStream 输出流* @param width 宽度* @param height 高度* @return*/public static void createScatterPlot(OutputStream outputStream, String chartTitle, XYDataset dataset, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {JFreeChart chart = createScatterPlot(chartTitle, dataset, theme, yAxisTitle, xAxisTitle);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}/*** 生成散点图(返回byte[])** @param chartTitle 图表标题* @param dataset 数据集* @param theme主题(null代表默认主题)* @param yAxisTitle y轴标题* @param xAxisTitle x轴标题* @param width宽度* @param height高度* @return*/public static byte[] createScatterPlot(String chartTitle, XYDataset dataset, StandardChartTheme theme, String yAxisTitle, String xAxisTitle, int width, int height) {ByteArrayOutputStream bas = new ByteArrayOutputStream();createScatterPlot(bas, chartTitle, dataset, theme, yAxisTitle, xAxisTitle, width, height);byte[] byteArray = bas.toByteArray();return byteArray;}}

5 饼图

5.1 饼图

具体实现过程请查看以下博客。

JFreeChart 生成饼图(标准饼图、3D饼图)/qq_38974638/article/details/121233491

5.2 3D饼图

具体实现过程请查看以下博客。

JFreeChart 生成饼图(标准饼图、3D饼图)/qq_38974638/article/details/121233491

6柱状图

6.1 调试代码

/*** 柱状图** @param response*/@GetMapping("/barChart")public void barChart(HttpServletResponse response) throws IOException {//x轴名称列表List<String> xAxisNameList = new ArrayList<>(Arrays.asList("一级", "二级", "三级", "四级", "五级"));//图例名称列表List<String> legendNameList = new ArrayList<>(Arrays.asList("李四", "张三"));//数据列表List<List<Object>> dataList = new ArrayList<>();dataList.add(new ArrayList<>(Arrays.asList(100, 90, 5, 6, 2)));dataList.add(new ArrayList<>(Arrays.asList(2, 0, 3, 4, 5)));//图例背景颜色List<Color> legendColorList = new ArrayList<>(Arrays.asList(new Color(65, 105, 225)));GenerateChartUtil.createBarChart(response.getOutputStream(), "各级变化图", legendNameList, xAxisNameList, dataList, JFreeChartUtil.createChartTheme("宋体"), "y轴", "x轴", 600, 400, 0d, 100d, legendColorList, true, "{2}%");}

6.2 调试结果

7 堆叠柱状图

7.1 调试代码

/*** 堆叠柱状图** @param response*/@GetMapping("/stackedBarChart")public void stackedBarChart(HttpServletResponse response) throws IOException {//x轴名称列表List<String> xAxisNameList = new ArrayList<>(Arrays.asList("一级", "二级", "三级", "四级", "五级"));//图例名称列表List<String> legendNameList = new ArrayList<>(Arrays.asList("李四", "张三"));//数据列表List<List<Object>> dataList = new ArrayList<>();dataList.add(new ArrayList<>(Arrays.asList(1, 3, 5, 6, 2)));dataList.add(new ArrayList<>(Arrays.asList(2, 1, 3, 4, 5)));GenerateChartUtil.createStackedBarChart(response.getOutputStream(), "各级变化图", legendNameList, xAxisNameList, dataList, JFreeChartUtil.createChartTheme("宋体"), "y轴", "x轴", 600, 400);}

7.2 调试结果

8 折线图

8.1 调试代码

/*** 折线图** @param response*/@GetMapping("/lineChart")public void lineChart(HttpServletResponse response) throws IOException {//x轴名称列表List<String> xAxisNameList = new ArrayList<>(Arrays.asList("一级", "二级", "三级", "四级", "五级"));//图例名称列表List<String> legendNameList = new ArrayList<>(Arrays.asList("李四", "张三"));//数据列表List<List<Object>> dataList = new ArrayList<>();dataList.add(new ArrayList<>(Arrays.asList(1, 3, 5, 6, 2)));dataList.add(new ArrayList<>(Arrays.asList(2, 1, 3, 4, 5)));GenerateChartUtil.createLineChart(response.getOutputStream(), "各级变化图", legendNameList, xAxisNameList, dataList, JFreeChartUtil.createChartTheme("宋体"), "y轴", "x轴", 600, 400);}

8.2 调试结果

9 散点图

9.1 调试代码

/*** 散点图** @param response*/@GetMapping("/scatterPlot")public void scatterPlot(HttpServletResponse response) throws IOException {//设置散点图数据集//设置第一个XYSeries firefox = new XYSeries("Firefox");firefox.add(1.0, 1.0);firefox.add(2.0, 4.0);firefox.add(3.0, 3.0);//设置第二个XYSeries chrome = new XYSeries("Chrome");chrome.add(1.0, 4.0);chrome.add(2.0, 5.0);chrome.add(3.0, 6.0);//设置第三个XYSeries ie = new XYSeries("IE");ie.add(3.0, 4.0);ie.add(4.0, 5.0);ie.add(5.0, 4.0);//添加到数据集XYSeriesCollection dataset = new XYSeriesCollection();dataset.addSeries(firefox);dataset.addSeries(chrome);dataset.addSeries(ie);GenerateChartUtil.createScatterPlot(response.getOutputStream(), "各级变化图", dataset, JFreeChartUtil.createChartTheme("宋体"), "y轴", "x轴", 600, 400);}

9.2 调试结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。