Home | History | Annotate | Download | only in demos
      1 package com.androidplot.demos;
      2 
      3 import android.app.Activity;
      4 import android.graphics.*;
      5 import android.os.Bundle;
      6 import android.view.View;
      7 import android.widget.ToggleButton;
      8 import com.androidplot.xy.*;
      9 
     10 import java.text.DecimalFormat;
     11 import java.util.Arrays;
     12 
     13 public class XYPlotWithBgImgActivity extends Activity {
     14     private static final String TAG = XYPlotWithBgImgActivity.class.getName();
     15 
     16 	private int SERIES_LEN = 50;
     17 	private Shader WHITE_SHADER = new LinearGradient(1, 1, 1, 1, Color.WHITE, Color.WHITE, Shader.TileMode.REPEAT);
     18 
     19 	private XYPlot plot;
     20 	private SimpleXYSeries series;
     21 
     22 	@Override
     23 	protected void onCreate(Bundle savedInstanceState) {
     24 		super.onCreate(savedInstanceState);
     25 		setContentView(R.layout.xy_plot_with_bq_img_example);
     26 
     27 		plot = (XYPlot) findViewById(R.id.graph_metrics);
     28 
     29 		//For debugging.
     30         //plot.setMarkupEnabled(true);
     31 
     32         // Format Graph
     33         plot.getGraphWidget().getBackgroundPaint().setColor(Color.TRANSPARENT);
     34         plot.getGraphWidget().getGridBackgroundPaint().setShader(WHITE_SHADER);
     35         plot.getGraphWidget().getDomainGridLinePaint().setColor(Color.BLACK);
     36         plot.getGraphWidget().getDomainGridLinePaint().setPathEffect(new DashPathEffect(new float[]{3, 3}, 1));
     37         plot.getGraphWidget().getRangeGridLinePaint().setColor(Color.BLACK);
     38         plot.getGraphWidget().getRangeGridLinePaint().setPathEffect(new DashPathEffect(new float[]{3, 3}, 1));
     39         plot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
     40         plot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);
     41         //plot.getGraphWidget().setMarginTop(10);
     42 
     43         // Customize domain and range labels.
     44         plot.setDomainLabel("x-vals");
     45         plot.setRangeLabel("y-vals");
     46         plot.setRangeValueFormat(new DecimalFormat("0"));
     47 
     48         // Make the domain and range step correctly
     49         plot.setRangeBoundaries(40, 160, BoundaryMode.FIXED);
     50         plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 20);
     51         plot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 60);
     52         plot.setTicksPerDomainLabel(2);
     53 
     54         series = (SimpleXYSeries) getSeries();
     55 		LineAndPointFormatter lpFormat = new LineAndPointFormatter(
     56 				Color.BLACK,
     57 				Color.BLACK,
     58 				null, // No fill
     59 				new PointLabelFormatter(Color.TRANSPARENT) // Don't show text at points
     60 		);
     61         plot.addSeries(series, lpFormat);
     62         plot.redraw();
     63 	}
     64 
     65 	private SimpleXYSeries getSeries() {
     66 		Integer[] xVals = new Integer[SERIES_LEN];
     67 		Integer[] yVals = new Integer[SERIES_LEN];
     68 
     69 		xVals[0] = 0;
     70 		yVals[0] = 0;
     71 
     72         for (int i = 1; i < SERIES_LEN; i += 1){
     73         	xVals[i] = xVals[i-1] + (int)(Math.random() * i);
     74         	yVals[i] = (int)(Math.random() * 140);
     75         }
     76 
     77         return new SimpleXYSeries(
     78         		Arrays.asList(xVals),
     79         		Arrays.asList(yVals),
     80         		"Sample Series");
     81 	}
     82 
     83 	public void onGraphStyleToggle(View v) {
     84 		boolean styleOn = ((ToggleButton) v).isChecked();
     85 
     86         /*RectF graphRect = plot.getGraphWidget().getGridRect();
     87         float segmentSize = 1.0f/6.0f;
     88         LinearGradient lg = new LinearGradient(
     89                 0,
     90                 graphRect.top,
     91                 0,
     92                 graphRect.bottom,
     93                 new int[]{
     94                         Color.RED,
     95                         Color.YELLOW,
     96                         Color.GREEN,
     97                         Color.WHITE},
     98                 new float[]{
     99                         0,
    100                         segmentSize*2,
    101                         segmentSize*3,
    102                         segmentSize*5
    103                 },
    104                 Shader.TileMode.REPEAT
    105         );
    106         plot.getGraphWidget().getGridBackgroundPaint().setShader(lg);*/
    107 
    108         RectF rect = plot.getGraphWidget().getGridRect();
    109         BitmapShader myShader = new BitmapShader(
    110                 Bitmap.createScaledBitmap(
    111                         BitmapFactory.decodeResource(
    112                                 getResources(),
    113                                 R.drawable.graph_background),
    114                         1,
    115                         (int) rect.height(),
    116                         false),
    117                 Shader.TileMode.REPEAT,
    118                 Shader.TileMode.REPEAT);
    119         Matrix m = new Matrix();
    120         m.setTranslate(rect.left, rect.top);
    121         myShader.setLocalMatrix(m);
    122         if (styleOn)
    123 	        plot.getGraphWidget().getGridBackgroundPaint().setShader(
    124 	        		myShader);
    125 		else
    126 			plot.getGraphWidget().getGridBackgroundPaint().setShader(WHITE_SHADER);
    127 
    128         plot.redraw();
    129 
    130 	}
    131 }
    132