<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.4</version>
</dependency>
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.swing.SwingUtilities;
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.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public static void getLineChart(XYSeriesCollection dataset, String title, String xReason, String yReason, String fileLocalPath){
SwingUtilities.invokeLater(() -> {
StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createXYLineChart(
title,
xReason,
yReason,
dataset,
org.jfree.chart.plot.PlotOrientation.VERTICAL,
true,
true,
false
);
final XYPlot plot = chart.getXYPlot( );
NumberAxis na = (NumberAxis)plot.getDomainAxis();
na.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
renderer.setSeriesItemLabelGenerator(0, new org.jfree.chart.labels.XYItemLabelGenerator() {
@Override
public String generateLabel(org.jfree.data.xy.XYDataset dataset, int series, int item) {
return String.format("%d", (int)dataset.getYValue(series, item));
}
});
renderer.setSeriesItemLabelGenerator(1, new org.jfree.chart.labels.XYItemLabelGenerator() {
@Override
public String generateLabel(org.jfree.data.xy.XYDataset dataset, int series, int item) {
return String.format("%d", (int)dataset.getYValue(series, item));
}
});
renderer.setDefaultItemLabelFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 16));
renderer.setDefaultItemLabelsVisible(true);
renderer.setSeriesPaint( 0 , Color.GREEN );
renderer.setSeriesPaint( 1 , Color.RED);
renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
plot.setRenderer( renderer );
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体",Font.BOLD,20));
LegendTitle legend = chart.getLegend();
if (legend!=null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
File file = new File(fileLocalPath);
int width = 800;
int height = 600;
try {
ChartUtils.saveChartAsJPEG(file, chart, width, height);
} catch (IOException e) {
e.printStackTrace();
}
});
}
public static void main(String[] args) {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("mmm数量");
series1.add(1, 10);
series1.add(2, 20);
series1.add(3, 15);
series1.add(4, 40);
series1.add(5, 50);
XYSeries series2 = new XYSeries("nnn数量");
series2.add(1, 15);
series2.add(2, 25);
series2.add(3, 35);
series2.add(4, 23);
series2.add(5, 55);
dataset.addSeries(series1);
dataset.addSeries(series2);
String title = "2024-09-10 报告汇总";
String xReason = "X轴---时间";
String yReason = "Y轴---数量";
String fileLocalPath = "G:\\test\\a.jpeg";
getLineChart(dataset, title, xReason, yReason, fileLocalPath);
}