How to fill an empty points with dotted lines in .NET MAUI Cartesian Charts?
This article explains how to fill the empty area with dotted lines in the .NET MAUI Cartesian Charts.
To demonstrate this, consider the two stacking area series. If it is populated data with a NaN or null, then it will be plotted as shown in the following image.
To fill this empty space with dotted lines, use LineSeries with calculated data from two stacking area series’s data collection as shown in the following code sample.
Step 1: To plot the dashed line, use the StrokeDashArray property in the LineSeries.
XAML
<chart:SfCartesianChart>
<chart:SfCartesianChart.Resources>
<DoubleCollection x:Key="dashArray">
<x:Double>5</x:Double>
<x:Double>6</x:Double>
</DoubleCollection>
</chart:SfCartesianChart.Resources>
<chart:StackingAreaSeries ItemsSource="{Binding Data1}"
XBindingPath="Date"
YBindingPath="Value"
Fill="#0090AF"/>
<chart:StackingAreaSeries ItemsSource="{Binding Data2}"
XBindingPath="Date"
YBindingPath="Value"
Fill="#8AC114"/>
<chart:LineSeries ItemsSource="{Binding LineData}"
XBindingPath="Date"
YBindingPath="Value"
Fill="#DB2C33"
StrokeDashArray="{StaticResource dashArray}"/>
</chart:SfCartesianChart>
Step 2: Data1 and Data2 are point collections of the stacking area series. Based on those two collections, create an items source for LineSeries.
C#
public class ViewModel
{
public ObservableCollection<Model> Data1 { get; set; }
public ObservableCollection<Model> Data2 { get; set; }
public ObservableCollection<Model> LineData { get; set; }
public bool IsEmpty { get; set; }
public ViewModel()
{
Data1 = new ObservableCollection<Model>();
Data2 = new ObservableCollection<Model>();
LineData = new ObservableCollection<Model>();
var date = new DateTime(2007, 08, 10);
var random = new Random();
for (int i = 0; i < 15; i++)
{
if (i == 3 || i == 10)
{
Data1.Add(new Model(date, double.NaN));
Data2.Add(new Model(date, double.NaN));
}
else
{
Data1.Add(new Model(date, random.Next(10, 30)));
Data2.Add(new Model(date, random.Next(10, 30)));
}
date= date.AddDays(1);
}
for(int i = 0;i < Data1.Count;i++)
{
if (double.IsNaN(Data1[i].Value))
{
IsEmpty = true;
}
else
{
IsEmpty = false;
}
if(IsEmpty)
{
if (!double.IsNaN(Data1[i-1].Value))
{
LineData.Add(new Model(Data1[i - 1].Date, 0));
LineData.Add(new Model(Data1[i - 1].Date, Data1[i-1].Value + Data2[i-1].Value));
}
if (!double.IsNaN(Data1[i+1].Value))
{
LineData.Add(new Model(Data1[i + 1].Date, Data1[i+1].Value + Data2[i+1].Value));
LineData.Add(new Model(Data1[i + 1].Date, 0));
LineData.Add(new Model(Data1[i + 1].Date, double.NaN));
}
}
}
}
}
Output
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to fill an empty area with dotted lines in .NET MAUI Chart (SfCartesianChart).
You can refer to our .NET MAUI Chart feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Chart documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!