Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

135
LINES

< > BotCompany Repo | #1013396 // GraphPanel - simple line graph

JavaX fragment (include)

1  
/**
2  
 * @author Rodrigo
3  
 * https://gist.github.com/roooodcastro/6325153
4  
 */
5  
sclass GraphPanel extends JPanel {
6  
  int width = 800;
7  
  int heigth = 400;
8  
  int padding = 25;
9  
  int labelPadding = 25;
10  
  Color lineColor = new Color(44, 102, 230, 180);
11  
  Color pointColor = new Color(100, 100, 100, 180);
12  
  Color gridColor = new Color(200, 200, 200, 200);
13  
  static final Stroke GRAPH_STROKE = new BasicStroke(2f);
14  
  int pointWidth = 4;
15  
  int numberYDivisions = 10;
16  
  List<Double> scores;
17  
18  
  public GraphPanel(List<Double> scores) {
19  
      this.scores = scores;
20  
  }
21  
22  
  @Override
23  
  protected void paintComponent(Graphics g) {
24  
      super.paintComponent(g);
25  
      Graphics2D g2 = (Graphics2D) g;
26  
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
27  
28  
      double xScale = ((double) getWidth() - (2 * padding) - labelPadding) / (scores.size() - 1);
29  
      double yScale = ((double) getHeight() - 2 * padding - labelPadding) / (getMaxScore() - getMinScore());
30  
31  
      List<Point> graphPoints = new ArrayList<>();
32  
      for (int i = 0; i < scores.size(); i++) {
33  
          int x1 = (int) (i * xScale + padding + labelPadding);
34  
          int y1 = (int) ((getMaxScore() - scores.get(i)) * yScale + padding);
35  
          graphPoints.add(new Point(x1, y1));
36  
      }
37  
38  
      // draw white background
39  
      g2.setColor(Color.WHITE);
40  
      g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);
41  
      g2.setColor(Color.BLACK);
42  
43  
      // create hatch marks and grid lines for y axis.
44  
      for (int i = 0; i < numberYDivisions + 1; i++) {
45  
          int x0 = padding + labelPadding;
46  
          int x1 = pointWidth + padding + labelPadding;
47  
          int y0 = getHeight() - ((i * (getHeight() - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
48  
          int y1 = y0;
49  
          if (scores.size() > 0) {
50  
              g2.setColor(gridColor);
51  
              g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
52  
              g2.setColor(Color.BLACK);
53  
              String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + "";
54  
              FontMetrics metrics = g2.getFontMetrics();
55  
              int labelWidth = metrics.stringWidth(yLabel);
56  
              g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
57  
          }
58  
          g2.drawLine(x0, y0, x1, y1);
59  
      }
60  
61  
      // and for x axis
62  
      for (int i = 0; i < scores.size(); i++) {
63  
          if (scores.size() > 1) {
64  
              int x0 = i * (getWidth() - padding * 2 - labelPadding) / (scores.size() - 1) + padding + labelPadding;
65  
              int x1 = x0;
66  
              int y0 = getHeight() - padding - labelPadding;
67  
              int y1 = y0 - pointWidth;
68  
              if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) {
69  
                  g2.setColor(gridColor);
70  
                  g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
71  
                  g2.setColor(Color.BLACK);
72  
                  String xLabel = i + "";
73  
                  FontMetrics metrics = g2.getFontMetrics();
74  
                  int labelWidth = metrics.stringWidth(xLabel);
75  
                  g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
76  
              }
77  
              g2.drawLine(x0, y0, x1, y1);
78  
          }
79  
      }
80  
81  
      // create x and y axes 
82  
      g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
83  
      g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);
84  
85  
      Stroke oldStroke = g2.getStroke();
86  
      g2.setColor(lineColor);
87  
      g2.setStroke(GRAPH_STROKE);
88  
      for (int i = 0; i < graphPoints.size() - 1; i++) {
89  
          int x1 = graphPoints.get(i).x;
90  
          int y1 = graphPoints.get(i).y;
91  
          int x2 = graphPoints.get(i + 1).x;
92  
          int y2 = graphPoints.get(i + 1).y;
93  
          g2.drawLine(x1, y1, x2, y2);
94  
      }
95  
96  
      g2.setStroke(oldStroke);
97  
      g2.setColor(pointColor);
98  
      for (int i = 0; i < graphPoints.size(); i++) {
99  
          int x = graphPoints.get(i).x - pointWidth / 2;
100  
          int y = graphPoints.get(i).y - pointWidth / 2;
101  
          int ovalW = pointWidth;
102  
          int ovalH = pointWidth;
103  
          g2.fillOval(x, y, ovalW, ovalH);
104  
      }
105  
  }
106  
107  
//    @Override
108  
//    public Dimension getPreferredSize() {
109  
//        return new Dimension(width, heigth);
110  
//    }
111  
112  
  double getMinScore() {
113  
      double minScore = Double.MAX_VALUE;
114  
      for (Double score : scores) {
115  
          minScore = Math.min(minScore, score);
116  
      }
117  
      return minScore;
118  
  }
119  
120  
  double getMaxScore() {
121  
      double maxScore = Double.MIN_VALUE;
122  
      for (Double score : scores) {
123  
          maxScore = Math.max(maxScore, score);
124  
      }
125  
      return maxScore;
126  
  }
127  
128  
  public void setScores(List<Double> scores) {
129  
      this.scores = scores;
130  
      invalidate();
131  
      repaint();
132  
  }
133  
134  
  public List<Double> getScores() { return scores; }
135  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013396
Snippet name: GraphPanel - simple line graph
Eternal ID of this version: #1013396/2
Text MD5: 03a29ad6eb08c57116cd5d695900b583
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-05 01:29:32
Source code size: 5268 bytes / 135 lines
Pitched / IR pitched: No / No
Views / Downloads: 399 / 966
Version history: 1 change(s)
Referenced in: [show references]