New Android journey (9) custom line chart

Source: Internet
Author: User
Tags drawtext

If you want to implement a line chart, but do not find the appropriate control orCodeThere is only one basic look. On the basis of it, some improvements have been made to make it more flexible. You can transfer parameters, set positions, coordinate axis length, and Scale length.

Custom chartview. Java

Package jetz. Common;

Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. View. view;

Public class chartview extends view {
Public int xpoint = 40; // X coordinate of the Origin
Public int ypoint = 260; // y coordinate of the Origin
Public int XScale = 55; // The Scale length of X
Public int yscale = 40; // The Scale length of Y
Public int xlength = 380; // The length of the X axis
Public int ylength = 240; // y axis length
Public String [] xlabel; // scale of X
Public String [] ylabel; // y Scale
Public String [] data; // data
Public String title; // The title displayed.
Public chartview (context)
{
Super (context );
}
Public void setinfo (string [] xlabels, string [] ylabels, string [] alldata, string strtitle)
{
Xlabel = xlabels;
Ylabel = ylabels;
Data = alldata;
Title = strtitle;
}
@ Override
Protected void ondraw (canvas ){
Super. ondraw (canvas); // rewrite the ondraw Method

// Canvas. drawcolor (color. White); // you can specify the background color.
Paint paint = new paint ();
Paint. setstyle (paint. style. Stroke );
Paint. setantialias (true); // de-sawtooth
Paint. setcolor (color. Blue); // color
Paint paint1 = new paint ();
Paint1.setstyle (paint. style. Stroke );
Paint1.setantialias (true); // de-sawtooth
Paint1.setcolor (color. dkgray );
Paint. settextsize (12); // you can specify the size of the Axis text.
// Set 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
Canvas. drawline (xpoint, ypoint-ylength, xpoint + 3, ypoint-ylength + 6, paint );
// Set 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) // calculates the Y coordinate during painting. If no data exists,-999 is returned.
{
Int y;
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;
}
}

Bytes --------------------------------------------------------------------------------------------

Call method:

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 int [] {15, 23, 10, 36, 45, 40, 12}, // data
"Icon title"
);

Effect:

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.