Line chart and Gradient

Source: Internet
Author: User
Tags drawtext
Figure:
View:


Package COM. example. test_chart; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. lineargradient; import android. graphics. paint; import android. graphics. shader; import android. view. view; public class chartviewextends view {public int xpoint = 30; // The X coordinate of the origin public int ypoint = 280; // The Y coordinate of the origin public int XScale = 40; // The Scale length of X is public int yscale = 40 ;/ /Y Scale length public int xlength = 240; // X axis length public int ylength = 240; // y axis length Public String [] xlabel; // X scale Public String [] ylabel; // y scale Public String [] data; // data Public String title; // display title Public chartview (context) {super (context);} public void setinfo (string [] xlabels, string [] ylabels, string [] alldata, string strtitle) {xlabel = xlabels; ylabel = ylabels; data = alldata; Title = strtitle;} @ ove Rride protected void ondraw (canvas) {super. ondraw (canvas); // override the ondraw method canvas. drawcolor (color. dkgray); // set the background color paint = NULL; paint paint2 = new paint (); paint2.setstyle (paint. style. stroke); paint2.setantialias (true); // de-sawtooth paint2.setcolor (color. blue); // color paint paint1 = new paint (); paint1.setstyle (paint. style. stroke); paint1.setantialias (true); // de-sawtooth paint1.setcolor (color. green); paint = Paint1; paint. settextsize (12); // set the axis text size/* set the gradient color to change */shader mshader = new lineargradient (0, 0,100,100, new int [] {color. red, color. green, color. blue, color. yellow, color. ltgray}, null, shader. tilemode. repeat); // a material to create a linear gradient along a line. Paint. setshader (mshader); // sets the Y axis canvas. drawline (xpoint, ypoint-ylength, xpoint, ypoint, paint); // axis for (INT I = 0; I * yscale <ylength; I ++) {canvas. drawline (xpoint, ypoint-I * yscale, xpoint + 5, ypoint-I * yscale, paint); // scale try {canvas. drawtext (ylabel [I], XPoint-22, ypoint-I * yscale + 5, paint); // text} catch (exception e) {}} canvas. drawline (xpoint, ypoint-ylength, XPoint-3, ypoint-ylength + 6, paint); // arrow ca Nvas. drawline (xpoint, ypoint-ylength, xpoint + 3, ypoint-ylength + 6, paint); // you can specify the X axis canvas. drawline (xpoint, ypoint, xpoint + xlength, ypoint, paint); // axis for (INT I = 0; I * XScale <xlength; I ++) {canvas. drawline (xpoint + I * XScale, ypoint, xpoint + I * XScale, YPoint-5, paint); // scale try {canvas. drawtext (xlabel [I], xpoint + I * XScale-10, ypoint + 20, paint ); // text // data value if (I> 0 & ycoord (data [I-1])! =-999 & ycoord (data [I])! =-999) // ensures valid data canvas. drawline (xpoint + (I-1) * XScale, ycoord (data [I-1]), xpoint + I * XScale, ycoord (data [I]), paint); canvas. drawcircle (xpoint + I * XScale, ycoord (data [I]), 2, paint);} catch (exception e) {}} canvas. drawline (xpoint + xlength, ypoint, xpoint + XLength-6, YPoint-3, paint); // arrow canvas. drawline (xpoint + xlength, ypoint, xpoint + XLength-6, ypoint + 3, paint); paint. settextsize (16); canvas. drawtext (title, 150, 50, paint);} private int ycoord (string y0) // calculate the Y coordinate when drawing. If no data exists,-999 {int y is returned; try {Y = integer. parseint (y0);} catch (exception e) {return-999; // if an error occurs,-999 is returned.} Try {return ypoint-y * yscale/integer. parseint (ylabel [1]);} catch (exception e) {} return y ;}}

Java:

Package COM. example. test_chart; import android. OS. bundle; import android. app. activity; import android. view. menu; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); chartview myview = new chartview (this); myview. setinfo (New String [] {"7-11", "7-12", "7-13", "7-14", "7-15 ", "7-16", "7-17"}, // X axis scale new string [] {"", "50", "100", "150 ", "200", "250"}, // y axis scale new string [] {105 + "", 203 + "", 100 + "", 46 + "", 95 + "", 80 + "", 102 + ""}, // data "icon title"); setcontentview (myview) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

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.