import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import com.androidplot.Plot;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
import java.text.*;
import java.util.Arrays;
import java.util.Date;
public class MyActivity extends Activity
{
private XYPlot mySimpleXYPlot;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
Number[] numSightings = {5, 8, 9, 2, 5};
Number[] years = {
978307200, // 2001
1009843200, // 2002
1041379200, // 2003
1072915200, // 2004
1104537600 // 2005
};
// create our series from our array of nums:
XYSeries series2 = new SimpleXYSeries(
Arrays.asList(years),
Arrays.asList(numSightings),
"Sightings in USA");
mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.getGraphWidget().getGridLinePaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getGridLinePaint().setPathEffect(new DashPathEffect(new float[]{1,1}, 1));
mySimpleXYPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);
mySimpleXYPlot.setBorderStyle(Plot.BorderStyle.SQUARE, null, null);
mySimpleXYPlot.getBorderPaint().setStrokeWidth(1);
mySimpleXYPlot.getBorderPaint().setAntiAlias(false);
mySimpleXYPlot.getBorderPaint().setColor(Color.WHITE);
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 100, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.rgb(100, 200, 0)); // fill color
// setup our line fill paint to be a slightly transparent gradient:
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));
LineAndPointFormatter formatter = new LineAndPointFormatter(Color.rgb(0, 0,0), Color.BLUE, Color.RED);
formatter.setFillPaint(lineFill);
mySimpleXYPlot.getGraphWidget().setPaddingRight(2);
mySimpleXYPlot.addSeries(series2, formatter);
// draw a domain tick for each year:
mySimpleXYPlot.setDomainStep(XYStepMode.SUBDIVIDE, years.length);
// customize our domain/range labels
mySimpleXYPlot.setDomainLabel("Year");
mySimpleXYPlot.setRangeLabel("# of Sightings");
// get rid of decimal points in our range labels:
mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0"));
mySimpleXYPlot.setDomainValueFormat(new Format() {
// create a simple date format that draws on the year portion of our timestamp.
// see http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
// for a full description of SimpleDateFormat.
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
// because our timestamps are in seconds and SimpleDateFormat expects milliseconds
// we multiply our timestamp by 1000:
long timestamp = ((Number) obj).longValue() * 1000;
Date date = new Date(timestamp);
return dateFormat.format(date, toAppendTo, pos);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}
}