Chart OHLC Series
OhlcSeries are a type of CategoricalSeries that are typically used to illustrate movements in the price of a financial instrument over time. That movement is represented by lines visualizing the open, high, low and close price (thus the name Ohlc) for a specific period. Each line on the chart shows the price range (the highest and lowest prices) over one unit of time, e.g., one day or one hour. Tick marks project from each side of the line indicating the opening (starting) price on the left, and the closing (ending) price for that time period on the right. The lines are shown in different colors depending on whether prices rose or fell in that period.
Setup
To display an Ohlc Chart, you will need to:
- Add a RadCartesianChart to your page.
- Set the chart's horizontalAxis to a category axis (CategoricalAxis, DateTimeCategoricalAxis or DateTimeContinuousAxis).
- Set the chart's verticalAxis to a value axis (LinearAxis or LogarithmicAxis).
- Add at least one instance of OhlcSeries to the chart's series property and set its items property to a collection of data items, its categoryProperty set to the name of the property of the data items that will be used to determine their category and the openPropertyName, highPropertyName, lowPropertyName and closePropertyName set to the names of the properties used to determine the open, high, low and close values.
To illustrate this setup, let's create an example. First we will create a source with items:
Example 1: Define a source with data
get ohlcSourceItems() {
return [
{ Date: "20/6/2015", Open: 100, Close: 85, Low: 50, High: 139 },
{ Date: "02/7/2015", Open: 60, Close: 150, Low: 40, High: 159 },
{ Date: "01/8/2015", Open: 120, Close: 81, Low: 45, High: 141 },
{ Date: "01/9/2015", Open: 105, Close: 200, Low: 55, High: 250 }
];
}
We use an instance of this model to assign it as the bindingContext of the page we have put our Scatter Bubble series on:
Example 2: Update bindingContext
import { FinancialDataModel } from "../../data-models/financial-data-model";
export function onPageLoaded(args) {
const page = args.object;
page.bindingContext = new FinancialDataModel();
}
And finally, in the XML definition of the page we put a RadCartesianChart, add a ScatterBubbleSeries instance to it and bind the series to the source of data:
Example 3: Add chart to page's markup
<navigation:ExamplePage xmlns:navigation="navigation/example-page" loaded="onPageLoaded" xmlns:chart="nativescript-ui-chart" xmlns="http://www.nativescript.org/tns.xsd">
<chart:RadCartesianChart id="cartesianChart">
<chart:RadCartesianChart.horizontalAxis>
<chart:DateTimeCategoricalAxis dateFormat="yyyy-MM-dd" verticalLocation="Bottom" />
</chart:RadCartesianChart.horizontalAxis>
<chart:RadCartesianChart.verticalAxis>
<chart:LinearAxis/>
</chart:RadCartesianChart.verticalAxis>
<chart:RadCartesianChart.series>
<chart:OhlcSeries
categoryProperty="Date"
openPropertyName="Open"
highPropertyName="High"
lowPropertyName="Low"
closePropertyName="Close"
items="{{ ohlcSourceItems }}">
</chart:OhlcSeries>
</chart:RadCartesianChart.series>
</chart:RadCartesianChart>
</navigation:ExamplePage>
Figure 1: Chart with OhlcSeries on Android (left) and iOS (right)
References
Want to see this scenario in action? Check our SDK examples repo on GitHub. You will find this and many other practical examples with NativeScript UI.
Examples used in this article:
Related articles you might find useful: