http://developer.aiwgame.com/canvas-draw-bar-charts-and-pie-charts-in-android.html
?
Canvas draw bar charts and pie charts in android!
Below is the result:
Main Activity PieColumar.java:
package com.dean;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.RectF;import android.os.Bundle;import android.view.Menu;import android.view.View;import com.entity.CountEntity;public class PieColumar extends Activity { CountEntity entity = new CountEntity(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DrawView myView = new DrawView(PieColumar.this); setContentView(myView); } public class DrawView extends View { private int value, location; // For statistical private double flagIn, flagOut, flagLeft; private float clotheRate, studyRate, trafficRate, otherRate; public DrawView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Set the background color to light gray canvas.drawColor(Color.LTGRAY); // Define a Paint Paint paint = new Paint(); // Anti-aliasing paint.setAntiAlias(true); // Draw the abscissa paint.setColor(Color.BLACK); canvas.drawLine(40, 200, 300, 200, paint); // Draw the vertical axis paint.setColor(Color.BLACK); canvas.drawLine(40, 30, 40, 200, paint); // raw a horizontal line paint.setColor(Color.BLACK); for (int i = 170; i > 20; i -= 30) { canvas.drawLine(40, i, 280, i, paint); } // Set the title font size paint.setTextSize(18); paint.setColor(Color.RED); canvas.drawText( getResources().getString(R.string.graph_page_count), 65, 26, paint); canvas.drawText( getResources().getString(R.string.graph_page_consume), 100, 250, paint); // Set the font of each item is black paint.setColor(Color.BLACK); paint.setTextSize(15); // Income, expenditure, balance canvas.drawText( getResources().getString(R.string.second_page_countIn), 60, 220, paint); canvas.drawText( getResources().getString(R.string.second_page_countOut), 130, 220, paint); canvas.drawText( getResources().getString(R.string.second_page_countLeft), 200, 220, paint); canvas.drawText(getResources().getString(R.string.graph_page_top), 5, 40, paint); canvas.drawText( getResources().getString(R.string.graph_page_right), 265, 218, paint); // The vertical axis corresponds to the value of for (value = 300, location = 180; value 1500) { entity.setIncome(1500); canvas.drawText(flagIn + "", 100, 45, paint); } else if (entity.getIncome() > 0 && entity.getIncome() 1500) { entity.setOutcome(1500); canvas.drawText(flagOut + "", 160, 45, paint); } else if (entity.getOutcome() > 0 && entity.getOutcome()1500) { entity.setLeft(1500); canvas.drawText(flagLeft + "", 220, 45, paint); } else if (entity.getLeft() >= 0 && entity.getLeft()
CountEntity.java
package com.entity;public class CountEntity { double outClothe = 400; double outStudy = 200; double outTraffic = 150; double outOther = 450; double income = 1200; double outcome = 1050; double left = 150; public double getOutClothe() { return outClothe; } public void setOutClothe(double outClothe) { this.outClothe = outClothe; } public double getOutStudy() { return outStudy; } public void setOutStudy(double outStudy) { this.outStudy = outStudy; } public double getOutTraffic() { return outTraffic; } public void setOutTraffic(double outTraffic) { this.outTraffic = outTraffic; } public double getOutOther() { return outOther; } public void setOutOther(double outOther) { this.outOther = outOther; } public double getIncome() { return income; } public void setIncome(double income) { this.income = income; } public double getOutcome() { return outcome; } public void setOutcome(double outcome) { this.outcome = outcome; } public double getLeft() { return left; } public void setLeft(double left) { this.left = left; }}
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Android Developer - AiwGame.com</string> <string name="graph_page_count">Income, expenditure, balance of histogram</string> <string name="graph_page_consume">Income</string> <string name="second_page_countIn">Expend</string> <string name="second_page_countOut">Balance</string> <string name="second_page_countLeft">Subtotal</string> <string name="graph_page_top">$</string> <string name="graph_page_right">...</string> <string name="second_page_moneyoutOne">Clothes</string> <string name="second_page_moneyoutTwo">Study</string> <string name="second_page_moneyoutThree">Traffic</string> <string name="second_page_moneyoutFour">Other</string>
炒股风暴