Quick Start Guide

Draw a Simple Chart

Start by creating a new Android Application project:

  1. From the Welcome screen click New Project (if you have a project already open click File > New > New Project…)
  2. Give the application an Application name: shinobi-charts-quickstart and a suitable Package name (we use com.example.shinobiquickstart but you should use your own)
  3. Choose a suitable location for the project and click Next
  4. Click Next again to choose the default form factor (Phone and Tablet) and Minimum SDK (shinobicharts supports down to Android API 9, however to keep things simple and avoid needing the Android Support Library we’ll use API 12)
  5. Click Blank Activity and click Next
  6. Choose an appropriate name for your activity - this guide will assume the main activity is named ShinobiQuickStartActivity
  7. Click Finish and your project will be created

Now that you have your new project set up follow the Android Studio import instructions to add a dependency to the shinobicharts library from your application. If you are using Eclipse then you can follow the Eclipse import instructions.

Add the ShinobiChart

In the XML layout file for your Activity (named activity_shinobi_quick_start.xml), which can be found in the res/layout/ folder, click on the tab along the bottom called Text to show the raw XML. Replace the TextView with a ChartFragment:

<fragment
    class="com.shinobicontrols.charts.ChartFragment"
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Here the ChartFragment is being told to match the dimensions of its parent and is being given an id which makes it easier to retrieve in code.

Now open up ShinobiQuickStartActivity.java. You’ll see Android Studio has already added some code to your Activity. You only want to set up your chart the first time the Activity is created so add the following to the end of the onCreate() method:

if (savedInstanceState == null) {

}

In Android, configuration changes such as rotating your device cause Activities to be destroyed and re-created. You can tell if the Activity is being created for the first time by checking to see if savedInstanceState == null. Normally Fragments within an Activity will be destroyed and re-created as well. However, by default, a ChartFragment is retained across Activity re-creation so you only need to do the setup once.

Add the following inside the if statement you just added:

ChartFragment chartFragment =
                (ChartFragment) getFragmentManager().findFragmentById(R.id.chart);

ShinobiChart shinobiChart = chartFragment.getShinobiChart();
shinobiChart.setTitle("My First Chart");

Using the id you gave the ChartFragment in the XML, and the Activity’s FragmentManager, you can get a reference to it in code. From the ChartFragment you obtain the ShinobiChart and give it a title.

Tip: You can use the Alt+Enter keyboard shortcut in Android Studio to add the necessary import statements.

If you have downloaded a trial version of shinobicharts you will have been issued with a trial license key. You will need to give this key to the chart in order for it to be displayed. You can do this as follows:

shinobiChart.setLicenseKey("<license_key_here>");

Make sure you change <license_key_here> to the trial license key you were given!

The main parts of a chart can be configured via some key properties on the ShinobiChart object. For example, you’ll need to add the axes and series. In this example, you’re making a chart with three columns on linear numeric scales. For now, add the axes and we’ll come back to the series a little bit later. Still inside the if statement add:

NumberAxis xAxis = new NumberAxis();
shinobiChart.setXAxis(xAxis);

NumberAxis yAxis = new NumberAxis();
shinobiChart.setYAxis(yAxis);

Here you’re creating both the X axis and Y axis as a NumberAxis and then setting them on the chart.

So with the exception of the data, the chart is set up.

Tip: To support devices down to API 9: Android 2.3 (Gingerbread) you will need to use SupportChartFragment instead of ChartFragment (in both the XML and the Java source). Additionally, you will need to change the ShinobiQuickStartActivity to extend FragmentActivity instead of Activity and use the getSupportFragmentManager() method instead of getFragmentManager(). Your project will need to reference the Android Support Library to do this.


<< Previous: Introduction | Next: Adding a Series >>