There are various ways to create data sets or format data for Jet Chart to understand and use it properly.
Mainly, two data types are widely used for linear plots.
Coordinate<T>
Linear Data Set
Coordinate<T>
Coordinates are the root objects for the linear charts and these represent a (x , y) coordinate in the graphs with a value attached to them.
Any value or variable you give to the linear charts library needs to be wrapped in the Coordinate object. According to the value, the library will calculate the respective (x , y) coordinates in the plot.
Here is an example of how to create a Coordinate<T> object :-
// Coordinate Example wrapping a Float
val coordinate = Coordinate<Float>(10f)
// Coordinate Example wrapping a String
val coordinate2 = Coordinate<String>("Excellent")
You can also transform a Kotlin List<T> into a Coordinate List<T> or create your own list directly, both using in-built functions.
Example of the above :-
// Transforming a List<T> to List<Coordinate<T>>
val yLabel = mutableListOf("Excellent", "Good", "Average", "Bad", "Worst")
val yLabelList1: List<Coordinate<String>> = yLabel.toCoordinateSet()
// Creating our own List<Coordinate<T>>
val yLabelList2: List<Coordinate<Float>> = Coordinate.coordinateSetBuilder(
1f, 2f, 3f, 4f, 5f, 6f
)
// Creating our own List<Coordinate<T>>
val yLabelList3: List<Coordinate<Float>> = Coordinate.coordinateSetBuilder(
listOf(1f , 2f , 3f , 4f , 5f)
)
Linear Data Sets
This is a data type that is used for linear plots. This contains the dataset title indicating the type of dataset and the actual dataset, markers, which is List<Coordinate<T>>.
Static Data Set
Linear Data Sets, which stay static and don't change or need any re-composition, can be created normally, like any other class object, as well as with the help of a public function.
Here's an example of how to create it by directly assigning values to it :-
// Creating Linear Data directly from class constructor()
val linearDataSet1: List<LinearDataSet> = listOf(
LinearDataSet(
title = "Amazon",
markers = listOf(100f, 200f, 303f, 408f, 52f).toCoordinateSet()
)
)
// Creating Linear Data using the in - built function
val linearDataSet2: List<LinearDataSet> = listOf(
LinearDataSet.createDataSet(
title = "Amazon",
markers = listOf(100f, 200f, 303f, 408f, 52f)
),
LinearDataSet.createDataSet(
title = "Google",
markers = listOf(10f, 290f, 492f, 201f, 502f)
) // It converts the markers List<T> to List<Coordinate<T>> internally
)
Here is another way to create the same using pre-made lists :-
// Each List represents a data set
val list1 = listOf(6f, 5f, 4f, 6f, 7.5f, 7f, 6f)
val list2 = listOf(3f, 6f, 8f, 2f, 3.5f, 3f, 4f)
// You can directly set the lists like this
val linearDataSet1: List<LinearDataSet> = listOf(
LinearDataSet.createDataSet(title = "Amazon", markers = list1),
LinearDataSet(title = "Google", markers = list2.toCoordinateSet())
)
State Data Sets
Data Sets that can be remembered during re-composition can also be made and used like any other mutable State data. Developers can create them for dynamic UIs, where the data sets can change during runtime due to user interactions and re-composition becomes necessary.
Here's an example of how to create it by directly assigning values to it :-
// We can create multiple Linear Data Set inside the listOf()
val linearDataSet1: List<LinearDataSet> by remember {
mutableStateOf(
listOf(
LinearDataSet.createDataSet(
title = "Amazon",
markers = listOf(1f, 2f, 3f, 4f, 5f)
), // Using built - in functions
LinearDataSet(
title = "Google",
markers = listOf(1f, 2f, 3f, 4f, 5f).toCoordinateSet()
) // Using constructor call
)
)
}
Here is another way to create the same using pre-made lists :-
// Declare the lists from which you would want to create a Linear Data Set
val list1 = listOf(6f, 5f, 4f, 6f, 7.5f, 7f, 6f)
val list2 = listOf(3f, 6f, 8f, 2f, 3.5f, 3f, 4f)
// You can directly set the lists like this
val linearDataSet: List<LinearDataSet> by remember {
mutableStateOf(
listOf(
LinearDataSet.createDataSet(
title = "Amazon",
markers = list1
), // Using built - in functions
LinearDataSet(
title = "Google",
markers = list2.toCoordinateSet()
) // Using constructor call
)
)
}
Warning:- If you are unable to create Linear Data Sets properly, we advise you to read through this page again before proceeding forward to create plots.
After finishing, you can follow the link given below and head to the linear plot section.