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: