Changeset 181

Show
Ignore:
Timestamp:
01/23/10 18:57:21 (2 years ago)
Author:
stefan
Message:

implemented feature #154: add legend to the diagram

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r179 r181  
    77* [stefan]  * fixed some I18N issues 
    88            * solved issue #161: design an icon for JErgometer 
     9            * implemented feature #154: add legend to the diagram 
    910 
    10112010-01-22 
  • trunk/src/org/jergometer/diagram/BikeDiagram.java

    r175 r181  
    1717      suffix = "-dest"; 
    1818    } 
    19     diagram.addGraph("pulse" + suffix, new Diagram.Graph("Pulse", Diagram.brighten(b, new Color(255,0,0, a)), s), Diagram.Side.left); 
    20     diagram.addGraph("pedalRPM" + suffix, new Diagram.Graph("Pedal RPM", Diagram.brighten(b, new Color(0,255,0, a)), s), Diagram.Side.left); 
    21     diagram.addGraph("power" + suffix, new Diagram.Graph("Power", Diagram.brighten(b, new Color(0,0,255, a)), s), Diagram.Side.left); 
    22     diagram.addGraph("performance" + suffix, new Diagram.Graph("Performance", Diagram.brighten(b, new Color(0,0,0, a)), s), Diagram.Side.left); 
     19    diagram.addGraph("pulse" + suffix, new Diagram.Graph("Pulse", Diagram.brighten(b, new Color(255,0,0, a)), s, bright), Diagram.Side.left); 
     20    diagram.addGraph("pedalRPM" + suffix, new Diagram.Graph("Pedal RPM", Diagram.brighten(b, new Color(0,255,0, a)), s, bright), Diagram.Side.left); 
     21    diagram.addGraph("power" + suffix, new Diagram.Graph("Power", Diagram.brighten(b, new Color(0,0,255, a)), s, bright), Diagram.Side.left); 
     22    diagram.addGraph("performance" + suffix, new Diagram.Graph("Performance", Diagram.brighten(b, new Color(0,0,0, a)), s, bright), Diagram.Side.left); 
    2323  } 
    2424} 
  • trunk/src/org/jergometer/gui/Diagram.java

    r180 r181  
    142142    graphs[lr.getInt()].add(graph); 
    143143    key2Graph.put(key, graph); 
     144 
     145    drawLegend(backgroundImage.createGraphics()); 
     146 
     147    redrawImage(); 
     148    repaint(); 
    144149  } 
    145150 
     
    212217      for(Graph graph : graphs[lr.getInt()]) drawGraph(g, graph, lr); 
    213218    } 
     219    drawLegend(g); 
    214220 
    215221    // axis 
     
    346352  } 
    347353 
     354  private void drawLegend(Graphics2D g) { 
     355    int width = backgroundImage.getWidth(); 
     356    int height = backgroundImage.getHeight(); 
     357 
     358    int y = height - margin.bottom + 22; 
     359    Range range = timeRange; 
     360    int minX = margin.left; 
     361    int maxX = width - margin.right; 
     362 
     363    // font metrics 
     364    FontMetrics fm = g.getFontMetrics(); 
     365 
     366    int spacing = 30; 
     367 
     368    // determine total with of the legend 
     369    int totalWidth = -10; 
     370    for (ArrayList<Graph> graphs2 : graphs) { 
     371      for (Graph graph : graphs2) { 
     372        if (!graph.hideInLegend) { 
     373          Rectangle2D bounds = fm.getStringBounds(graph.name, g); 
     374          totalWidth += bounds.getWidth() + spacing; 
     375        } 
     376      } 
     377    } 
     378 
     379    // draw legend 
     380    int x = minX + ((maxX-minX) - totalWidth)/2; 
     381    for (ArrayList<Graph> graphs2 : graphs) { 
     382      for (Graph graph : graphs2) { 
     383        if (!graph.hideInLegend) { 
     384          Rectangle2D bounds = fm.getStringBounds(graph.name, g); 
     385          g.setColor(graph.color); 
     386          g.setStroke(normalStroke); 
     387          g.drawString(graph.name, x, y + (int) bounds.getHeight() + markerSize + 1); 
     388          g.setColor(Color.BLACK); 
     389          g.setStroke(normalStroke); 
     390 
     391          x += bounds.getWidth() + spacing; 
     392        } 
     393      } 
     394    } 
     395  } 
     396 
    348397  private void drawGraph(Graphics2D g, Graph graph, Side lr) { 
    349398    g.setPaint(graph.color); 
     
    443492    public Color color; 
    444493    public Stroke stroke; 
     494    public boolean hideInLegend; 
    445495    public ArrayList<Point> timedValues; 
    446496 
    447     public Graph(String name, Color color, Stroke stroke) { 
     497    public Graph(String name, Color color, Stroke stroke, boolean hideInLegend) { 
    448498      this.name = name; 
    449499      this.color = color; 
    450500      this.stroke = stroke; 
     501      this.hideInLegend = hideInLegend; 
    451502      timedValues = new ArrayList<Point>(); 
    452503    } 
    453504 
    454505    public Graph(String name, Color color) { 
    455       this(name, color, new BasicStroke(0.5f)); 
     506      this(name, color, new BasicStroke(0.5f), false); 
    456507    } 
    457508  }