Android custom line chart and android custom line chart

Source: Internet
Author: User
Tags dateformat drawtext

Android custom line chart and android custom line chart

BrokenLine control:

Import java. text. simpleDateFormat; import java. util. arrayList; import java. util. calendar; import java. util. collections; import java. util. date; import java. util. gregorianCalendar; import java. util. list; import android. content. context; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import Ndroid. graphics. dashPathEffect; import android. graphics. linearGradient; import android. graphics. paint; import android. graphics. path; import android. graphics. pathEffect; import android. graphics. shader; import android. util. log; import android. view. view; import android. view. viewGroup. layoutParams; import com. qianhua. healthy_brain.R;/*** line chart ** @ author Administrator **/public class BrokenLine extends View {Private List <Integer> milliliter; private float tb; private float interval; private float interval_left; private float margin_bottom; private Paint paint_date, paint_brokenLine, interval, interval, framPanint; private int time_index; private Bitmap bitmap_point; private Path path; private float dotted_text; public float getDotted_text () {return dotted_text;} public void setDotte D_text (float dotted_text) {this. dotted_text = dotted_text;} private int fineLineColor = 0x5faaaaaa; // gray private int blueLineColor = 0xff00ffff; // blue private int orangeLineColor = 0xffd56f2b; // orange public BrokenLine (Context context Context, list <Integer> milliliter) {super (context); init (milliliter);} public void init (List <Integer> milliliter) {if (null = milliliter | milliliter. size () = 0) return; this. mill Iliter = delZero (milliliter); Resources res = getResources (); tb = res. getDimension (R. dimen. historyscore_tb); interval_left_right = tb * 5.0f; interval_left = tb * 0.5f; margin_bottom = 8 * tb * 0.2f; paint_date = new Paint (); tb * 0.1f ); paint_date.setTextSize (tb * 1.2f); paint_date.setColor (fineLineColor); paint_brokenLine = new Paint (); paint_brokenLine.setStrokeWidth (tb * 0.1f); pain T_brokenLine.setColor (blueLineColor); paint_brokenLine.setAntiAlias (true); paint_dottedline = new Paint (); paint_dottedline.setStyle (Paint. style. STROKE); paint_dottedline.setColor (fineLineColor); paint_brokenline_big = new Paint (); rotate (tb * 0.2f); rotate (fineLineColor); trim (true); framPanint = new Paint (); framPanint. setAnt IAlias (true); framPanint. setStrokeWidth (2f); path = new Path (); bitmap_point = BitmapFactory. decodeResource (getResources (), R. drawable. icon_point_blue); setLayoutParams (new LayoutParams (int) (this. milliliter. size () * interval_left_right), LayoutParams. MATCH_PARENT);}/*** remove Left and Right zero data ** @ return */public List <Integer> delZero (List <Integer> milliliter) {List <Integer> list = new ArrayList <Integer> (); int s Ta = 0; int end = 0; for (int I = 0; I <milliliter. size (); I ++) {if (milliliter. get (I )! = 0) {sta = I; break ;}for (int I = milliliter. size ()-1; I> = 0; I --) {if (milliliter. get (I )! = 0) {end = I; break ;}for (int I = 0; I <milliliter. size (); I ++) {if (I> = sta & I <= end) {list. add (milliliter. get (I) ;}} time_index = sta; // dotted_text = (Collections. max (milliliter)-Collections. min (milliliter)/12.0f * 5.0f); return list;} protected void onDraw (Canvas c) {if (null = milliliter | milliliter. size () = 0) return; drawStraightLine (c); drawBrokenLine (c); drawDate (c);}/*** draw a vertical line * * @ Param c */public void drawStraightLine (Canvas c) {int count_line = 0; for (int I = 0; I <milliliter. size (); I ++) {if (I = 0) {// draw the Y axis c. drawLine (interval_left_right * I, 0, interval_left_right * I, getHeight ()-margin_bottom, paint_date); for (int j = 0; j <10; j ++) {c. drawText (String. valueOf (10 * (j + 1), 0, (getHeight ()-margin_bottom)/10 * (10-(j + 1), paint_date ); if (j = 5) {// draw the dotted line paint_dottedline.setC Olor (orangeLineColor); Path path = new Path (); path. moveTo (0, (getHeight ()-margin_bottom)/10 * (10-(j + 1); path. lineTo (getWidth (), (getHeight ()-margin_bottom)/10 * (10-(j + 1 ))); pathEffect effects = new DashPathEffect (new float [] {tb * 0.3f, tb * 0.3f, tb * 0.3f, tb * 0.3f}, tb * 0.1f); effect (effects); c. drawPath (path, paint_dottedline) ;}} continue ;}// draw four consecutive bars as a set of paint_dottedline.s EtColor (fineLineColor); if (count_line = 0) {c. drawLine (interval_left_right * I, 0, interval_left_right * I, getHeight ()-margin_bottom, paint_date);} if (count_line = 2) {c. drawLine (interval_left_right * I, tb * 1.5f, interval_left_right * I, getHeight ()-margin_bottom, paint_date);} if (count_line = 1 | count_line = 3) {Path path = new Path (); path. moveTo (interval_left_right * I, tb * 1.5f); p Ath. lineTo (interval_left_right * I, getHeight ()-margin_bottom); PathEffect effects = new DashPathEffect (new float [] {tb * 0.3f, tb * 0.3f, tb * 0.3f, tb * 0.3f}, tb * 0.1f); paint_dottedline.setPathEffect (effects); c. drawPath (path, paint_dottedline);} count_line ++; if (count_line> = 4) {count_line = 0 ;}// draw the X axis c. drawLine (0, getHeight ()-margin_bottom, getWidth (), getHeight ()-margin_bottom, paint _ Brokenline_big);}/*** draw a line ** @ param c */public void drawBrokenLine (Canvas c) {int index = 0; float temp_x = 0; float temp_y = 0; // float base = (getHeight ()-tb * 3.0f)/(Collections. max (milliliter)-Collections. min (milliliter); float base = (getHeight ()-margin_bottom)/100; Shader mShader = new LinearGradient (0, 0, 0, getHeight (), new int [] {Color. argb (100, 0,255,255), Color. argb (45, 0, 25 5,255), Color. argb (10, 0,255,255)}, null, Shader. tileMode. CLAMP); framPanint. setShader (mShader); for (int I = 0; I <milliliter. size ()-1; I ++) {Log. e ("I", "" + milliliter. get (I); float x1 = interval_left_right * I; float y1 = getHeight ()-margin_bottom-(base * milliliter. get (I); float Y1 = milliliter. get (I); float x2 = interval_left_right * (I + 1); float y2 = getHeight ()-margin_bottom-(base * Milliliter. get (I + 1); float Y2 = milliliter. get (I + 1); if (int) (base * milliliter. get (I + 1) = 0 & index = 0) {index ++; temp_x = x1; temp_y = y1 ;}if (int) (base * milliliter. get (I + 1 ))! = 0 & index! = 0) {index = 0; x1 = temp_x; y1 = temp_y;} paint_date.setColor (orangeLineColor); if (I = 0) c. drawText (String. valueOf (Y1), x1, y1, paint_date); // plot the size of the first value if (index = 0) {c. drawText (String. valueOf (Y2), x2, y2, paint_date); // plot the size of the I + 1 value c. drawLine (x1, y1, x2, y2, paint_brokenLine); path. lineTo (x1, y1); if (I! = 0) c. drawBitmap (bitmap_point, x1-bitmap_point.getWidth ()/2, y1-bitmap_point.getHeight ()/2, null); if (I = milliliter. size ()-2) {path. lineTo (x2, y2); path. lineTo (x2, getHeight (); path. lineTo (0, getHeight (); path. close (); c. drawPath (path, framPanint); c. drawBitmap (bitmap_point, x2-bitmap_point.getWidth ()/2, y2-bitmap_point.getHeight ()/2, null) ;}} paint_date.setColor (fineLineColor );} /*** draw time ** @ param c */public void drawDate (Canvas c) {Date date Date = new Date (); // set the time to Calendar calendar = new GregorianCalendar (); calendar. setTime (date); SimpleDateFormat dateFormat = new SimpleDateFormat ("MM-dd"); String [] dates = new String [milliliter. size ()]; for (int I = 0; I <milliliter. size (); I ++) {calendar. add (calendar. DATE, 1); // Add a day after the DATE. the integer is pushed back, and the negative number moves forward date = calendar ar. getTime (); // this time is the result of one day after the date. dates [I] = dateFormat. format (date ). toString () ;}for (int I = 0; I <milliliter. size (); I + = 1) {paint_date.setStrokeWidth (tb * 2.8f); c. drawText (dates [I], interval_left_right * I, getHeight (), paint_date );}}}

Activity:

Import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. util. log; import android. widget. relativeLayout; import com. qianhua. healthy_brain.R; import com. qianhua. healthy_brain.widget.BrokenLine; public class TrainResultActivity extends Activity {RelativeLayout linear; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. train_result); List <Integer> lists = new ArrayList <Integer> (); // linear graph range: 0-100for (int I = 0; I <48; I ++) {if (I <8 | I = 28 | I = 12 | I = 18 | I = 20 | I = 30 | I = = 34) {lists. add (0) ;}else {// int item = getRandom (0,100); // Log. w ("" + I, "" + item); lists. add (getRandom (0,100) ;}} linear = (RelativeLayout) findViewById (R. id. linear); linear. addView (new BrokenLine (this, lists);} public int getRandom (int min, int max) {return (int) Math. round (Math. random () * (max-min) + min );}}


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" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#f4f4f7"        android:orientation="vertical" >        <HorizontalScrollView            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_margin="10dp"            android:scrollbars="none" >            <RelativeLayout                android:id="@+id/linear"                android:layout_marginTop="30dp"                android:layout_width="fill_parent"                android:layout_height="fill_parent" >            </RelativeLayout>        </HorizontalScrollView>    </LinearLayout></LinearLayout>

Reference: http://www.apkbus.com/forum.php? Mod = viewthread & tid = 158365 & extra = page % 3D1


How to set the background of the android line chart, such as adding a background image

Xml background backgrond = "*****"

Line chart drawn by android: urgent code

Here is a link, which contains line charts, bar charts, and pie charts.
Www.eoeandroid.com/...18.01

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.