A Simple XY Plot
[doc_header]
[last_updated_for version=0.5.1]
Overview
In this example we create an Activity, define an XYPlot View using XML and assign a single XYSeries to it. This is as simple as it gets. You will however need to already have your Android project setup and configured with access to the Androidplot jar. Checkout the
Quickstart instructions for details on how to do this.
Walkthrough
layout/Main.xml
If you want to use XML to arrange your UI, here's the straightforward way of doing it. The new thing to watch out for in v0.2 is the title attribute. It's mandatory and if you don't enter it, your program will not run.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.androidplot.xy.XYPlot
android:id="@+id/mySimpleXYPlot"
android:layout_width="fill_parent"
android:layout_height="100px"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
androidPlot.title="A Simple XYPlot Example"/>
</LinearLayout>
MyActivity.java
package com.androidplot.demos;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
import java.util.Arrays;
/**
* The simplest possible example of using AndroidPlot to plot some data.
*/
public class MyActivity extends Activity
{
private XYPlot mySimpleXYPlot;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// Create a couple arrays of y-values to plot:
Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1"); // Set the display title of the series
// same as above
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
null, // fill color (none)
new PointLabelFormatter(Color.WHITE)); // text color
// add a new series' to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);
// same as above:
mySimpleXYPlot.addSeries(series2,
new LineAndPointFormatter(
Color.rgb(0, 0, 200),
Color.rgb(0, 0, 100),
null,
new PointLabelFormatter(Color.WHITE)));
// reduce the number of range labels
mySimpleXYPlot.setTicksPerRangeLabel(3);
}
}