export sfchart automatically when the plot is ready

Hi all,

I am working on a project that receives data from USB and Plots using sfChart, after plot function I run export function in which I save Charts. However, the problem is whenever i save the charts it saves the previous state of the chart. this means after the first plotting the images are blank charts, and the second time it shows previous charts. 

the exporting process is Ok for ex if I use a button, but i want this process to be automatic, any suggestions ?

Thanks in advance for your help.


5 Replies

YP Yuvaraj Palanisamy Syncfusion Team January 20, 2022 05:34 PM UTC

Hi Navid,  
  
We can achieve your requirement automatically export the image when plot is ready by call the Save method using Task.Delay after updating the data for series ItemsSource as per the below code snippet. 
  
CodeSnippet 
private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    (this.DataContext as ViewModel).Data = new ObservableCollection<Model>() 
    { 
        new Model(11, 10), 
        new Model(12, 24), 
        new Model(13, 37), 
        new Model(14, 18), 
        new Model(15, 14), 
        new Model(16, 46), 
        new Model(17, 12), 
    }; 
 
    await Task.Delay(10); 
    this.chart.Save("AfterExport.png"); 
} 
 
Please find the sample from the below location and let us know if you have any other queries. 
 
  
Regards, 
Yuvaraj. 



NA navid replied to Yuvaraj Palanisamy January 20, 2022 05:56 PM UTC

Hi Yuvaraj,

Thanks for your response. But the problem is the data i am plotting is big from 20 MB to 300 MB. So using delays might not be the best solution as it might not be correct for plots which take more time to be ready. Is there a more structured way? Like a property or event or smth like that?




YP Yuvaraj Palanisamy Syncfusion Team January 21, 2022 03:00 PM UTC

Hi Navid, 
 
We would like to suggest that to call the Save method in LayoutUpdated event of SfChart control to export the chart after update data point for chart series using a bool value as per the following code example.  
MainWindow.xaml 
 
<chart:SfChart x:Name="chart" LayoutUpdated="chart_LayoutUpdated" > 
 
 
MainWindow.xaml.cs 
bool isUpdateData = false; 
 
private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Random random = new Random(); 
    ObservableCollection<Model> data = new ObservableCollection<Model>(); 
    for (int i = 0; i < 5000; i++) 
    { 
        data.Add(new Model(i, random.Next(100, 150))); 
    } 
 
   (this.DataContext as ViewModel).Data = data; 
    isUpdateData = true; 
} 
 
private void chart_LayoutUpdated(object sender, EventArgs e) 
{ 
    if (isUpdateData) 
    { 
        this.chart.Save("AfterExport.png"); 
        isUpdateData = false; 
    } 
} 
 
Please find the modified sample in the below location and let us know if have any other queries. 
 
 
Please let us know if you have any concern. 
 
Regards, 
Yuvaraj. 



NA navid January 22, 2022 10:23 AM UTC

Hello again Yuvaraj,

I really appretiate your time and effort, it solved my problem.




YP Yuvaraj Palanisamy Syncfusion Team January 24, 2022 07:36 AM UTC

Hi Navid, 
 
Thank you for your update. 
 
Please let us know if you need any further assistance. 
 
Regards, 
Yuvaraj.

Loader.
Up arrow icon