how to draw a graph in java

Solutions on MaxInterview for how to draw a graph in java by the best coders in the world

showing results for - "how to draw a graph in java"
Mariana
21 Sep 2020
1import java.awt.BasicStroke;
2import java.awt.Color;
3import java.awt.Dimension;
4import java.awt.Graphics;
5import java.awt.Graphics2D;
6import java.awt.Point;
7import java.awt.RenderingHints;
8import java.awt.Stroke;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.Random;
12import javax.swing.*;
13
14@SuppressWarnings("serial")
15public class DrawGraph extends JPanel {
16   private static final int MAX_SCORE = 20;
17   private static final int PREF_W = 800;
18   private static final int PREF_H = 650;
19   private static final int BORDER_GAP = 30;
20   private static final Color GRAPH_COLOR = Color.green;
21   private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180);
22   private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
23   private static final int GRAPH_POINT_WIDTH = 12;
24   private static final int Y_HATCH_CNT = 10;
25   private List<Integer> scores;
26
27   public DrawGraph(List<Integer> scores) {
28      this.scores = scores;
29   }
30
31   @Override
32   protected void paintComponent(Graphics g) {
33      super.paintComponent(g);
34      Graphics2D g2 = (Graphics2D)g;
35      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
36
37      double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.size() - 1);
38      double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_SCORE - 1);
39
40      List<Point> graphPoints = new ArrayList<Point>();
41      for (int i = 0; i < scores.size(); i++) {
42         int x1 = (int) (i * xScale + BORDER_GAP);
43         int y1 = (int) ((MAX_SCORE - scores.get(i)) * yScale + BORDER_GAP);
44         graphPoints.add(new Point(x1, y1));
45      }
46
47      // create x and y axes 
48      g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
49      g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP);
50
51      // create hatch marks for y axis. 
52      for (int i = 0; i < Y_HATCH_CNT; i++) {
53         int x0 = BORDER_GAP;
54         int x1 = GRAPH_POINT_WIDTH + BORDER_GAP;
55         int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
56         int y1 = y0;
57         g2.drawLine(x0, y0, x1, y1);
58      }
59
60      // and for x axis
61      for (int i = 0; i < scores.size() - 1; i++) {
62         int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores.size() - 1) + BORDER_GAP;
63         int x1 = x0;
64         int y0 = getHeight() - BORDER_GAP;
65         int y1 = y0 - GRAPH_POINT_WIDTH;
66         g2.drawLine(x0, y0, x1, y1);
67      }
68
69      Stroke oldStroke = g2.getStroke();
70      g2.setColor(GRAPH_COLOR);
71      g2.setStroke(GRAPH_STROKE);
72      for (int i = 0; i < graphPoints.size() - 1; i++) {
73         int x1 = graphPoints.get(i).x;
74         int y1 = graphPoints.get(i).y;
75         int x2 = graphPoints.get(i + 1).x;
76         int y2 = graphPoints.get(i + 1).y;
77         g2.drawLine(x1, y1, x2, y2);         
78      }
79
80      g2.setStroke(oldStroke);      
81      g2.setColor(GRAPH_POINT_COLOR);
82      for (int i = 0; i < graphPoints.size(); i++) {
83         int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
84         int y = graphPoints.get(i).y - GRAPH_POINT_WIDTH / 2;;
85         int ovalW = GRAPH_POINT_WIDTH;
86         int ovalH = GRAPH_POINT_WIDTH;
87         g2.fillOval(x, y, ovalW, ovalH);
88      }
89   }
90
91   @Override
92   public Dimension getPreferredSize() {
93      return new Dimension(PREF_W, PREF_H);
94   }
95
96   private static void createAndShowGui() {
97      List<Integer> scores = new ArrayList<Integer>();
98      Random random = new Random();
99      int maxDataPoints = 16;
100      int maxScore = 20;
101      for (int i = 0; i < maxDataPoints ; i++) {
102         scores.add(random.nextInt(maxScore));
103      }
104      DrawGraph mainPanel = new DrawGraph(scores);
105
106      JFrame frame = new JFrame("DrawGraph");
107      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
108      frame.getContentPane().add(mainPanel);
109      frame.pack();
110      frame.setLocationByPlatform(true);
111      frame.setVisible(true);
112   }
113
114   public static void main(String[] args) {
115      SwingUtilities.invokeLater(new Runnable() {
116         public void run() {
117            createAndShowGui();
118         }
119      });
120   }
121}