TL;DR: Let’s see how to analyze stock trends using the .NET MAUI Toolkit Candle Chart. We’ll fetch historical stock data, structure it, and visualize open, high, low, and close prices. The chart supports interactive features like zooming, panning, and real-time updates. We’ll enhance it with a secondary Y-axis, DateTime Range Selector, and Segmented Control for dynamic data exploration.
Welcome to our Chart of the Week blog series! Understanding market trends is crucial in finance. Stock charts help us to visualize price movements and trends elegantly. Syncfusion®.NET MAUI Toolkit Cartesian Chart allows users to create clear, interactive stock charts. With the support for Line, Candle, and OHLC chart types, it helps us to identify patterns and trends over time. Today, we’ll analyze Amazon’s stock prices using the .NET MAUI Toolkit Candle Chart to gain insights into market trends and price movements. The Candle Chart helps visualize open, high, low, and close (OHLC) values, making it easier to track price fluctuations and trading volume. Refer to the following image.
Next, we need to structure the stock data properly. We’ll define a StockModel class containing essential properties such as Date, Open, High, Low, Close, and Volume. Refer to the following code example.
public class StockModel
{
public DateTime Date { get; }
public double Open { get; }
public double High { get; }
public double Low { get; }
public double Close { get; }
public double Volume { get; }
public StockModel(DateTime date, double open, double high, double low, double close, double volume)
{
Date = date;
Open = open;
High = high;
Low = low;
Close = close;
Volume = volume;
}
}
To generate the data collection, we’ll use the StockViewModel class and StockPrices property. We’ll also create the ReadCSV method to convert the CSV data into a collection of stock data and store it in the StockPrices property. The RangeStart and RangeEnd properties define the date range for the stock data. Refer to the following code example.
public class StockViewModel : INotifyPropertyChanged
{
private ObservableCollection<StockModel>? _stockPrices;
private DateTime _rangeStart = new DateTime(2024, 1, 1);
private DateTime _rangeEnd = new DateTime(2024, 6, 30);
public DateTime RangeStart
{
get => _rangeStart;
set
{
if (_rangeStart != value)
{
_rangeStart = value;
OnPropertyChanged(nameof(RangeStart));
}
}
}
public DateTime RangeEnd
{
get => _rangeEnd;
set
{
if (_rangeEnd != value)
{
_rangeEnd = value;
OnPropertyChanged(nameof(RangeEnd));
}
}
}
public ObservableCollection<StockModel>? StockPrices
{
get => _stockPrices;
set
{
if (_stockPrices != value)
{
_stockPrices = value;
OnPropertyChanged(nameof(StockPrices));
}
}
}
public StockViewModel()
{
StockPrices = new ObservableCollection<StockModel>(
ReadCSV("StockChart.Resources.Raw.amazon.csv").Reverse()
);
}
public ObservableCollection<StockModel> ReadCSV(string resourceStream)
{
var executingAssembly = typeof(App).GetTypeInfo().Assembly;
using var inputStream = executingAssembly.GetManifestResourceStream(resourceStream)
?? throw new FileNotFoundException($"Resource {resourceStream} not found.");
var lines = new List<string>();
using (var reader = new StreamReader(inputStream))
{
string? line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
var formats = new[] { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy" };
return new ObservableCollection<StockModel>(
lines.Select(line =>
{
var data = line.Split(',');
if (data.Length < 6) return null;
if (DateTime.TryParseExact(data[0], formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
return new StockModel(
date,
Convert.ToDouble(data[1]),
Convert.ToDouble(data[2]),
Convert.ToDouble(data[3]),
Convert.ToDouble(data[4]),
Convert.ToDouble(data[5]) / 1000000
);
}
return null;
})
.Where(stock => stock != null)
.ToList()!
);
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler? PropertyChanged;
}
Step 3: Configure the layout
In this demo, we’ll use the following three essential controls:
Step 6: Binding data to .NET MAUI Candle and Column Series
Next, we’ll bind the stock data to the Candle and Column Charts series. Before doing this, we need to set the ViewModel as the BindingContext for the ContentPage. This allows us to access the ViewModel across the entire page. If we bind the ViewModel only to SfCartesianChart, we won’t be able to access it globally within the page, which is necessary for other controls. Refer to the following code example.
To enhance the user experience, we can add interactive features like the trackball, which allows users to see precise data points when hovering over the chart. Refer to the following code example.
Step 10: Connect the DateTime Range Selector to the Candle Chart
In our case, we use the SfDateTimeRangeSelector to easily select a specific date range. To achieve this, we bind the RangeStart and RangeEnd properties of the SfDateTimeRangeSelector to the Minimum and Maximum values of the Candle Chart’s X-axis. Refer to the following code example.
Refer to the following image. Adding DateTime Range Selector to the .NET MAUI Candle Chart
Step 12: Add Segmented Control to customize ranges
We use the Segmented Control to switch between different time ranges, such as 3months, 6months, 9months, 1year, and all data for Amazon’s stock from November 2023 to January 2025. This allows users to visualize the stock price data based on the selected range. Refer to the following code example.
Let’s customize the chart trackball using the TrackballTemplate property. This allows us to change the layout, colors, fonts and displayed data within the trackball. Refer to the following code example.
These customizations enhance the readability and visual appeal of your charts and controls, making the data easier to interpret and analyze. After executing these code examples, we’ll get the output that resembles the following image. Analyzing stock prices and market trends using .NET MAUI Toolkit Candle Chart
Thank you for reading! In this blog, we’ve explored how to analyze prices and market trends using the Syncfusion®.NET MAUI Toolkit Candle Chart. We encourage you to follow the steps outlined in this blog. Stay tuned for next week’s Chart of the Week! If you require assistance, please don’t hesitate to contact us via our support forum, support portal, or feedback portal. We are always eager to help you!