Android build xy graph

Android build xy graph

Chart and Graph Library for Android

Project maintainer wanted! For time reasons I can not continue to maintain GraphView. Contact me if you are interested and serious about this project. g.jjoe64@gmail.com

What is GraphView

GraphView is a library for Android to programmatically create flexible and nice-looking diagrams. It is easy to understand, to integrate and to customize.

Supported graph types:

  • Line Graphs
  • Bar Graphs
  • Point Graphs
  • or implement your own custom types.

  • Line Chart, Bar Chart, Points
  • Combination of different graph types
  • Scrolling vertical and horizontal . You can scroll with a finger touch move gesture.
  • Scaling / Zooming vertical and horizontal . With two-fingers touch scale gesture (Multi-touch), the viewport can be changed.
  • Realtime Graph (Live change of data)
  • Second scale axis
  • Draw multiple series of data . Let the diagram show more that one series in a graph. You can set a color and a description for every series.
  • Show legend . A legend can be displayed inline the chart. You can set the width and the vertical align (top, middle, bottom).
  • Custom labels . The labels for the x- and y-axis are generated automatically. But you can set your own labels, Strings are possible.
  • Handle incomplete data . It’s possible to give the data in different frequency.
  • Viewport . You can limit the viewport so that only a part of the data will be displayed.
  • Manual Y axis limits
  • And much more. Check out the project page and/or the demo app

Download Demo project at Google Play Store

More examples and documentation

To show you how to integrate the library into an existing project see the GraphView-Demos project! See GraphView-Demos for examples. https://github.com/jjoe64/GraphView-Demos
View GraphView wiki page https://github.com/jjoe64/GraphView/wiki

About

Android Graph Library for creating zoomable and scrollable line and bar graphs.

Источник

Android build xy graph

Easy way is use a Vector, data comes in e.g. you populate the vector and reload/redraw
with new vector, may have to play with indexes and stuff but that is just a little debug
session. Sign In· View Thread

Re: Dynamic data grndvl1 30-Aug-11 5:33

Not quite the answer I was looking for as I have already tried it with Vectors and its really slow for the data packets I need to show, at 250 data points per sec. I am trying to only refresh the graph at 10 fps.

Sign In· View Thread
How can i show titles for x-axis, like u show for y axis, value is 0.0, 20,00, and so on ?

kp_iteng 29-Aug-11 1:44
Hello,

I am getting help from this code.

But i want to show label on x-axix, like you show for y-axix.

Actually i want to show number of visitors on y-axis and date on x-axis. So everyone get ideas about number of visitor for that specific date.

Thanks In advanced for help.

Help me solve this issue.

Thank Your,
Kashyap Patel

Sign In· View Thread
Re: How can i show titles for x-axis, like u show for y axis, value is 0.0, 20,00, and so on ?

becker666 29-Aug-11 19:12

Look at the code it loops thru the data for Y, just do the same for X
as u iterate for each x sample point right there you can draw the X-data

Sign In· View Thread
Re: How can i show titles for x-axis, like u show for y axis, value is 0.0, 20,00, and so on ? kp_iteng 8-Sep-11 0:12

I have your code. I am try but not get Values for x-axis.
Will you please Help me to do this?
Will you change on below code?

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.FontMetrics;
import android.os.Bundle;
import android.widget.ImageView;

public class main extends Activity <

// these_labela has elemnes[label,maxX,maxY]
static int draw_only_this_idx = -1;
static int[] drawSizes;

@Override
public void onCreate(Bundle savedInstanceState) <
super.onCreate(savedInstanceState);
setContentView(R.layout.testy);
setTitle(«Quick XY Plot»);

ImageView image = (ImageView) findViewById(R.id.testy_img);

Bitmap emptyBmap = Bitmap.createBitmap(250,
200, Config.ARGB_8888);

int width = emptyBmap.getWidth();
int height = emptyBmap.getHeight();
Bitmap charty = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

public static Bitmap quicky_XY(Bitmap bitmap)
<
// xode to get bitmap onto screen
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff0B0B61;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;

// get the little rounded cornered outside
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

// —- NOw just draw on this bitmap

// Set the labels info manually
String[] cur_elt_array = new String[4];
cur_elt_array[0]=»Voltage»;
cur_elt_array[1]=»volts»;
cur_elt_array[2]=»93″; // max
cur_elt_array[3]=»0″; //min

Vector labels = new Vector();
labels.add(cur_elt_array);

// se the data to be plotted and we should on our way

Vector data_2_plot = new Vector();

data_2_plot.add(«0.2») ;
data_2_plot.add(«1.2») ;
data_2_plot.add(«9.6») ;
data_2_plot.add(«83.2») ;
data_2_plot.add(«44.2») ;
data_2_plot.add(«20.2») ;
data_2_plot.add(«16.2») ;

plot_array_list(canvas , data_2_plot , labels , «the title» , 0 );

canvas.drawBitmap(bitmap, rect, rect, paint);

// these_labels is vector of [label,units,max.min]

public static void draw_the_grid(Canvas this_g, Vector these_labels)
<

double rounded_max = 0.0;
double rounded_min = 0.0;
double rounded_max_temp;
Object curElt;
String[] cur_elt_array;
int left_margin_d, right_margin_d;

if( draw_only_this_idx == -1)
curElt = these_labels.elementAt(0); // default it to 1st one if non set
else
curElt = these_labels.elementAt(draw_only_this_idx); // now just the 1st elt

rounded_max = get_ceiling_or_floor (Double.parseDouble(cur_elt_array[2]) , true);
rounded_min = get_ceiling_or_floor (Double.parseDouble(cur_elt_array[3]) , false);

// ok so now we have the max value of the set just get a cool ceiling and we go on
final Paint paint = new Paint();
paint.setTextSize(15);

left_margin_d = getCurTextLengthInPixels(paint, Double.toString(rounded_max));
//keep the position for later drawing — leave space for the legend
int p_height = 170;
int p_width = 220;
int[] tmp_draw_sizes = <2 + left_margin_d, 25,p_width - 2 left_margin_d ,p_height 25 -5>;
drawSizes = tmp_draw_sizes; //keep it for later processing

//with the mzrgins worked out draw the plotting grid
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE );

// Android does by coords
this_g.drawRect(drawSizes[0], drawSizes[1],drawSizes[0]+ drawSizes[2], drawSizes[1]+ drawSizes[3] , paint);

// finally draw the grid

paint.setStyle(Paint.Style.STROKE);
this_g.drawRect(drawSizes[0], drawSizes[1],drawSizes[0]+ drawSizes[2], drawSizes[1]+ drawSizes[3] , paint);

for(int i=1; i 0) <
// If has a decimal point, may need to clip off after or force 2 decimal places
this_str = this_str + «00»;
this_str = this_str.substring(0,point+3);
> else <
this_str = this_str + «.00»;
>

if (i == 5)
// thisDrawingArea.drawText(this_str, x_guide — 2, drawSizes[1] + drawSizes[3] — (i *drawSizes[3] / 5) );
thisDrawingArea.drawText(this_str, x_guide — 2, drawSizes[1] + drawSizes[3] — (i *drawSizes[3] / 5) , paint );
else
thisDrawingArea.drawText(this_str, x_guide- 2, drawSizes[1] + drawSizes[3] — (i * drawSizes[3] / 5) -3, paint);
>

// smallyFont = Font.getDefault().derive(Font.BOLD, 12);
// thisDrawingArea.setFont(smallyFont);
paint.setTextSize(10);
switch(this_idx )
<
case 0:

thisDrawingArea.drawText(» » + cur_label +» — » +cur_units, x_guide — 2, drawSizes[1] -15 , paint);
break;

// int len = getFont().getAdvance(cur_label +» — » +cur_units)

thisDrawingArea.drawText(» » + cur_label +» — » +cur_units, x_guide — 2 -30, drawSizes[1] -15, paint );
break;

> // — end of print_axis_values_4_grid —

private static Point scale_point(int this_x , double this_y , Point drawPoint ,
int scr_x , int scr_y , int scr_width , int src_height ,
double maxX , double minX , double maxY , double minY )
<
int temp_x, temp_y;
Point temp = new Point();

if (maxY == minY) //skip bad data
return null;

//don’t touch it if is nothing
try
<
temp_x = scr_x + (int)( ((double)this_x — minX) * ((double)scr_width / (maxX — minX)) );
temp_y = scr_y + (int)( (maxY — this_y) * ((double)src_height / (maxY — minY)) );

temp.x = temp_x;
temp.y= temp_y;
drawPoint = temp;

> // — end of scale_point —

public static boolean plot_array_list(Canvas this_g, Vector this_array_list , Vector these_labels , String this_title , int only_this_idx )
<
int idx;
int lRow ;
int nParms;
int i, points_2_plot, shifted_idx ;
int prev_x, prev_y ;
int cur_x=0, cur_y=0 ;
//Dim ShowMarker As Object
Point cur_point = new Point();
cur_point.set(0,0);

double cur_maxX, cur_minX, cur_maxY=20, cur_minY=0, cur_rangeY;
int cur_start_x, cur_points_2_plot;

int POINTS_TO_CHANGE = 30;
double cur_OBD_val;

//Object curElt;
String curElt;
String[] cur_elt_array;
Object curElt2;
String[] cur_elt_array2;

final Paint paint = new Paint();

try // catch in this block for some thing
<

points_2_plot = this_array_list.size();
<
cur_start_x = 0;
cur_points_2_plot = points_2_plot;
cur_maxX = cur_points_2_plot;
cur_minX = 0;
>

//’Create the plot points for this series from the ChartPoints array:

//the lines have to come out good
paint.setStyle(Paint.Style.STROKE);
//
//for( nParms = 0 ; nParms nothing

> // end of for lrow

> // end of for nParmns

//this_g.invalidate();
return( true);
>
catch (Exception e)
<
return( false);

> // — end of plot_array_list —

// need the width of the labels
private static int getCurTextLengthInPixels(Paint this_paint, String this_text) <
FontMetrics tp = this_paint.getFontMetrics();
Rect rect = new Rect();
this_paint.getTextBounds(this_text, 0, this_text.length(), rect);
return rect.width();
> // — end of getCurTextLengthInPixels —

public static double get_ceiling_or_floor(double this_val , boolean is_max )
<
double this_min_tmp;
int this_sign;
int this_10_factor=0;
double this_rounded;

if (this_val == 0.0)
<
this_rounded = 0.0;
return this_rounded;
>

>
else
<
if (this_val > 0.0)
this_sign = -1;
else
this_sign = 1;

if (this_min_tmp > 1)
this_rounded = (double)(((int)(this_min_tmp / this_10_factor) + this_sign) * this_10_factor);
else
<
this_rounded = (int)(this_min_tmp * 100.0);
//’ cover same as above bfir number up to .001 less than tha it will skip
if (this_rounded >= 1 && this_rounded = 10 && this_rounded = 100 && this_rounded View Thread

scale pb

kingpio 7-Jun-11 1:20
great tutorial!

how do you edit the scale on the horizontal axis though? And with 7 values why are only 6 shown?

thx

Sign In· View Thread
quicky_XY?

AndroidAnnie 23-Mar-11 13:27
I admire greatly the results, but judging from the other responses, I’m evidently the only one who doesn’t get this.

Where is quicky_XY defined?

Sign In· View Thread
Re: quicky_XY? becker666 23-Mar-11 13:48

Download the source code and you’ll see it.

Becker

Sign In· View Thread
Re: quicky_XY?

AndroidAnnie 23-Mar-11 15:40

*Face palm* Oh, great. What am I supposed to do with all this code I just typed in by hand?
JJ — download worked superbly. Nice work. Many thanks.

Sign In· View Thread
Nice introduction

Oleksandr Kucherenko 6-Mar-11 22:49
I think you should also have to take a look on existing commercial solutions, for example:

Some of them are free and open source.

Sign In· View Thread
My vote of 5 Bosah Chude 4-Mar-11 3:06
Excellent Post
Sign In· View Thread
My vote of 5 gourav vaidya 28-Feb-11 23:37
Nice Tutorial
Sign In· View Thread
My vote of 5 Member 4387143 25-Aug-10 3:02
Excelent One
Sign In· View Thread
Last Visit: 31-Dec-99 19:00 Last Update: 4-Dec-21 18:46 Refresh 1

General News Suggestion Question Bug Answer Joke Praise Rant Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Источник

Читайте также:  Driving zone для android
Оцените статью