Android Open Source - AnkiStats Pie Chart






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

package com.wildplot.android.rendering;
// w  w  w  . j a v  a  2 s  .  com
import com.wildplot.android.rendering.graphics.wrapper.Color;
import com.wildplot.android.rendering.graphics.wrapper.FontMetrics;
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.Legendable;


public class PieChart implements Drawable, Legendable {

    private String mName = "";
    private boolean mNameIsSet = false;
  private double[] mValues;
  private double[] mPercent;
  private double mSum;
  private int mColorHelper;
  private PlotSheet mPlotSheet;

  private Color[] mColors = {
    new Color(255,  0,  0,180),
    new Color(0  ,255,  0,180),
    new Color(0  ,0,  255,180),
    
    new Color(255,255,  0,180),
    new Color(  0,255,255,180),
    new Color(255,  0,255,180)
    };

  public PieChart(PlotSheet plotSheet, double[] vals){
    this.mPlotSheet = plotSheet;
    mValues = vals;
    //mValues = new double[] {5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5};
    mPercent = new double[mValues.length];
    for(double v: mValues) mSum +=v;
    mPercent[0]= mValues[0]/ mSum;
    for(int i=1; i< mValues.length; i++){
      mPercent[i]= mPercent[i-1]+ mValues[i]/ mSum;
    }
    mColorHelper = mColors.length;
    if((mValues.length-1)%(mColors.length)==0) mColorHelper = mColors.length-1;
  }

  /*
   * (non-Javadoc)
   * @see rendering.Drawable#isOnFrame()
   */
  @Override
  public boolean isOnFrame() {
    return false;
  }
  
  /*
   * (non-Javadoc)
   * @see rendering.Drawable#paint(java.awt.Graphics)
   */
  public void paint(Graphics g){
    Rectangle field = g.getClipBounds();
        float realBorder= mPlotSheet.getFrameThickness() + 3;
        float diameter=Math.min(field.width, field.height)-2*realBorder;

        float xCenter = (float)(field.width/2.0);
        float yCenter = (float)(field.height/2.0);
    Color oldColor = g.getColor();

        float xMiddle = xCenter - (float)(diameter/2.0);
        float yMiddle = yCenter - (float)(diameter/2.0);

        float currentAngle = 0;
        float nextAngle = (float)(360.0* mPercent[0]);
        int tmp = 0;
    for(int i = 0; i< mPercent.length-1; i++) {
      g.setColor(mColors[i% mColorHelper]);
      g.fillArc(xMiddle, yMiddle, (int)diameter, (int)(diameter), currentAngle, nextAngle - currentAngle);
      currentAngle = nextAngle;
      nextAngle = (int)(360.0* mPercent[i+1]);
      tmp = i;
    }
        tmp++;
    //last one does need some corrections to fill a full circle:
    g.setColor(mColors[tmp% mColorHelper]);
    g.fillArc(xMiddle, yMiddle, diameter, diameter, currentAngle, 360 - currentAngle);
    g.setColor(Color.black);
    g.drawArc(xMiddle, yMiddle, diameter, diameter, 0, 360);
    
    //Beschriftung
    g.setColor(Color.white);
    //g.drawString("" + Math.round(((mPercent[0]) * 100) * 100) / 100.0 + "%", (float) (xCenter + Math.cos(mPercent[0] * Math.PI) * 0.375 * diameter) - 20, (float) (yCenter - Math.sin(mPercent[0] * Math.PI) * 0.375 * diameter));
    for(int j=0;j< mPercent.length;j++)
    {
            double oldPercent = 0;
            if(j  != 0)
                oldPercent = mPercent[j-1];
            String text = ""+Math.round((((mPercent[j]- oldPercent))*100)*100)/100.0+"%";
            float x = (float)(xCenter+Math.cos((oldPercent+(mPercent[j]- oldPercent)*0.5)*360*Math.PI/180.0)*0.375*diameter)-20;
            float y = (float)(yCenter-Math.sin((oldPercent+(mPercent[j]- oldPercent)*0.5)*360*Math.PI/180.0)*0.375*diameter);
            FontMetrics fm = g.getFontMetrics();
            float width = fm.stringWidth(text);
            float height = fm.getHeight();
            Color color = g.getColor();
            g.setColor(new Color(0,0,0,0.5f));
            g.fillRect(x-1,y-height+3,width+2,height);
            g.setColor(color);
      g.drawString(text,x ,y );
    }
    
    g.setColor(oldColor);
  }

  @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 mColors[0];
    }

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

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

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

    public void setColors(Color[] colors){
        mColors = colors;
        mColorHelper = mColors.length;
        if((mValues.length-1)%(mColors.length)==0)
            mColorHelper = mColors.length-1;
    }
}




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