From 5af7b8c289f3e4e2322fe4025cce9cbdaa24a6c1 Mon Sep 17 00:00:00 2001 From: Francesco Galgani <1997316+jsfan3@users.noreply.github.com> Date: Wed, 6 Mar 2024 16:11:04 +0700 Subject: [PATCH] https://jira.baeldung.com/browse/BAEL-7575 (#15956) * Update pom.xml - https://jira.baeldung.com/browse/BAEL-7575 https://jira.baeldung.com/browse/BAEL-7575 * Create ignore-this-file - https://jira.baeldung.com/browse/BAEL-7575 https://jira.baeldung.com/browse/BAEL-7575 * https://jira.baeldung.com/browse/BAEL-7575 https://jira.baeldung.com/browse/BAEL-7575 * Delete libraries-4/src/main/java/com/baeldung/jfreechart/ignore-this-file https://jira.baeldung.com/browse/BAEL-7575 * Update pom.xml --- libraries-4/pom.xml | 8 ++- .../baeldung/jfreechart/BarChartExample.java | 37 +++++++++++ .../jfreechart/CombinationChartExample.java | 61 +++++++++++++++++++ .../baeldung/jfreechart/LineChartExample.java | 37 +++++++++++ .../baeldung/jfreechart/PieChartExample.java | 36 +++++++++++ .../jfreechart/TimeSeriesChartExample.java | 43 +++++++++++++ 6 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 libraries-4/src/main/java/com/baeldung/jfreechart/BarChartExample.java create mode 100644 libraries-4/src/main/java/com/baeldung/jfreechart/CombinationChartExample.java create mode 100644 libraries-4/src/main/java/com/baeldung/jfreechart/LineChartExample.java create mode 100644 libraries-4/src/main/java/com/baeldung/jfreechart/PieChartExample.java create mode 100644 libraries-4/src/main/java/com/baeldung/jfreechart/TimeSeriesChartExample.java diff --git a/libraries-4/pom.xml b/libraries-4/pom.xml index 165f017d89..90ffb8da37 100644 --- a/libraries-4/pom.xml +++ b/libraries-4/pom.xml @@ -98,6 +98,11 @@ commons-lang3 ${commons-lang3.version} + + org.jfree + jfreechart + ${jfreechart.version} + @@ -117,6 +122,7 @@ 19 10.3.0 0.9.0 + 1.5.4 - \ No newline at end of file + diff --git a/libraries-4/src/main/java/com/baeldung/jfreechart/BarChartExample.java b/libraries-4/src/main/java/com/baeldung/jfreechart/BarChartExample.java new file mode 100644 index 0000000000..9005746b63 --- /dev/null +++ b/libraries-4/src/main/java/com/baeldung/jfreechart/BarChartExample.java @@ -0,0 +1,37 @@ +package com.baeldung.jfreechart; + +import javax.swing.JFrame; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.data.category.DefaultCategoryDataset; + +public class BarChartExample { + + public static void main(String[] args) { + // Create a dataset + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + dataset.addValue(200, "Sales", "January"); + dataset.addValue(150, "Sales", "February"); + dataset.addValue(180, "Sales", "March"); + dataset.addValue(260, "Sales", "April"); + dataset.addValue(300, "Sales", "May"); + + // Create a chart using the dataset + JFreeChart chart = ChartFactory.createBarChart( + "Monthly Sales", // Chart title + "Month", // X-axis label + "Sales", // Y-axis label + dataset); // data + + // Display the chart + ChartPanel chartPanel = new ChartPanel(chart); + JFrame frame = new JFrame(); + frame.setSize(800, 600); + frame.setContentPane(chartPanel); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/libraries-4/src/main/java/com/baeldung/jfreechart/CombinationChartExample.java b/libraries-4/src/main/java/com/baeldung/jfreechart/CombinationChartExample.java new file mode 100644 index 0000000000..4143814ba9 --- /dev/null +++ b/libraries-4/src/main/java/com/baeldung/jfreechart/CombinationChartExample.java @@ -0,0 +1,61 @@ +package com.baeldung.jfreechart; + +import java.awt.Font; + +import javax.swing.JFrame; + +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.CategoryAxis; +import org.jfree.chart.axis.NumberAxis; +import org.jfree.chart.plot.CategoryPlot; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.renderer.category.BarRenderer; +import org.jfree.chart.renderer.category.LineAndShapeRenderer; +import org.jfree.data.category.DefaultCategoryDataset; + +public class CombinationChartExample { + + public static void main(String[] args) { + // Create datasets + DefaultCategoryDataset lineDataset = new DefaultCategoryDataset(); + lineDataset.addValue(200, "Sales", "January"); + lineDataset.addValue(150, "Sales", "February"); + lineDataset.addValue(180, "Sales", "March"); + + DefaultCategoryDataset barDataset = new DefaultCategoryDataset(); + barDataset.addValue(400, "Profit", "January"); + barDataset.addValue(300, "Profit", "February"); + barDataset.addValue(250, "Profit", "March"); + + // Create a combination chart + CategoryPlot plot = new CategoryPlot(); + plot.setDataset(0, lineDataset); + plot.setRenderer(0, new LineAndShapeRenderer()); + + plot.setDataset(1, barDataset); + plot.setRenderer(1, new BarRenderer()); + + plot.setDomainAxis(new CategoryAxis("Month")); + plot.setRangeAxis(new NumberAxis("Value")); + + plot.setOrientation(PlotOrientation.VERTICAL); + plot.setRangeGridlinesVisible(true); + plot.setDomainGridlinesVisible(true); + + JFreeChart chart = new JFreeChart( + "Monthly Sales and Profit", // chart title + null, // null means to use default font + plot, // combination chart as CategoryPlot + true); // legend + + // Display the chart + ChartPanel chartPanel = new ChartPanel(chart); + JFrame frame = new JFrame(); + frame.setSize(800, 600); + frame.setContentPane(chartPanel); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/libraries-4/src/main/java/com/baeldung/jfreechart/LineChartExample.java b/libraries-4/src/main/java/com/baeldung/jfreechart/LineChartExample.java new file mode 100644 index 0000000000..795a65f841 --- /dev/null +++ b/libraries-4/src/main/java/com/baeldung/jfreechart/LineChartExample.java @@ -0,0 +1,37 @@ +package com.baeldung.jfreechart; + +import javax.swing.JFrame; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.data.category.DefaultCategoryDataset; + +public class LineChartExample { + + public static void main(String[] args) { + // Create a dataset + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + dataset.addValue(200, "Sales", "January"); + dataset.addValue(150, "Sales", "February"); + dataset.addValue(180, "Sales", "March"); + dataset.addValue(260, "Sales", "April"); + dataset.addValue(300, "Sales", "May"); + + // Create a chart using the dataset + JFreeChart chart = ChartFactory.createLineChart( + "Monthly Sales", // Chart title + "Month", // X-axis label + "Sales", // Y-axis label + dataset); // data + + // Display the chart + ChartPanel chartPanel = new ChartPanel(chart); + JFrame frame = new JFrame(); + frame.setSize(800, 600); + frame.setContentPane(chartPanel); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/libraries-4/src/main/java/com/baeldung/jfreechart/PieChartExample.java b/libraries-4/src/main/java/com/baeldung/jfreechart/PieChartExample.java new file mode 100644 index 0000000000..ed0cff99c8 --- /dev/null +++ b/libraries-4/src/main/java/com/baeldung/jfreechart/PieChartExample.java @@ -0,0 +1,36 @@ +package com.baeldung.jfreechart; + +import javax.swing.JFrame; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.data.general.DefaultPieDataset; + +public class PieChartExample { + + public static void main(String[] args) { + // Create a dataset + DefaultPieDataset dataset = new DefaultPieDataset<>(); + dataset.setValue("January", 200); + dataset.setValue("February", 150); + dataset.setValue("March", 180); + + // Create a chart using the dataset + JFreeChart chart = ChartFactory.createPieChart( + "Monthly Sales", // Chart title + dataset, // data + true, // include legend + true, // generate tool tips + false); // no URLs + + // Display the chart + ChartPanel chartPanel = new ChartPanel(chart); + JFrame frame = new JFrame(); + frame.setSize(800, 600); + frame.setContentPane(chartPanel); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/libraries-4/src/main/java/com/baeldung/jfreechart/TimeSeriesChartExample.java b/libraries-4/src/main/java/com/baeldung/jfreechart/TimeSeriesChartExample.java new file mode 100644 index 0000000000..60339823e7 --- /dev/null +++ b/libraries-4/src/main/java/com/baeldung/jfreechart/TimeSeriesChartExample.java @@ -0,0 +1,43 @@ +package com.baeldung.jfreechart; + +import javax.swing.JFrame; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.data.time.Month; +import org.jfree.data.time.TimeSeries; +import org.jfree.data.time.TimeSeriesCollection; + +public class TimeSeriesChartExample { + + public static void main(String[] args) { + // Create a dataset + TimeSeries series = new TimeSeries("Monthly Sales"); + series.add(new Month(1, 2024), 200); + series.add(new Month(2, 2024), 150); + series.add(new Month(3, 2024), 180); + + TimeSeriesCollection dataset = new TimeSeriesCollection(); + dataset.addSeries(series); + + // Create a chart using the dataset + JFreeChart chart = ChartFactory.createTimeSeriesChart( + "Monthly Sales", // Chart title + "Date", // X-axis label + "Sales", // Y-axis label + dataset, // data + true, // legend + false, // tooltips + false); // no URLs + + // Display the chart + ChartPanel chartPanel = new ChartPanel(chart); + JFrame frame = new JFrame(); + frame.setSize(800, 600); + frame.setContentPane(chartPanel); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} \ No newline at end of file