Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Generate a Graph using Java?

Generate Graph in Java

Condition for Generating a Graph in Java

  • Description: To generate a graph in Java, the JFreeChart library can be used, which provides a wide range of charting and graphing capabilities. First, include the JFreeChart library in the project, either by downloading it or by using a build tool like Maven. A dataset, such as `DefaultCategoryDataset` for bar charts or `XYSeries` for scatter plots, is created to hold the data that will be visualized. The graph is then created by passing the dataset to the corresponding chart type (e.g., `JFreeChart` for a line chart, bar chart, or pie chart). After the chart is created, it is added to a `ChartPanel`, which is a Swing component that displays the chart. The `ChartPanel` is then added to a `JFrame` to render the graph in the application window. Additional customization, such as axis labels, titles, and colors, can also be applied to enhance the graph's appearance.
Sample Source Code
  • # Graph.java
    package JavaSamples2;

    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class Graph extends JPanel {
    int data[] = new int[50];
    final int PAD = 50;

    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (int i = 0; i < 50; i++) {
    data[i] = i;
    }

    int w = getWidth();
    int h = getHeight();

    g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));

    g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));

    Font font = g2.getFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("0", frc);
    float sh = lm.getAscent() + lm.getDescent();

    String s = "y axis";
    float sy = PAD + ((h - 2 * PAD) - s.length() * sh) / 2 + lm.getAscent();
    for (int i = 0; i < s.length(); i++) {
    String letter = String.valueOf(s.charAt(i));
    float sw = (float) font.getStringBounds(letter, frc).getWidth();
    float sx = (PAD - sw) / 2;
    g2.drawString(letter, sx, sy);
    sy += sh;
    }

    s = "x axis";
    sy = h - PAD + (PAD - sh) / 2 + lm.getAscent();
    float sw = (float) font.getStringBounds(s, frc).getWidth();
    float sx = (w - sw) / 2;
    g2.drawString(s, sx, sy);

    double xInc = (double) (w - 2 * PAD) / (data.length - 1);
    double scale = (double) (h - 2 * PAD) / getMax();
    g2.setPaint(Color.green.darker());

    for (int i = 0; i < data.length - 1; i++) {
    double x1 = PAD + i * xInc;
    double y1 = h - PAD - scale * data[i];
    double x2 = PAD + (i + 1) * xInc;
    double y2 = h - PAD - scale * data[i + 1];
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    }

    g2.setPaint(Color.blue);
    for (int i = 0; i < data.length; i++) {
    double x = PAD + i * xInc;
    double y = h - PAD - scale * data[i];
    g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
    }
    }

    private int getMax() {
    int max = -Integer.MAX_VALUE;
    for (int i = 0; i < data.length; i++) {
    if (data[i] > max) {
    max = data[i];
    }
    }
    return max;
    }

    public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new Graph());
    f.setSize(600, 600);
    f.setLocation(200, 200);
    f.setVisible(true);
    }
    }
Screenshots
  • STEP 1: The graph is populated with data points and lines connecting them.
  • The graph is populated with data points and lines connecting them.