This sample demonstrates how to perform lazy loading in WPF Chart(SfChart).
Here's a step-by-step guide on how to perform lazy loading in a WPF Chart using the Syncfusion SfChart
Initialize the SfChart with Primary and Secondary axis.
Hook the PanChanged event in SfChart and set the required ZoomFactor on the primary axis.
XAML
<syncfusion:SfChart PanChanged="chart_PanChanged">
<syncfusion:SfChart .PrimaryAxis>
<syncfusion:NumericalAxis x:name="xAxis" zoomfactor="0.5"/>
</syncfusion:SfChart .PrimaryAxis>
<syncfusion:SfChart .SecondaryAxis>
<syncfusion:NumericalAxis/>
</syncfusion:SfChart .SecondaryAxis>
.... Add your series here ....
</syncfusion:SfChart>
C#
SfChart chart = new SfChart();
chart.PanChanged += chart_PanChanged;
NumericalAxis xAxis = new NumericalAxis();
xAxis.ZoomFactor = 0.5;
chart.PrimaryAxis = xAxis;
NumericalAxis yAxis = new NumericalAxis();
chart.SecondaryAxis = yAxis;
this.Content = chart;
Initialize the ChartZoomPanBehavior and enable panning by setting EnablePanning to “True”.
<syncfusion:SfChart.Behaviors>
<syncfusion:ChartZoomPanBehavior EnablePanning="True"/>
</syncfusion:SfChart.Behaviors>
ChartZoomPanBehavior zoomPanBehavior = new ChartZoomPanBehavior();
zoomPanBehavior.EnablePanning = true;
chart.Behaviors.Add(zoomPanBehavior);
Implement the chart_PanChanged method to calculate the end range of the chart. As horizontal scrolling or panning reaches the end of the chart, additional data points are added, then adjust the position of the xAxis range using ZoomPosition.
**// startValue = You can set the last value of the data source in viewModel.**
private void chart_PanChanged(object sender, PanChangedEventArgs e)
{
var position = xAxis.ZoomPosition - xAxis.ZoomFactor;
if (e.Axis.Equals(xAxis) && position>=0)
{
// Update the data based on your requirement.
for (int i = 0; i < 4; i++)
{
viewModel.Data.Add(new Model(startValue, new Random().Next(10, 40)));
startValue++;
}
xAxis.ZoomPosition = 1;
}
}
If you are facing a path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.