Quick Start Guide

Adding a Series

Now you have your chart it’s time to add some data. Adding data to a ShinobiChart is as simple as creating a DataAdapter, adding data to it and handing it to a series on the chart. For this app you’ll have 2 series, each with its own data adapter. Still inside the if statement add:

val dataAdapter1 = SimpleDataAdapter<Double, Double>()
val dataAdapter2 = SimpleDataAdapter<Double, Double>()

Each of these statements creates a new SimpleDataAdapter - it notifies the chart every time a data point/set of data points is added or removed, and is supplied out the box.

Now, add some data to it - sine waves

for (i in 0..99) {
    val radians = i * Math.PI / 25.0
    dataAdapter1.add(DataPoint<Double, Double>(radians, Math.sin(radians)))
    dataAdapter2.add(DataPoint<Double, Double>(radians, Math.cos(radians)))
}

A DataPoint has an X and a Y value and is parameterized by the types of those X and Y values. Like SimpleDataAdapter it’s supplied out the box for when you just need a simple data point representation. You can instead create your own data points by implementing the Data interface. Here you are using Double, but the types should be appropriate for their intended axes. NumberAxis is compatible with any numeric data which is representable using kotlin.Number or its subclasses, and will accept Data objects parameterized to those types.

Next, create 2 line series and give the data adapters to them. Finally, give them to the chart.

val series1 = LineSeries()
series1.dataAdapter = dataAdapter1
shinobiChart.addSeries(series1)

val series2 = LineSeries()
series2.dataAdapter = dataAdapter2
shinobiChart.addSeries(series2)

Excellent, you now have your first ShinobiChart complete with data. But don’t go anywhere just yet, you still need to style the chart a bit to make it a little bit more exciting.

Tip: Some devices have to be told that the application requires hardware acceleration in order to display a chart. Simply add android:hardwareAccelerated="true" to the application element of the AndroidManifest.xml file in your project.


<< Previous: Draw a Simple Chart | Next: Basic Styling >>