一、饼图

 // 数据DefaultPieDataset dataSet = new DefaultPieDataset();dataSet.setValue("2020年", 26542);// 图例-值dataSet.setValue("2021年", 27542);// 图例-值dataSet.setValue("2022年", 28542);// 图例-值StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 64)); // 设置标题字体standardChartTheme.setRegularFont(new Font("宋体", Font.BOLD, 32)); // 设置图例的字体standardChartTheme.setLargeFont(new Font("宋体", Font.BOLD, 20)); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-饼形图的数据集对象-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart = ChartFactory.createPieChart("JFreeChart饼图", dataSet, true, true, false);// chart.setTitle(new TextTitle(title[0], new Font("宋书", Font.BOLD, 64)));// 重新设置标题// chart.removeLegend();// 是否移除图例PiePlot plot = (PiePlot)chart.getPlot(); // 获得图表显示对象plot.setOutlinePaint(Color.WHITE);// 外边框颜色plot.setBackgroundPaint(Color.WHITE);// 白色背景plot.setNoDataMessage("无图表数据");// 无数据提示plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 32));// 提示字体plot.setNoDataMessagePaint(Color.RED);// 提示字体颜色plot.setCircular(true);// 是否是正圆plot.setLegendItemShape(new Rectangle(10, 10));// 图例形状plot.setShadowPaint(Color.WHITE);// 白色阴影plot.setExplodePercent(0, 0.1D);// 突出第一个片区// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置 上、下、左、右legend.setVisible(true);// 是否显示图例legend.setBorder(BlockBorder.NONE);// 图例无边框legend.setItemFont(new Font("宋体", Font.BOLD, 32));// 图例大小// 数据标签plot.setLabelFont(new Font("宋书", Font.BOLD, 32)); // 设置数据标签字体plot.setLabelBackgroundPaint(Color.WHITE);// 数据标签背景色plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));// 数据标签数据格式plot.setLabelOutlinePaint(Color.WHITE);// 数据标签外边框颜色plot.setLabelShadowPaint(Color.WHITE);// 数据标签阴影颜色plot.setMaximumLabelWidth(0.2D);// 数据标签最大宽度FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/pie.png");// 输出流ChartUtilities.writeChartAsPNG(out, chart, 800, 800);// 输出流、图标、图片宽度、图片高度} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}

效果图:

二、条状图

// 数据DefaultCategoryDataset dataSet = new DefaultCategoryDataset();dataSet.addValue(1542, "2020年", "第一季度");// Y轴-图例-X轴dataSet.addValue(2542, "2020年", "第二季度");// Y轴-图例-X轴dataSet.addValue(3542, "2020年", "第三季度");// Y轴-图例-X轴dataSet.addValue(4542, "2020年", "第四季度");// Y轴-图例-X轴dataSet.addValue(1642, "2021年", "第一季度");// Y轴-图例-X轴dataSet.addValue(2642, "2021年", "第二季度");// Y轴-图例-X轴dataSet.addValue(3642, "2021年", "第三季度");// Y轴-图例-X轴dataSet.addValue(4642, "2021年", "第四季度");// Y轴-图例-X轴StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 64)); // 设置标题字体standardChartTheme.setRegularFont(new Font("宋体", Font.BOLD, 32)); // 设置图例的字体standardChartTheme.setLargeFont(new Font("宋体", Font.BOLD, 20)); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-饼形图的数据集对象-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart = ChartFactory.createBarChart("JFreeChart条形图", "销售额", "季度", dataSet, PlotOrientation.VERTICAL,true, true, false);// chart.setTitle(new TextTitle(title[0], new Font("宋书", Font.BOLD, 64)));// 重新设置标题// chart.removeLegend();// 是否移除图例CategoryPlot plot = (CategoryPlot)chart.getPlot(); // 获得图表显示对象plot.setOutlineVisible(false);// 是否显示外边框plot.setOutlinePaint(Color.WHITE);// 外边框颜色// plot.setOutlineStroke(new BasicStroke(2.f));// 外边框框线粗细plot.setBackgroundPaint(Color.WHITE);// 白色背景plot.setNoDataMessage("无图表数据");// 无数据提示plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 32));// 提示字体plot.setNoDataMessagePaint(Color.RED);// 提示字体颜色// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置 上、下、左、右legend.setVisible(true);// 是否显示图例legend.setBorder(BlockBorder.NONE);// 图例无边框legend.setItemFont(new Font("宋体", Font.BOLD, 32));// 图例大小// 网格线plot.setDomainGridlinePaint(Color.BLUE);plot.setDomainGridlinesVisible(true);// 竖线plot.setRangeGridlinePaint(Color.BLACK);plot.setRangeGridlinesVisible(true);// 横线// 横坐标CategoryAxis xAxis = plot.getDomainAxis();xAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 25));// 设置X轴值字体xAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 设置X轴标签字体xAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置X轴线粗细xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜xAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色xAxis.setUpperMargin(0.18D);// 右边距xAxis.setLowerMargin(0.1D);// 左边距// 纵坐标ValueAxis yAxis = plot.getRangeAxis();yAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 25));// 设置y轴字体yAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 设置X轴标签字体yAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置y轴线粗细yAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色yAxis.setUpperMargin(0.18D);// 上边距yAxis.setLowerMargin(0.1D);// 下边距// 标签数据BarRenderer renderer = new BarRenderer();renderer.setBarPainter(new StandardBarPainter());// 取消渐变效果renderer.setShadowVisible(false);// 关闭倒影renderer.setDrawBarOutline(false); // 设置柱子边框可见renderer.setItemMargin(0.03); // 组内柱子间隔为组宽的10%renderer.setMaximumBarWidth(0.03);// 设置条形柱最大宽度renderer.setMinimumBarLength(0.03);// 设置条形柱最小宽度renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 数据标签格式renderer.setBaseItemLabelsVisible(true);// 是否显示数据标签renderer.setItemLabelFont(new Font("宋书", Font.BOLD, 32));// 数据标签字体renderer.setSeriesPaint(0, Color.BLUE);plot.setRenderer(renderer);FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/bar.png");// 输出流ChartUtilities.writeChartAsPNG(out, chart, 1800, 800);// 输出流、图标、图片宽度、图片高度} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}


三、折线图

 // 数据DefaultCategoryDataset dataSet = new DefaultCategoryDataset();dataSet.addValue(3542, "2020年", "第一季度");// Y轴-图例-X轴dataSet.addValue(3692, "2020年", "第二季度");// Y轴-图例-X轴dataSet.addValue(8542, "2020年", "第三季度");// Y轴-图例-X轴dataSet.addValue(5742, "2020年", "第四季度");// Y轴-图例-X轴dataSet.addValue(1242, "2021年", "第一季度");// Y轴-图例-X轴dataSet.addValue(2612, "2021年", "第二季度");// Y轴-图例-X轴dataSet.addValue(1942, "2021年", "第三季度");// Y轴-图例-X轴dataSet.addValue(4612, "2021年", "第四季度");// Y轴-图例-X轴StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 64)); // 设置标题字体standardChartTheme.setRegularFont(new Font("宋体", Font.BOLD, 32)); // 设置图例的字体standardChartTheme.setLargeFont(new Font("宋体", Font.BOLD, 20)); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-饼形图的数据集对象-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart = ChartFactory.createLineChart("JFreeChart折线图", "销售额", "季度", dataSet, PlotOrientation.VERTICAL,true, true, false);// chart.setTitle(new TextTitle(title[0], new Font("宋书", Font.BOLD, 64)));// 重新设置标题// chart.removeLegend();// 是否移除图例CategoryPlot plot = (CategoryPlot)chart.getPlot(); // 获得图表显示对象plot.setOutlineVisible(false);// 是否显示外边框plot.setOutlinePaint(Color.WHITE);// 外边框颜色// plot.setOutlineStroke(new BasicStroke(2.f));// 外边框框线粗细plot.setBackgroundPaint(Color.WHITE);// 白色背景plot.setNoDataMessage("无图表数据");// 无数据提示plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 32));// 提示字体plot.setNoDataMessagePaint(Color.RED);// 提示字体颜色// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置 上、下、左、右legend.setVisible(true);// 是否显示图例legend.setBorder(BlockBorder.NONE);// 图例无边框legend.setItemFont(new Font("宋体", Font.BOLD, 32));// 图例大小// 网格线plot.setDomainGridlinePaint(Color.BLUE);plot.setDomainGridlinesVisible(true);// 竖线plot.setRangeGridlinePaint(Color.BLACK);plot.setRangeGridlinesVisible(true);// 横线// 横坐标CategoryAxis xAxis = plot.getDomainAxis();xAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 25));// 设置X轴值字体xAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 设置X轴标签字体xAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置X轴线粗细xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜xAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色xAxis.setUpperMargin(0.18D);// 右边距xAxis.setLowerMargin(0.1D);// 左边距// 纵坐标ValueAxis yAxis = plot.getRangeAxis();yAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 25));// 设置y轴字体yAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 设置X轴标签字体yAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置y轴线粗细yAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色yAxis.setUpperMargin(0.18D);// 上边距yAxis.setLowerMargin(0.1D);// 下边距// 数据标签LineAndShapeRenderer renderer = new LineAndShapeRenderer();renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 数据标签格式renderer.setBaseItemLabelsVisible(true);// 是否显示数据标签renderer.setBaseItemLabelFont(new Font("宋书", Font.BOLD, 32));// 数据标签字体renderer.setSeriesPaint(0, Color.BLUE);renderer.setBaseShapesFilled(Boolean.TRUE); // 在数据点显示实心的小图标renderer.setBaseShapesVisible(true); // 设置显示小图标renderer.setBaseItemLabelFont(font2);// 设置数字的字体大小renderer.setBaseStroke(new BasicStroke(4f));plot.setRenderer(renderer);FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/line.png");// 输出流ChartUtilities.writeChartAsPNG(out, chart, 1800, 800);// 输出流、图标、图片宽度、图片高度} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}


四、曲线图

// 数据XYSeries series = new XYSeries("2020年");series.add(1, 3542);series.add(2, 3692);series.add(3, 8542);series.add(4, 5742);XYSeries series1 = new XYSeries("2021年");series1.add(1, 1242);series1.add(2, 2612);series1.add(3, 1942);series1.add(4, 4612);XYSeriesCollection xySeriesCollection = new XYSeriesCollection();xySeriesCollection.addSeries(series);xySeriesCollection.addSeries(series1);StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 64)); // 设置标题字体standardChartTheme.setRegularFont(new Font("宋体", Font.BOLD, 32)); // 设置图例的字体standardChartTheme.setLargeFont(new Font("宋体", Font.BOLD, 20)); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-饼形图的数据集对象-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart = ChartFactory.createXYLineChart("JFreeChart折线图", "销售额", "季度", xySeriesCollection,PlotOrientation.VERTICAL, true, true, false);// chart.setTitle(new TextTitle(title[0], new Font("宋书", Font.BOLD, 64)));// 重新设置标题// chart.removeLegend();// 是否移除图例XYPlot plot = (XYPlot)chart.getPlot(); // 获得图表显示对象plot.setOutlineVisible(false);// 是否显示外边框plot.setOutlinePaint(Color.WHITE);// 外边框颜色// plot.setOutlineStroke(new BasicStroke(2.f));// 外边框框线粗细plot.setBackgroundPaint(Color.WHITE);// 白色背景plot.setNoDataMessage("无图表数据");// 无数据提示plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 32));// 提示字体plot.setNoDataMessagePaint(Color.RED);// 提示字体颜色// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置 上、下、左、右legend.setVisible(true);// 是否显示图例legend.setBorder(BlockBorder.NONE);// 图例无边框legend.setItemFont(new Font("宋体", Font.BOLD, 32));// 图例大小// 网格线plot.setDomainGridlinePaint(Color.BLUE);plot.setDomainGridlinesVisible(true);// 竖线plot.setRangeGridlinePaint(Color.BLACK);plot.setRangeGridlinesVisible(true);// 横线// 横坐标NumberAxis xAxis = (NumberAxis)plot.getDomainAxis();// 获得横坐标xAxis.setTickLabelFont(font2);// 设置X轴字体xAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 轴标签值字体xAxis.setTickLabelFont(new Font("宋书", Font.BOLD, 25));// 轴标签字体xAxis.setAxisLineStroke(new BasicStroke(2f)); // 设置轴线粗细xAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色xAxis.setLowerMargin(0.03D);// 左侧边距xAxis.setUpperMargin(0.03D);// 右侧边距xAxis.setTickUnit(new NumberTickUnit(1D));// 间距1D// 纵坐标ValueAxis yAxis = plot.getRangeAxis();yAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 25));// 设置y轴字体yAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));// 设置X轴标签字体yAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置y轴线粗细yAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色yAxis.setUpperMargin(0.18D);// 上边距yAxis.setLowerMargin(0.1D);// 下边距// 标签XYSplineRenderer renderer = new XYSplineRenderer();renderer.setItemLabelGenerator(new StandardXYItemLabelGenerator());renderer.setBaseItemLabelsVisible(true); // 基本项标签显示renderer.setBaseShapesVisible(true);renderer.setShapesFilled(Boolean.TRUE); // 在数据点显示实心的小图标renderer.setShapesVisible(true); // 设置显示小图标renderer.setItemLabelFont(new Font("宋书", Font.BOLD, 20));// 设置数字的字体大小renderer.setStroke(new BasicStroke(4f));plot.setRenderer(renderer);FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/xyline.png");// 输出流ChartUtilities.writeChartAsPNG(out, chart, 1800, 800);// 输出流、图标、图片宽度、图片高度} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}


五、复合图(条形图、折线图)

 // 数据DefaultCategoryDataset dataSet1 = new DefaultCategoryDataset();dataSet1.addValue(4542, "2020年", "第一季度");// y值-图例-x值dataSet1.addValue(2692, "2020年", "第二季度");dataSet1.addValue(6542, "2020年", "第三季度");dataSet1.addValue(1742, "2020年", "第四季度");dataSet1.addValue(3542, "2021年", "第一季度");// y值-图例-x值dataSet1.addValue(3692, "2021年", "第二季度");dataSet1.addValue(8542, "2021年", "第三季度");dataSet1.addValue(5742, "2021年", "第四季度");DefaultCategoryDataset dataSet2 = new DefaultCategoryDataset();dataSet2.addValue(4542, "2022年", "第一季度");// y值-图例-x值dataSet2.addValue(2692, "2022年", "第二季度");dataSet2.addValue(1542, "2022年", "第三季度");dataSet2.addValue(7742, "2022年", "第四季度");StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 64)); // 设置标题字体standardChartTheme.setRegularFont(new Font("宋体", Font.BOLD, 32)); // 设置图例的字体standardChartTheme.setLargeFont(new Font("宋体", Font.BOLD, 20)); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.WHITE);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-横轴标题-纵轴标题-饼形图的数据集对象-图表方向-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart =ChartFactory.createBarChart("复合图", "x轴", "y轴", null, PlotOrientation.VERTICAL, true, true, false);chart.setBackgroundPaint(Color.white); // 设置画布背景色// chart.setTitle(new TextTitle(title[0], new Font("宋书", Font.BOLD, 64)));// 重新设置标题CategoryPlot plot = (CategoryPlot)chart.getPlot();// 获得图标中间部分,即plotplot.setBackgroundPaint(Color.WHITE); // 背景色plot.setOutlinePaint(Color.WHITE);// 颜色// plot.setOutlineStroke(new BasicStroke(2.f));// 边框大小plot.setOutlineVisible(false);// 是否显示// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置legend.setVisible(true);// 关闭图例legend.setItemFont(font2);// 图例大小// 网格线plot.setRangeGridlinesVisible(true); // 显示纵坐标格线plot.setRangeGridlinePaint(Color.BLUE); // 纵坐标格线颜色plot.setDomainGridlinesVisible(true); // 显示横坐标格线plot.setDomainGridlinePaint(Color.BLACK); // 横坐标格线颜色// 设置轴1--数据匹配NumberAxis axis0 = new NumberAxis();// axis0.setLabelFont(new Font("宋体", Font.BOLD, 32));axis0.setTickLabelFont(font2);// y轴字体plot.setRangeAxis(0, axis0);plot.setDataset(0, dataSet1);plot.mapDatasetToRangeAxis(0, 0);// 数据和y轴// 设置轴2--数据匹配NumberAxis axis1 = new NumberAxis();// axis1.setLabelFont(new Font("宋体", Font.BOLD, 32));axis1.setTickLabelFont(font2);// y轴字体plot.setRangeAxis(1, axis1);plot.setDataset(1, dataSet2);plot.mapDatasetToRangeAxis(1, 1);// 坐标轴1--曲线颜色BarRenderer renderer0 = new BarRenderer();renderer0.setItemLabelFont(font4);renderer0.setSeriesPaint(0, color1);renderer0.setLegendTextPaint(0, color1);// 设置对应图例字体颜色renderer0.setSeriesPaint(1, color2);renderer0.setLegendTextPaint(1, color2);// 设置对应图例字体颜色renderer0.setShadowVisible(false);// 关闭倒影renderer0.setBarPainter(new StandardBarPainter());// 取消渐变效果renderer0.setMaximumBarWidth(0.03);// 设置条形柱最大宽度renderer0.setMinimumBarLength(0.03);renderer0.setDrawBarOutline(false); // 设置柱子边框可见renderer0.setItemMargin(0.03); // 组内柱子间隔为组宽的10%renderer0.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer0.setBaseItemLabelsVisible(true);renderer0.setItemLabelFont(font4);renderer0.setStroke(new BasicStroke(4f));plot.setRenderer(0, renderer0);// 坐标轴2--曲线颜色LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();renderer1.setItemLabelFont(font4);renderer1.setSeriesPaint(0, color3);renderer1.setLegendTextPaint(0, color3);// 设置对应图例字体颜色// renderer1.setSeriesPaint(1, color4);// renderer1.setLegendTextPaint(1, color4);// 设置对应图例字体颜色renderer1.setToolTipGenerator(new StandardCategoryToolTipGenerator());DecimalFormat decimalformat1 = new DecimalFormat(" ##.## "); // 数据点显示数据值的格式renderer1.setItemLabelGenerator(new StandardCategoryItemLabelGenerator(" {2} ", decimalformat1));// 上面这句是设置数据项标签的生成器renderer1.setItemLabelsVisible(true); // 设置项标签显示renderer1.setBaseItemLabelsVisible(true); // 基本项标签显示// 上面这几句就决定了数据点按照设定的格式显示数据值renderer1.setShapesFilled(Boolean.TRUE); // 在数据点显示实心的小图标renderer1.setShapesVisible(true); // 设置显示小图标renderer1.setItemLabelFont(font4);// 设置数字的字体大小renderer1.setStroke(new BasicStroke(4f));plot.setRenderer(1, renderer1);CategoryAxis xAxis = plot.getDomainAxis();// 获得横坐标xAxis.setTickLabelFont(font4);// 设置X轴字体// xAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));xAxis.setAxisLineStroke(new BasicStroke(2f)); // 设置轴线粗细// xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜ValueAxis yAxis = plot.getRangeAxis();yAxis.setTickLabelFont(font4);// 设置y轴字体yAxis.setUpperMargin(0.18);// 上边距,防止最大的一个数据靠近了坐标轴。plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);// 折线在柱面前面显示FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/lienAndbar.png");// 输出流ChartUtilities.writeChartAsPNG(out, chart, 1800, 800);// 输出流、图标、图片宽度、图片高度} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}


六、自定义同组条形图颜色

public static class CustomRenderer extends BarRenderer {private Paint[] colors;public CustomRenderer(DefaultCategoryDataset dataSet) {int size = dataSet.getColumnKeys().size();colors = new Paint[size];double obj = 0;for (int i = 0; i < size; i++) {obj = (double)dataSet.getValue(0, i);if (obj > 0) {// 大于0为红色colors[i] = Color.decode("#0000FF");} else {// 小于等于零为蓝色colors[i] = Color.decode("#D64646");}}}// 每根柱子以初始化的颜色不断轮循public Paint getItemPaint(int i, int j) {return colors[j % colors.length];}}public static void main(String[] args) {// 数据DefaultCategoryDataset dataSet = new DefaultCategoryDataset();dataSet.addValue(123, "2020年", "第一季度");dataSet.addValue(-231, "2020年", "第二季度");dataSet.addValue(531, "2020年", "第三季度");dataSet.addValue(223, "2020年", "第四季度");StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式standardChartTheme.setExtraLargeFont(font3); // 设置标题字体standardChartTheme.setRegularFont(font2); // 设置图例的字体standardChartTheme.setLargeFont(font1); // 设置轴向的字体standardChartTheme.setChartBackgroundPaint(Color.white);// 设置主题背景色ChartFactory.setChartTheme(standardChartTheme);// 应用主题样式// 定义图表对象 图表的标题-横轴标题-纵轴标题-饼形图的数据集对象-图表方向-是否显示图列-是否显示提示文-是否生成超链接JFreeChart chart =ChartFactory.createBarChart("自定义颜色条形图", "x轴", "y轴", dataSet, PlotOrientation.VERTICAL, true, true, false);// chart.removeLegend();// 删除图例// chart.setTitle(new TextTitle(title[0], new Font("宋体", Font.BOLD, 50)));// 重新设置标题CategoryPlot plot = (CategoryPlot)chart.getPlot();// 获得图标中间部分,即plotplot.setBackgroundPaint(Color.WHITE);// 白色背景// 图例LegendTitle legend = chart.getLegend();// 图例对象legend.setPosition(RectangleEdge.BOTTOM);// 图例位置legend.setVisible(true);// 关闭图例legend.setItemFont(new Font("宋书", Font.BOLD, 32));// 图例大小// 图表绘制背景边框线设置plot.setOutlinePaint(Color.WHITE);// 颜色// plot.setOutlineStroke(new BasicStroke(2.f));// 大小plot.setOutlineVisible(false);// 是否显示plot.setDomainGridlinePaint(Color.BLUE);plot.setDomainGridlinesVisible(true);// 竖线plot.setRangeGridlinePaint(Color.BLACK);plot.setRangeGridlinesVisible(true);// 横线CategoryAxis xAxis = plot.getDomainAxis();// 获得横坐标xAxis.setTickLabelFont(font4);// 设置X轴字体// xAxis.setLabelFont(new Font("宋书", Font.BOLD, 32));xAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置轴线粗细xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜xAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色ValueAxis yAxis = plot.getRangeAxis();yAxis.setTickLabelFont(font4);// 设置y轴字体yAxis.setUpperMargin(0.18D);// 上边距,防止最大的一个数据靠近了坐标轴。yAxis.setLowerMargin(0.18D);// 下边距yAxis.setAxisLineStroke(new BasicStroke(1f)); // 设置轴线粗细yAxis.setAxisLinePaint(Color.BLACK);// 轴线颜色CustomRenderer renderer = new CustomRenderer(dataSet);// 自定义柱子样式renderer.setBarPainter(new StandardBarPainter());// 取消渐变效果renderer.setShadowVisible(false);// 关闭倒影renderer.setDrawBarOutline(false); // 设置柱子边框可见renderer.setItemMargin(0.03); // 组内柱子间隔为组宽的10%renderer.setMaximumBarWidth(0.03);// 设置条形柱最大宽度renderer.setMinimumBarLength(0.03);// 设置条形柱最小宽度renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setBaseItemLabelsVisible(true);renderer.setBaseItemLabelFont(font4);renderer.setSeriesPaint(0, Color.BLUE);plot.setRenderer(renderer);FileOutputStream out = null;try {out = new FileOutputStream("C:/Users/Administrator/Desktop/line.png");ChartUtilities.writeChartAsPNG(out, chart, 800, 800);} catch (IOException e) {e.printStackTrace();} finally {try {if (out != null) {out.close();}} catch (IOException e) {e.printStackTrace();}}}

JFreeChart常用图表使用相关推荐

  1. java 柱状图下载_(JFreeChart)Java图表制作

    (JFreeChart)Java图表制作软件时一个灵活的设计,很容易扩展,和目标服务器端和客户端应用程序;,支持多种图表类型. 输出类型也是多样化.包括Swing组件.图像文件(包括PNG和JPEG) ...

  2. JfreeChart常用表格绘制

    JfreeChart常用表格绘制 绘制普通柱状图--垂直 package com.offcn.utils;import org.jfree.chart.ChartFactory; import org ...

  3. 数据可视化:常用图表使用总结

    文章对常见的数据可视化图表进行了简单的汇总分析,希望对你有益. 什么是数据可视化? 数据可视化,简简单单就是把数据展示出来吗?非也非也,其终极是为了满足用户对数据的价值期望,利用数据,借助可视化工具, ...

  4. ECharts 常用图表一看即会「散点图」「饼图」「地图」「雷达图」「仪表盘」

    文接上篇 ECharts 入门知识 ECharts 入门真的很简单 ,本文继续介绍 ECharts 常用图表相关内容. 一.散点图 散点图多用于推断不同维度数据之间的相关性,如下图判断身高体重相关性散 ...

  5. Tableau上面地图与条形图结合_Tableau | 20种常用图表(上文)

    数据源:超市数据 使用工具:Tableau 数据源及Tableau工作簿 :https://pan.baidu.com/s/17oq4seZbP0reT8gh170KxQ 密码:pj3z 没错,以超市 ...

  6. 第五章. 可视化数据分析图表—常用图表的绘制2—直方图,饼形图

    第五章. 可视化数据分析图 5.3 常用图表的绘制2-直方图,饼形图 本节主要介绍常用图表的绘制,主要包括直方图,饼形图. 1.直方图(matplotlib.pyplot.hist) 直方图,又称质量 ...

  7. Apache Echarts常用图表之柱状图

    文章目录 Echarts常用图表 柱状图 1. 柱状图的实现步骤 2. 柱状图的常见效果 最大值\最小值 `markPoint` 平均值 `markLine` 数值显示 `label` 柱宽度 `ba ...

  8. 计算机中常用于比较的图表有,2013年职称计算机Excel考点:常用图表类型

    [摘要]为帮助广大考生备考,网校特整理职称计算机Excel考点常用图表类型的辅导资料,祝您在环球网校学习愉快! Excel提供了14种标准类型的图表,每种图表类型又包含了若干种子图表类型,并且还提供了 ...

  9. 计算机中的图表类型,高级会计师《职称计算机》图表制作:主要常用图表类型...

    高级会计备考的征程已经开始,大家准备好了么?中公财经小编为大家整理了高级会计师<职称计算机>图表制作:主要常用图表类型,希望能帮助考生们更加了解高级会计师计算机. 高级会计师<职称计 ...

  10. 数据可视化之旅:常用图表对比

    <七天数据可视化之旅>第五天:常用图表对比 作者:Destiny 来源:木东居士 Destiny,某物流公司数据产品经理,目前从事数据平台搭建和可视化相关的工作.持续学习中,期望与大家多多 ...

最新文章

  1. 赠书 | 联邦学习如何在视觉领域应用?
  2. python_redis模块
  3. python爬虫机器_Python常用的机器学习库|python爬虫|python入门|python教程
  4. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(二)
  5. static class 静态类(Java)
  6. android intent包装,Android 中的 Intent
  7. 7-15 求组合数 (15 分)
  8. cartographer探秘第四章之代码解析(二) --- 传感器数据处理过程
  9. 科学研究:统计来看,是不是新(接触的)人运气明显好?
  10. c++编写浪漫烟花 表白代码大全
  11. 计算机表格两行互换步骤,excel怎么让两行互换位置,EXCEL里两个格的内容怎么互换?...
  12. ipa-server
  13. 计算机等级考试报名班级填什么,全国计算机等级考试报名流程
  14. 第4章数据库的查询、视图和游标
  15. 安装 FreeBSD-13.0-RELEASE-amd64
  16. J276-删除链表中重复的节点
  17. Java高级开发面试题整理
  18. 设置iPhone的呼叫转移来电等待
  19. 一种基于快速GeoHash实现海量商品与商圈高效匹配的算法
  20. vacuum 数据库 用法_SQLite Vacuum

热门文章

  1. pip install 报语法错误
  2. 一起学爬虫(Python) — 22 自动化详解
  3. 抽象代数笔记2——群
  4. CTO、技术总监、首席架构师的区别
  5. 虚拟电厂 3D 可视化,节能减排绿色发展
  6. Photoshop的安装教程
  7. 8.04版本liveCD安装到94%时出现GRUB致命错误的问题解决
  8. oracle box怎么全屏,Oracle VM VirtualBox 虚拟机设置全屏与共享
  9. hive 自定义UDF函数解析HTML
  10. 苏州新导蓝牙定位系统原理精确解读,三分钟看懂蓝牙定位