How to Create a Custom Line Chart In Java Netbeans
In this Java Tutorial we will see How To Create a Custom Line Chart from scratch with labeled axes, grid lines, and data points using graphics class and jpanel in java netbeans.
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package linechart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author 1BestCsharp
*/
public class LineChart extends JPanel{
// Constants for padding and labels
private final int padding = 30;
private final int labelPadding = 40;
private final int axisLabelPadding = 20;
private final int maxVerticalLabels = 5;
private final int maxHorizontalLabels = 5;
// Arrays for vertical and horizontal labels
private String[] verticalLabels = {"0","25","50","75","100"};
private String[] horizontalLabels = {"Label 1","Label 2","Label 3","Label 4","Label 5"};
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Calculate coordinates and dimensions
int width = getWidth();
int height = getHeight();
int xStart = padding + labelPadding + axisLabelPadding;
int yStart = height - padding - labelPadding;
int xEnd = width - padding;
int yEnd = padding + labelPadding;
// Calculate step size for vertical and horizontal labels
int yStep = (yStart - yEnd) / (maxVerticalLabels - 1);
int xStep = (xEnd - xStart) / (maxHorizontalLabels - 1);
// Draw vertical labels
for(int i = 0; i < maxVerticalLabels; i++){
int y = yStart - i * yStep;
drawVerticalLabel(g2d, verticalLabels[i], padding / 2, y);
}
// Draw horizontal labels
for(int i = 0; i < maxHorizontalLabels; i++){
int x = xStart + i * xStep;
//drawHorizontal(g2d, horizontalLabels[i], x, height - (padding / 2));
drawHorizontalLabel(g2d, horizontalLabels[i], x, height - (padding / 2));
}
// Data for the chart
int[] data = {40, 100, 75, 20, 55};
int maxValue = 100;
// Find the maximum value in the data
for(int value : data){
if(value > maxValue)
{
maxValue = value;
}
}
// Draw chart background
g2d.setColor(Color.white);
g2d.fillRect(xStart, yEnd, xEnd - xStart, yStart - yEnd);
// Draw chart border
g2d.setColor(Color.black);
g2d.drawRect(xStart, yEnd, xEnd - xStart, yStart - yEnd);
// Draw horizontal grid lines
int yGridCount = 5;
for(int i = 0; i < yGridCount; i++){
int y = yStart - i * yStep;
g2d.setColor(Color.red);
g2d.drawLine(xStart, y, xEnd, y);
}
// Draw vertical grid lines
int xGridCount = data.length;
for(int i = 0; i < xGridCount; i++){
int x = xStart + i * xStep;
g2d.setColor(Color.lightGray);
g2d.drawLine(x, yEnd, x, yStart);
}
// Plot data points and connect with lines
for(int i = 0; i < data.length; i++){
g2d.setColor(Color.orange);
int x = xStart + i * xStep;
int y = yStart - data[i] * (yStart - yEnd) / maxValue;
g2d.fillOval(x-5, y-5, 10, 10);
// Connect data points with lines
if( i > 0){
int prevX = xStart + (i - 1) * xStep;
int prevY = yStart - data[i - 1] * (yStart - yEnd) / maxValue;
g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.gray);
g2d.drawLine(prevX, prevY, x, y);
}
}
// Draw Chart Title
g2d.setFont(new Font("Arial",Font.BOLD, 20));
g2d.setColor(Color.red);
String chartTitle = "Chart Title";
g2d.drawString(chartTitle, (width - g2d.getFontMetrics().stringWidth(chartTitle)) / 2, padding);
}
// Method to draw a vertical label with rotation
private void drawVerticalLabel(Graphics2D g2d, String label, int x, int y){
AffineTransform atf = new AffineTransform();
atf.rotate(-Math.PI / 2);
Font rotatedFont = g2d.getFont().deriveFont(atf);
g2d.setFont(rotatedFont);
g2d.setColor(Color.yellow);
g2d.drawString(label, x + 30, y);
g2d.setFont(g2d.getFont().deriveFont(atf));
}
// Method to draw a horizontal label
private void drawHorizontalLabel(Graphics2D g2d, String label, int x, int y){
g2d.setColor(new Color(248,239,186));
g2d.drawString(label, x - g2d.getFontMetrics().stringWidth(label) / 2, y);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Custom Line Chart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
LineChart chartPanel = new LineChart();
chartPanel.setBackground(Color.darkGray);
frame.add(chartPanel);
frame.setVisible(true);
}
}
The Final Result:
More Java Projects:
Download Projects Source Code