正文  图形图像 > Canvas/Bitmap >

Android平台创建 XY 图表的完整例子

[图片] android_xy.gif[代码] 绘制网格线的方法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......

[图片] android_xy.gif

[代码] 绘制网格线的方法

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

cur_elt_array = (String[])curElt;

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);

paint.setColor(Color.GRAY );

// 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 < 5 ; i++)

{

this_g.drawLine(drawSizes[0], drawSizes[1] +

(i * drawSizes[3] / 5), drawSizes[0] + drawSizes[2],

drawSizes[1] + (i * drawSizes[3] / 5), paint);

this_g.drawLine(drawSizes[0]+ (i * drawSizes[2] / 5),

drawSizes[1], drawSizes[0] + (i * drawSizes[2] / 5),

drawSizes[1] + drawSizes[3], paint);

}

// good for one value

print_axis_values_4_grid(this_g, cur_elt_array[1] ,

Double.toString(rounded_max) , Double.toString(rounded_min),

cur_elt_array[0] , 2 ,0 );

} // --- end of draw_grid ---