Android Open Source - AnkiStats Bar Graph






From Project

Back to project page AnkiStats.

License

The source code is released under:

GNU General Public License

If you think the Android project AnkiStats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * /*from  ww  w  . ja  v  a2s  .  c o  m*/
 */
package com.wildplot.android.rendering;

import com.wildplot.android.rendering.graphics.wrapper.Color;
import com.wildplot.android.rendering.graphics.wrapper.Graphics;
import com.wildplot.android.rendering.graphics.wrapper.Rectangle;
import com.wildplot.android.rendering.interfaces.Drawable;
import com.wildplot.android.rendering.interfaces.Function2D;
import com.wildplot.android.rendering.interfaces.Legendable;


/**
 * BarGraph uses a point matrix or a function to render bar graphs on PlotSheet object
 * 
 *
 */
public class BarGraph implements Drawable, Legendable {

    private String mName = "";
    private boolean mNameIsSet = false;
  
  private PlotSheet plotSheet;
  
  private double[][] points;
  
  private Function2D function;
  
  private double size = 1;
  
  private boolean hasFunction = false;
  
  private double steps = 1;
  
  private Color color;
  
  private Color fillColor;
  
  private boolean filling = false;
  
  /**
   * Constructor for BarGraph object
   * @param plotSheet the sheet the bar will be drawn onto
   * @param size absolute x-width of the bar
   * @param points start points (x,y) from each bar
   * @param color color of the bar
   */
  public BarGraph(PlotSheet plotSheet, double size, double[][] points, Color color){
    this.plotSheet = plotSheet;
    this.size = size;
    this.points = points;
    this.color = color;
  }
  
  /**
   * Constructor for BarGraph object
   * @param plotSheet the sheet the bar will be drawn onto
   * @param size absolute x-width of the bar
   * @param function given function of the bar graph
   * @param color color of the bar
   */
  public BarGraph(PlotSheet plotSheet, double size, Function2D function, Color color){
    this.plotSheet = plotSheet;
    this.size = size;
    this.function = function;
    this.hasFunction = true;
    this.color = color;
  }
  
  /**
   * Constructor for BarGraph object
   * @param plotSheet the sheet the bar will be drawn onto
   * @param size absolute x-width of the bar
   * @param function given function of the bar graph
   * @param steps step-width for the bar graph
   * @param color color of the bar
   */
  public BarGraph(PlotSheet plotSheet, double size, Function2D function, double steps, Color color){
    this.plotSheet = plotSheet;
    this.size = size;
    this.function = function;
    this.steps = steps;
    this.hasFunction = true;
    this.color = color;
  }
  
  /**
   * Set filling for a bar graph true or false
   */
  public void setFilling(boolean filling) {
    this.filling = filling;
    if(this.fillColor == null) {
      this.fillColor = this.color;
    }
  }
  
  /**
   * Set filling color for bar graph 
   * @param fillColor of the bar graph
   */
  public void setFillColor(Color fillColor) {
    this.fillColor = fillColor;
  }

  /* (non-Javadoc)
   * @see rendering.Drawable#paint(java.awt.Graphics)
   */
  @Override
  public void paint(Graphics g) {
    Color oldColor = g.getColor();
    Rectangle field = g.getClipBounds();
    g.setColor(color);
    
    if(this.hasFunction) {
      
      double tmp =  (int)((0-plotSheet.getxRange()[0])/this.steps);
      tmp = (0.0 - tmp*this.steps); 
      
      while(tmp <= plotSheet.getxRange()[1]) {
        drawBar(tmp, function.f(tmp), g, field);
        tmp += this.steps;
      }
      
    } else {
      for(int i = 0; i<this.points[0].length; i++) {
        if(points.length == 3) {
          drawBar(points[0][i], points[1][i], g, field, points[2][i]);
        } else {
          drawBar(points[0][i], points[1][i], g, field);
        }
      }
      
    }
    
    
    
    g.setColor(oldColor);

  }
  
  /**
   * draw a single bar at given coordinates with given graphics object and bounds 
   * @param x x-coordinate of bar
   * @param y height of bar
   * @param g graphics object for drawing
   * @param field bounds of plot
   */
  private void drawBar(double x, double y, Graphics g, Rectangle field) {
    drawBar(x,y,g,field,this.size);
  }
  
  /**
   * draw a single bar at given coordinates with given graphics object and bounds and specific size
   * @param x x-coordinate of bar
   * @param y height of bar
   * @param g graphics object for drawing
   * @param field bounds of plot
   * @param size specific size for this bar
   */
  private void drawBar(double x, double y, Graphics g, Rectangle field, double size) {


        float[] pointUpLeft     = plotSheet.toGraphicPoint(x-size/2, y, field);
        float[] pointUpRight     = plotSheet.toGraphicPoint(x+size/2, y, field);
        float[] pointBottomLeft   = plotSheet.toGraphicPoint(x-size/2, 0, field);
    
    if(filling){
      Color oldColor = g.getColor();
      if(this.fillColor != null)
        g.setColor(fillColor);
      
      if(y<0) {
        g.fillRect(pointUpLeft[0], plotSheet.yToGraphic(0, field), pointUpRight[0]-pointUpLeft[0], pointUpLeft[1]- pointBottomLeft[1]);
      } else {
        g.fillRect(pointUpLeft[0], pointUpLeft[1], pointUpRight[0]-pointUpLeft[0], pointBottomLeft[1]-pointUpLeft[1]);
      }
      //g.fillRect(pointUpLeft[0], pointUpLeft[1], pointUpRight[0]-pointUpLeft[0], pointBottomLeft[1]-pointUpLeft[1]);
      
      g.setColor(oldColor);
    }
    
    if(y<0) {
      g.drawRect(pointUpLeft[0], plotSheet.yToGraphic(0, field), pointUpRight[0]-pointUpLeft[0], pointUpLeft[1]- pointBottomLeft[1]);
    } else {
      g.drawRect(pointUpLeft[0], pointUpLeft[1], pointUpRight[0]-pointUpLeft[0], pointBottomLeft[1]-pointUpLeft[1]);
    }
    
//    g.drawLine(pointUpLeft[0], pointUpLeft[1], pointUpRight[0], pointUpRight[1]);
//    g.drawLine(pointUpLeft[0], pointUpLeft[1], pointBottomLeft[0], pointBottomLeft[1]);
//    g.drawLine(pointBottomRight[0], pointBottomRight[1], pointBottomLeft[0], pointBottomLeft[1]);
    
    
    
  }
  
  /**
   * returns true if this BarGraph can draw on the outer frame of plot (normally not)
   */
  public boolean isOnFrame() {
    return false;
  }

  @Override
  public void abortAndReset() {
    // TODO Auto-generated method stub
    
  }

    @Override
    public boolean isClusterable() {
        return true;
    }

    @Override
    public boolean isCritical() {
        return false;
    }

    @Override
    public Color getColor() {
        return fillColor;
    }

    @Override
    public String getName() {
        return mName;
    }

    @Override
    public boolean nameIsSet() {
        return mNameIsSet;
    }

    public void setName(String name){
        mName = name;
        mNameIsSet = true;
    }
}




Java Source Code List

com.wildplot.android.ankistats.AnkiDb.java
com.wildplot.android.ankistats.AnkiStatsActivity.java
com.wildplot.android.ankistats.AnkiStatsApplication.java
com.wildplot.android.ankistats.AnswerButton.java
com.wildplot.android.ankistats.ApplicationTest.java
com.wildplot.android.ankistats.CardsTypes.java
com.wildplot.android.ankistats.CollectionData.java
com.wildplot.android.ankistats.Forecast.java
com.wildplot.android.ankistats.HourlyBreakdown.java
com.wildplot.android.ankistats.Intervals.java
com.wildplot.android.ankistats.ReviewCount.java
com.wildplot.android.ankistats.Utils.java
com.wildplot.android.ankistats.WeeklyBreakdown.java
com.wildplot.android.parsing.Atom.java
com.wildplot.android.parsing.ExpressionFormatException.java
com.wildplot.android.parsing.Expression.java
com.wildplot.android.parsing.Factor.java
com.wildplot.android.parsing.Pow.java
com.wildplot.android.parsing.Term.java
com.wildplot.android.parsing.TopLevelParser.java
com.wildplot.android.parsing.TreeElement.java
com.wildplot.android.parsing.AtomTypes.FunctionXAtom.java
com.wildplot.android.parsing.AtomTypes.FunctionXYAtom.java
com.wildplot.android.parsing.AtomTypes.MathFunctionAtom.java
com.wildplot.android.parsing.AtomTypes.NumberAtom.java
com.wildplot.android.parsing.AtomTypes.VariableAtom.java
com.wildplot.android.parsing.AtomTypes.XVariableAtom.java
com.wildplot.android.parsing.AtomTypes.YVariableAtom.java
com.wildplot.android.rendering.AdvancedPlotSheet.java
com.wildplot.android.rendering.BarGraph.java
com.wildplot.android.rendering.DrawableContainer.java
com.wildplot.android.rendering.FunctionDrawer.java
com.wildplot.android.rendering.FunctionDrawer_y.java
com.wildplot.android.rendering.Integral.java
com.wildplot.android.rendering.LegendDrawable.java
com.wildplot.android.rendering.LinesPoints.java
com.wildplot.android.rendering.Lines.java
com.wildplot.android.rendering.MultiScreenPart.java
com.wildplot.android.rendering.PieChart.java
com.wildplot.android.rendering.PlotSheet.java
com.wildplot.android.rendering.PointDrawer2D.java
com.wildplot.android.rendering.RelativeColorGradient.java
com.wildplot.android.rendering.ReliefDrawer.java
com.wildplot.android.rendering.XAxisBarGraph.java
com.wildplot.android.rendering.XAxisHistoGram.java
com.wildplot.android.rendering.XAxis.java
com.wildplot.android.rendering.XGrid.java
com.wildplot.android.rendering.YAxisBarGraph.java
com.wildplot.android.rendering.YAxisHistoGram.java
com.wildplot.android.rendering.YAxis.java
com.wildplot.android.rendering.YGrid.java
com.wildplot.android.rendering.graphics.wrapper.BasicStroke.java
com.wildplot.android.rendering.graphics.wrapper.BufferedImage.java
com.wildplot.android.rendering.graphics.wrapper.Color.java
com.wildplot.android.rendering.graphics.wrapper.FontMetrics.java
com.wildplot.android.rendering.graphics.wrapper.Graphics2D.java
com.wildplot.android.rendering.graphics.wrapper.Graphics.java
com.wildplot.android.rendering.graphics.wrapper.Rectangle.java
com.wildplot.android.rendering.graphics.wrapper.Stroke.java
com.wildplot.android.rendering.interfaces.Drawable.java
com.wildplot.android.rendering.interfaces.Function2D.java
com.wildplot.android.rendering.interfaces.Function3D.java
com.wildplot.android.rendering.interfaces.Legendable.java
com.wildplot.android.rendering.interfaces.StepFunction2D.java