How does Android draw a bar chart? android bar chart

Source: Internet
Author: User
Tags drawtext

How does Android draw a bar chart? android bar chart



The following figure shows how the bar chart changes based on the SeekBar Sliding Mode:

Effect:




Code for custom View:

Package com. example. coustomviewdemo; import android. r. color; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. graphics. paint. align; import android. graphics. paint. fontMetrics; import android. util. attributeSet; import android. view. view; public class StatscsView extends View {public StatscsVi Ew (Context context) {super (context); // TODO Auto-generated constructor stubinit (context, null);} public StatscsView (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stubinit (context, attrs);} // axis brush: private Paint axisLinePaint; // horizontal dotted line inside the Axis brush private Paint hLinePaint; // Paint brush private Paint titlePaint for text painting; // style information of the rectangular Paint bar chart private Paint recPaint; private void Init (Context context, AttributeSet attrs) {axisLinePaint = new Paint (); hLinePaint = new Paint (); titlePaint = new Paint (); recPaint = new Paint (); axisLinePaint. setColor (Color. DKGRAY); hLinePaint. setColor (Color. LTGRAY); titlePaint. setColor (Color. BLACK);} // 7 private int [] thisYear; // 7 private int [] lastYear;/*** re-paint the new data with the View subclass. ** Called when the main thread refreshes the control: * this. invalidate (); invalid meaning. * This. postInvalidate (); you can call a subthread to update the view method. * ** // Updata this year datapublic void updateThisData (int [] thisData) {thisYear = thisData; // this. invalidate (); // invalid meaning. This. postInvalidate (); // you can call a method to update the view in a subthread .} // Updata last year datapublic void updateLastData (int [] lastData) {lastYear = lastData; // this. invalidate (); // invalid meaning. This. postInvalidate (); // you can call a method to update the view in a subthread .} Private String [] yTitlesStrings = new String [] {"80000", "60000", "40000", "20000", "0 "}; private String [] xTitles = new String [] {"1", "2", "3", "4", "5", "6", "7 "}; @ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); int width = getWidth (); int height = getHeight (); // 1 draw the coordinate line: canvas. drawLine (100, 10,100,320, axisLinePaint); canvas. drawLine (100,320, width-10, 320, axisLinePaint); // 2 draw the horizontal line inside the coordinate int leftHeight = 300; // the height to be divided on the left peripheral: int hPerHeight = leftHeight/4; hLinePaint. setTextAlign (Align. CENTER); for (int I = 0; I <4; I ++) {canvas. drawLine (100, 20 + I * hPerHeight, width-10, 20 + I * hPerHeight, hLinePaint);} // 3 draw Y week coordinates FontMetrics metrics = titlePaint. getFontMetrics (); int descent = (int) metrics. descent; titlePaint. setTextAlign (Align. RIGHT); for (int I = 0; I <yTitlesStrin Gs. length; I ++) {canvas. drawText (yTitlesStrings [I], 80, 20 + I * hPerHeight + descent, titlePaint);} // 4 draw X-week coordinates int xAxisLength = width-110; int columCount = xTitles. length + 1; int step = xAxisLength/columCount; for (int I = 0; I <columCount-1; I ++) {canvas. drawText (xTitles [I], 100 + step * (I + 1), 360, titlePaint);} // 5 draw a rectangle if (thisYear! = Null & thisYear. length> 0) {int thisCount = thisYear. length; for (int I = 0; I <thisCount; I ++) {int value = thisYear [I]; int num = 8-value/10000; recPaint. setColor (0xFF1078CF); Rect rect = new Rect (); rect. left = 100 + step * (I + 1)-10; rect. right = 100 + step * (I + 1) + 10; // current relative height: int rh = (leftHeight * num)/8; rect. top = rh + 20; rect. bottom = 320; canvas. drawRect (rect, recPaint) ;}} if (lastYear! = Null & lastYear. length> 0) {int thisCount = lastYear. length; for (int I = 0; I <thisCount; I ++) {int value = lastYear [I]; int num = 8-value/10000; recPaint. setColor (0xFFAA1122); Rect rect = new Rect (); rect. left = 100 + step * (I + 1)-10; rect. right = 100 + step * (I + 1) + 10; // current relative height: int rh = (leftHeight * num)/8; rect. top = rh + 20; rect. bottom = 320; canvas. drawRect (rect, recPaint );}}}}

Activity:

package com.example.coustomviewdemo;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class StatscsActivity extends Activity implementsOnSeekBarChangeListener {private SeekBar seekBar;private StatscsView statscsView;public StatscsActivity() {// TODO Auto-generated constructor stub}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.sycts);seekBar = (SeekBar) this.findViewById(R.id.seekBar);statscsView = (StatscsView) this.findViewById(R.id.statscsView1);// seekerBarseekBar.setOnSeekBarChangeListener(this);}private int[] lastData0 = new int[] { 70000, 10000, 20000, 40000, 50000,80000, 40000 };private int[] thisData0 = new int[] { 40000, 10000, 10000, 20000, 30000,50000, 30000 };private int[] lastData1 = new int[] { 70000, 60000, 60000, 40000, 50000,80000, 80000 };private int[] thisData1 = new int[] { 40000, 30000, 30000, 20000, 30000,50000, 30000 };private int[] lastData2 = new int[] { 70000, 50000, 70000, 80000, 80000,80000, 70000 };private int[] thisData2 = new int[] { 40000, 10000, 40000, 40000, 30000,40000, 10000 };private int[] lastData3 = new int[] { 70000, 80000, 70000, 40000, 50000,80000, 40000 };private int[] thisData3 = new int[] { 10000, 10000, 10000, 20000, 30000,10000, 30000 };@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubint cc = progress / 4;switch (cc) {case 0:statscsView.updateThisData(lastData0);statscsView.updateLastData(thisData0);break;case 1:statscsView.updateThisData(lastData1);statscsView.updateLastData(thisData1);break;case 2:statscsView.updateThisData(lastData2);statscsView.updateLastData(thisData2);break;case 3:statscsView.updateThisData(lastData3);statscsView.updateLastData(thisData3);break;default:break;}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}}


Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <SeekBar         android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:max="48"                />        <LinearLayout         android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:gravity="center"        >                <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="1"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="2"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="3"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="4"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="5"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="6"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="7"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="8"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="9"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="10"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="11"            />        <TextView             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1" android:gravity="center"            android:text="12"            />                    </LinearLayout>        <com.example.coustomviewdemo.StatscsView        android:id="@+id/statscsView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>





How can I create a bar chart for android to display listening events for each column?

Whether your image is self-painted or an image. If your View is self-painted, listen to it.
Listening for touchEvent, rewriting onKeyDown and other functions

How does Android create a bar chart?

Android bar chart
Package org. achartengine. chartdemo. demo. chart;
Import org. achartengine. chartFactory; import org. achartengine. chart. barChart. type; import org. achartengine. chartdemo. demo. r; import org. achartengine. model. rangeCategorySeries; import org. achartengine. model. XYMultipleSeriesDataset; import org. achartengine. renderer. simpleSeriesRenderer; import org. achartengine. renderer. XYMultipleSeriesRenderer; import android. app. activity; import android. graphics. color; import android. graphics. paint. align; import android. OS. bundle; import android. view. view;

Public class TemperatureChart extends Activity {
@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. main );
Double [] minValues = new double [] {55,50, 40,30, 20,20, 30,40, 50,55}; double [] maxValues = new double [] {95,100, 95,85 };
// Used to save the point set data, including the X and Y coordinates of each curve XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset (); // use RangeCategorySeries = new RangeCategorySeries ("your consumption record for the current month") with the bar chart; // the text at the bottom of the chart in parentheses is for (int k = 0; k <minValues. length; k ++) {series. add (minValues [k], maxValues [k]);} dataset. addSeries (series. toXYSeries (); int [] colors = new int [] {Color. CYAN}; // blue-green XYMultipleSeriesRenderer renderer = buildBarRenderer (colors); setChartSettings (renderer, "title", "x", "y", 0.5, 12.5, 0,150, Color. GRAY, Color. LTGRAY); // color pre-paint ...... remaining full text>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.