一、饼图
// 数据
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();// 获得图标中间部分,即plot
plot.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();// 获得图标中间部分,即plot
plot.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();
}
}
}