Table of Contents
Quickstart
We try to keep these pages as up to date as possible, but typos are inevitable, especially immediately following new releases. If you spot an error, please let us know either in the forums or shoot an email to oops@androidplot.com. Thanks!
Last Updated For: Androidplot 0.5.0
Overview
This tutorial will show you the bare essentials of how to display two series of data stored as Number arrays in an XYPlot. It is also included within the installable AndroidPlot Demo App.
You can also see the code in action by installing the Demo App on your device and clicking on the Simple XY Plot Example.
Step 1: Create the project
The first thing you’ll need is to get a project setup. There are a couple ways to go about this.
With Maven
Maven users can generate an Androidplot project from scratch by following these instructions.
With an IDE
How exactly one does this depends on which IDE (if any) that you use. Google does have some pretty good documentation on the process. Our recommendations would be to go with either IntelliJ IDEA or Eclipse with ADT. Here are a few links that might be useful:
Using the Android SDK
You’ll obviously need the Android SDK installed to do this. Assuming that <android-sdk-dir>/tools is on your PATH, run this from a command line:
|
1 2 3 4 5 6 |
android create project \ --target <target_ID> \ --name <your_project_name> \ --path path/to/your/project \ --activity <your_activity_name> \ --package <your_package_namespace> |
For more detailed instructions see the official Google documentation.
Step 2: Add the AndroidPlot library to the project
Simply download the latest version of AndroidPlot and copy it to your /libs directory. Maven Quickstart users can skip this step.
Step 3: Add an XYPlot Element to main.xml
Once you’ve got the project skeleton created, it’s time to edit /res/layout/main.xml and add an XYPlot view:
main.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="fill_parent" android:layout_marginTop="10px" android:layout_marginLeft="10px" android:layout_marginRight="10px" title="A Simple XYPlot Example"/> </LinearLayout> |
TODO
Step 4: Create the Activity
The example below uses com.androidplot.demos as it’s package – change this to whatever package you used when you created your project in step 1.
SimpleXYPlotActivity.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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 SimpleXYPlotActivity 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) // 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)); // reduce the number of range labels mySimpleXYPlot.setTicksPerRangeLabel(3); // by default, AndroidPlot displays developer guides to aid in laying out your plot. // To get rid of them call disableAllMarkup(): mySimpleXYPlot.disableAllMarkup(); } } |
Step 5: AndroidManifest.xml
This is a pretty straightforward AndroidManifest.xml except for one minor detail: Applications with targetSdkVersion set to 14 or above should disable hardware acceleration as AndroidPlot uses methods that are not currently supported by hardware acceleration. If other parts of your app outside of AndroidPlot rely on hardware acceleration, see here for additional methods of controlling hardware acceleration at the Activity and View level. Also note that in the upcoming 0.5.1 release, AndroidPlot disables hardware acceleration on it’s own Views by default.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="16" /> <application android:label="Quickstart" android:icon="@drawable/icon" android:hardwareAccelerated="false"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
What’s Next?
Now that you know the basics you’re ready to start plotting dynamic data.


thanks. Like the new site.
Can you help me, I tried to make this but it doesn’t work because I use values out of a database. There is no Exception, but only Series 2 ist visible.
Here is my Code:
package de.gerding.wert_auslesen_wifi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
/**
* The simplest possible example of using AndroidPlot to plot some data.
*/
public class diagrammAnzeigen extends Activity
{
private XYPlot mySimpleXYPlot;
private ArduinoCommManager commMan;
private WifiManager wifi;
private InetAddress ip;
private int port = 1234;
private int pin = 15;
static String HOST = “Http://192.168.72.1:5984/”;
//static String DBNAME = “my_test_db”;
static String DBNAME = “my_auto_test”;
static String DOC_ID = “doc_id”;
private static int anzahl;
private static String[] entries;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagramm);
// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// Create a couple arrays of y-values to plot:
//int anzahl;
//anzahl = CouchDBManager.getDatabaseSize(DBNAME);
//String[] entries;
//entries = CouchDBManager.getAllEntries(DBNAME);
try
{
ip = InetAddress.getByName(“192.168.72.2″);
} catch (UnknownHostException e)
{
Log.i(“diagrammAnzeigen”,”onCreate-IpFehler”);
e.printStackTrace();
Toast.makeText(this, “Fehler beim setzen der IP”, Toast.LENGTH_LONG).show();
}
final int spin = pin;
final InetAddress sip = ip;
final int sport = port;
final Handler my2Handler = new Handler(); // wird automatisch mit aktuellem Thread vebunden.
new Thread()
{
public void run()
{
anzahl = CouchDBManager.getDatabaseSize(DBNAME);
entries = CouchDBManager.getAllEntries(DBNAME);
}
}.start();
Number[] series1Numbers;
series1Numbers = new Number[anzahl];
for(int i = 0; i < anzahl; i++)
{
Number numObj = (Number)Integer.valueOf(entries[i]);
series1Numbers[i] = numObj;
}
//Number[] series1Numbers = {1, 8, 5, 2, 7, 4};//Werte einer Kurve
Number[] series2Numbers = {4, 6, 3, 8, 2, 10};//Werte einer zweiten Kurve
// 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)
// 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));
// reduce the number of range labels
mySimpleXYPlot.setTicksPerRangeLabel(3);
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
Log.i("WERT_AUSLESEN_WIFI","onKeyDown()");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Intent i = new Intent(this, WertAuslesen.class);;
finish();
startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
}
Hi Micka,
That’s a question better suited for the help forum. Can you repost it there? Here’s the link:
https://groups.google.com/forum/?fromgroups#!forum/androidplot
Followed everything and when I try to run it, it crashes and says: “03-27 20:01:29.954: E/dalvikvm(29320): Could not find class ‘com.androidplot.xy.XYPlot’, referenced from method com.plot.plottester.PlotTesterActivity.onCreate”