Loading repository data…
Loading repository data…
mohammadijoo / repository
How to make Plots in Java Programming Language, and a step by step guide plus a simulation video tutorial
This repository is a small but extensible playground for plotting in Java using the XChart library.
Current examples:
LinePlots.java — multiple line-plot demonstrations (multi-series plots, markers, subplots via grid layouts, etc.).Histograms.java — various histogram use-cases (different binning rules, normalization modes, categorical histograms, overlays, etc.).The layout is intentionally modular, so you can:
*.java classes.XChart is a light-weight Java plotting library that uses Swing for interactive windows and supports:
This repository uses:
XYChart for line plots (LinePlots.java).CategoryChart for histograms (Histograms.java).XChartPanel to embed charts inside Swing containers (JFrame + JTabbedPane).Below is a minimal code snippet showing how to build and display a line chart using XChart and Swing:
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XChartPanel;
import javax.swing.*;
import java.util.Arrays;
public class SimpleLineExample {
public static void main(String[] args) {
double[] x = {0.0, 1.0, 2.0, 3.0};
double[] y = {0.0, 1.0, 4.0, 9.0};
XYChart chart = new XYChartBuilder()
.width(600)
.height(400)
.title("y = x^2")
.xAxisTitle("x")
.yAxisTitle("y")
.build();
chart.addSeries("x^2", x, y);
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("XChart example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new XChartPanel<>(chart));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
In this repo, we generalize that pattern to multiple charts, all shown in a single window with tabs (for line plots and histograms).
To save a chart visually, right-click on the chart area and choose “Save As…” or use the keyboard shortcut (usually Ctrl+S) while the chart has focus.
You will need:
Download and install a JDK (e.g., from Adoptium, Oracle, etc.).
Ensure java and javac are on your PATH:
java -version
javac -version
Both commands should print a version and not “command not found”.
Download Maven from the official Apache Maven site.
Unzip/Install it and add the bin folder to your PATH.
Verify installation:
mvn -version
You should see Maven’s version and the Java version it uses.
Once Java and Maven are installed, you can clone this repository and build/run the examples directly.
There are two common ways to use XChart:
Via Maven (recommended)
Maven downloads and manages the XChart JAR automatically.
Manual JAR download (non-Maven projects)
You download the JAR and add it to your classpath manually.
In pom.xml we declare XChart as a dependency:
<dependencies>
<dependency>
<groupId>org.knowm.xchart</groupId>
<artifactId>xchart</artifactId>
<version>3.8.8</version>
</dependency>
</dependencies>
When you run mvn compile, Maven will automatically download XChart (and its transitive dependencies) into your local repository (~/.m2/repository) and place it on the classpath for compilation and execution.
If you are not using Maven, you can:
Download the xchart-3.8.8.jar file from Maven Central or the XChart website.
Compile and run with -cp (class path), for example on Windows:
javac -cp .;path\to\xchart-3.8.8.jar LinePlots.java
java -cp .;path\to\xchart-3.8.8.jar LinePlots
On Linux/macOS, the classpath separator is : instead of ;:
javac -cp .:path/to/xchart-3.8.8.jar LinePlots.java
java -cp .:path/to/xchart-3.8.8.jar LinePlots
In this repository we strongly recommend Maven, as it is already configured.
A minimal view of the project tree:
Java_Plot/
├── pom.xml
└── src
└── main
└── java
├── LinePlots.java
└── Histograms.java
pom.xml
Maven configuration file. Declares the project coordinates (groupId, artifactId, version), dependencies (XChart), and the exec-maven-plugin to run Java main classes from the command line.
src/main/java/LinePlots.java
Contains all line plot examples.
It builds multiple XYChart objects and shows them in a single JFrame with a JTabbedPane (tabs 1–6).
src/main/java/Histograms.java
Contains all histogram examples.
It builds multiple CategoryChart objects, including a 2×3 grid of charts for comparing binning rules, again shown in a tabbed window.
You can add more plot examples by creating new Java files in src/main/java/ and wiring them via Maven or by running them directly.
All commands below assume you are in the project root (Java_Plot), e.g., in Git Bash:
cd ~/Desktop/Java_Plot
mvn compile
This:
src/main/java/ into target/classes/.pom.xml is configured with an exec-maven-plugin that defines a named execution for line plots:
<execution>
<id>line-plots</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>LinePlots</mainClass>
</configuration>
</execution>
To run it:
mvn exec:java@line-plots
This launches the line-plot GUI (LinePlots.main) with one window and 6 tabs.
Similarly, pom.xml contains:
<execution>
<id>histograms</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Histograms</mainClass>
</configuration>
</execution>
To run it:
mvn exec:java@histograms
This launches the histogram GUI (Histograms.main) with one window and 7 tabs (one of them is a 2×3 grid).
Although Maven is recommended, you can still compile and run manually with javac and java, provided that XChart is on the classpath.
Assume that:
Java_Plot/.lib/xchart-3.8.8.jar (you may choose a different path).From Java_Plot:
javac -cp .;lib\xchart-3.8.8.jar src\main\java\LinePlots.java src\main\java\Histograms.java
On Linux/macOS, use : as the separator:
javac -cp .:lib/xchart-3.8.8.jar src/main/java/LinePlots.java src/main/java/Histograms.java
This will place .class files alongside the .java files unless you specify -d for a custom output directory.
On Windows:
java -cp .;lib\xchart-3.8.8.jar;src\main\java LinePlots
java -cp .;lib\xchart-3.8.8.jar;src\main\java Histograms
On Linux/macOS:
java -cp .:lib/xchart-3.8.8.jar:src/main/java LinePlots
java -cp .:lib/xchart-3.8.8.jar:src/main/java Histograms
Note: Using Maven avoids all of this manual classpath management, which is why the project is Maven-based.
Two commonly used Maven commands:
mvn compilesrc/main/java into the target/classes directory.target directory.mvn clean compilemvn clean deletes the target directory completely.compile from a fresh state.pom.xml..class files causing odd behavior.As a rule of thumb:
mvn compile and then mvn exec:java@...mvn clean compileYou do not need to manually delete the target folder; Maven’s clean goal takes care of that for you.
LinePlots.java is the Java code for making line plots in Java. It showcases multiple line-plot patterns using XChart and a Swing tabbed window.
main(String[] args)
createMultipleLineChart()