Android Trend Chart

Source: Internet
Author: User
Tags getcolor xunit

Ideas:

Custom view inherits view, draws points and lines in the OnDraw () function

Gets the height of the view, distinguishing the view from the specified trend value,

For example, our sales performance from 0-10, divided into 10 stages, then the height of the view/10, and then according to the specified state array to draw the location of the point, and then the line connection point

Trend View Code :

public class Lineview extends View {

The distance between points
private int xUnit = 50;

The height of the view
private int height;

Default Trend Unit
private int yunit = 10;

Default Trend Array
private int arr[] = {5, 6, 3, 6, 3, 7, 6, 7, 7, 8, 8, 2, 6, 4, 3};


private int LineColor, radius, Referlinecolor, maxlevel;


Private float linewidth, referlinewidth;

Public Lineview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
Init (context, attrs);
}

Public Lineview (context context, AttributeSet Attrs) {
Super (context, attrs);
Init (context, attrs);
}

private paint paint;
Private Paint Paintreferline;

private void init (context context, AttributeSet Attrs) {
LineColor = Color.green;
LineWidth = 3;
Referlinecolor = Color.gray;
Referlinewidth = 1;
Maxlevel = 10;

TypedArray Typearray = context.obtainstyledattributes (Attrs,
R.styleable.lineview);
for (int i = 0; i < typearray.length (); i++) {
Switch (Typearray.getindex (i)) {

Color of trend Lines
Case R.styleable.lineview_linecolor:
LineColor = Typearray.getcolor (i, color.green);
Break

Width of trend Line
Case R.styleable.lineview_linewidth:
LineWidth = Typearray.getdimension (i, 3);
Break

Radius of the Dot
Case R.styleable.lineview_pointradius:
Radius = Typearray.getdimensionpixelsize (i, 5);
Break

Reference Line Color
Case R.styleable.lineview_referlinecolor:
Referlinecolor = Typearray.getcolor (i, Color.gray);
Break

Reference line width
Case R.styleable.lineview_referlinewidth:
Referlinewidth = Typearray.getdimension (i, 1);
Break

Stage value
Case R.styleable.lineview_maxlevel:
Maxlevel = Typearray.getint (i, 10);
Break
}
}

Paint = new paint ();
Paint.setcolor (LineColor);
Paint.setstrokewidth (linewidth);
Paint.setantialias (TRUE);

Paintreferline = new Paint ();
Paintreferline.setcolor (Referlinecolor);
Paintreferline.setstrokewidth (Referlinewidth);
Paintreferline.setantialias (TRUE);
}

public void Setlevelarray (int[] array) {
This.arr = array;
Invalidate ();
}

public void Setmaxlevel (int maxlevel) {
This.maxlevel = Maxlevel;
}

public void Setlinecolor (int linecolor) {
This.linecolor = LineColor;
Paint.setcolor (LineColor);
}

public void Setlinewidth (float linewidth) {
This.linewidth = linewidth;
Paint.setstrokewidth (linewidth);
}

public void Setradius (int radius) {
This.radius = radius;
}

public void Setreferlinecolor (int referlinecolor) {
This.referlinecolor = Referlinecolor;
Paintreferline.setcolor (Referlinecolor);
}

public void setreferlinewidth (int referlinewidth) {
This.referlinewidth = Referlinewidth;
Paintreferline.setstrokewidth (Referlinewidth);
}

@Override
protected void OnLayout (Boolean changed, int left, int top, int. right,
int bottom) {
Super.onlayout (changed, left, top, right, bottom);
Height = getheight ();
Yunit = Height/maxlevel;
if (Arr.length > 0) {
XUnit = GetWidth ()/arr.length;
}
}

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
if (Arr.length = = 0) {
Canvas.drawtext ("Set point Array First", [+], paint);
Return
}
for (int i = 0; i < arr.length-1; i++) {
int SX = i * xUnit + radius;
int SY = arr[i] * YUNIT;
int EX = (i + 1) * xUnit + radius;
int code = Arr[i + 1];
int EY = code * YUNIT;

if (SY = = 0) {
SY = radius;
}
if (EY = = 0) {
EY = radius;
}
if (SY >= Height-radius) {
SY = Height-radius;
}
if (EY >= Height-radius) {
EY = Height-radius;
}

if (i = = 0) {
Canvas.drawline (SX, 0, SX, height, paintreferline);
Canvas.drawcircle (SX, SY, radius, paint);
}
Canvas.drawline (ex, 0, ex, height, paintreferline);
Canvas.drawline (SX, SY, EX, EY, paint);
Canvas.drawcircle (EX, EY, radius, paint);
}
}
}

XML layout:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:lineview= "Http://schemas.android.com/apk/res/com.example.lineview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<demo. Lineview
Android:id= "@+id/lineview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Lineview:linecolor= "#000000"
Lineview:linewidth= "2DP"
Lineview:maxlevel= "10"
Lineview:pointradius= "5DP"
Lineview:referlinecolor= "#DCDCDC"
Lineview:referlinewidth= "1DP"/>

</LinearLayout>

Source code Download

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.