Preface
In the world of data visualization, charts are powerful tools for displaying data. Whether it is a line chart, a bar chart or a pie chart, they can help us understand data more intuitively. In the Java ecosystem, JFreeChart is a powerful and flexible chart library that is widely used in various Java applications. This article will take you to learn from scratch how to create dynamic charts using JFreeChart, and combine actual code examples to quickly grasp its core features.
1. Introduction to JFreeChart
JFreeChart is an open source Java chart library that supports a variety of chart types, including:
Line Chart
Bar Chart
Pie Chart
Scatter Plot
Area Chart
Its main features include:
Easy to use: Create complex charts with a simple API.
Highly customizable: supports custom colors, fonts, styles, etc.
Support export: You can export charts to PNG, JPEG, PDF and other formats.
2. Environmental preparation
Before we start, we need to introduce JFreeChart's dependencies into the project. If you use Maven, you can add the following dependencies in :
<dependency> <groupId></groupId> <artifactId>jfreechart</artifactId> <version>1.5.3</version> </dependency>
3. Create the first line chart
Line charts are commonly used chart types that show data trends. Here is a simple example showing how to create a line chart using JFreeChart.
import ; import ; import ; import ; import ; import ; public class LineChartExample { public static void main(String[] args) { // Create a dataset DefaultCategoryDataset dataset = new DefaultCategoryDataset(); (1, "Series1", "Category1"); (4, "Series1", "Category2"); (3, "Series1", "Category3"); (5, "Series1", "Category4"); // Create a line chart JFreeChart chart = ( "Sample Line Chart", // Chart title "Category", // X-axis label "Value", // Y-axis label dataset // Dataset ); // Save the chart as a picture try { (new File("line_chart.png"), chart, 600, 400); ("Chart saved as line_chart.png"); } catch (IOException e) { (); } } }
Running results
After running the above code, an image file named line_chart.png will be generated, with the following content:
Title: Sample Line Chart
X-axis label: Category
Y-axis label: Value
Data points: (Category1, 1), (Category2, 4), (Category3, 3), (Category4, 5)
4. Customize the chart style
JFreeChart provides rich APIs to customize chart styles. Here are some common customization actions:
4.1 Set background color
import ; // Set the background color of the chart(); // Set the background color of the drawing areaCategoryPlot plot = (); (Color.LIGHT_GRAY);
4.2 Set the polyline color
import ; // Get the rendererLineAndShapeRenderer renderer = (LineAndShapeRenderer) (); // Set the polyline color(0, ); // The first line is red(0, new BasicStroke(2.0f)); // Set the line thickness
4.3 Set fonts (solve Chinese garbled)
import ; // Set global fontsStandardChartTheme chartTheme = new StandardChartTheme("CN"); (new Font("Songyi", , 20)); // Title Font(new Font("Songyi", , 16)); // Legend Font(new Font("Songyi", , 14)); // Axis label font(chartTheme);
4.4 Set the label width and direction of the horizontal coordinate
CategoryAxis categoryAxis = ().getDomainAxis(); (2); // Allow up to two lines per label(CategoryLabelPositions.UP_45); // Rotate the tag(0.0); // Remove edge blanks(0.0); // Remove edge blanks
5. Export the chart
JFreeChart supports exporting charts to multiple formats, such as PNG, JPEG, PDF, etc. Here is an example of saving a chart as a PNG file:
import ; // Save the chart as a PNG file(new File(""), chart, 800, 600);
6. Practical combat: Dynamically generate daily charts
Here is a practical example showing how to dynamically generate a line chart in a daily report and insert it into a Word document.
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class DailyReportExample { public static void main(String[] args) throws Exception { // Create a dataset DefaultCategoryDataset dataset = new DefaultCategoryDataset(); (10, "Monitoring Station", "00:00"); (20, "Monitoring Station", "06:00"); (30, "Monitoring Station", "12:00"); (40, "Monitoring Station", "18:00"); // Create a line chart JFreeChart chart = ( "Total number of online monitoring stations", // Chart title "Hour", // X-axis label "quantity", // Y-axis label dataset // Dataset ); // Save the chart as a picture String chartPath = "daily_chart.png"; (new File(chartPath), chart, 800, 600); // Insert the picture into the Word document Map<String, Object> dataMap = new HashMap<>(); ("chart", (new FileInputStream(chartPath), ).create()); XWPFTemplate template = ("").render(dataMap); (""); } }
Summarize
Through this article, you have mastered the basic usage of JFreeChart, including:
Create common charts such as line charts, bar charts, etc.
Customize the chart style (background color, polyline color, font, etc.).
Export the chart as a picture or insert it into a Word document.
JFreeChart is a powerful and flexible chart library suitable for a variety of data visualization needs. Hope this article helps you get started with JFreeChart quickly and apply it in real projects!
This is the end of this article about Java's code examples for creating dynamic charts using JFreeChart. For more related content on Java JFreeChart to create dynamic charts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!