We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adding a new item to a ListView everytime you click a button

I am trying to make a method which when called adds a SFChart in a ListView (which may or may not already have charts in it) with all the data being taken from a json (the json is mapped using json.NET). 

I've uploaded the method that is supposed to add the chart (uploaded as addFunc.PNG), the XAML (Xaml.png), the JSON(json.png) and the models (models.PNG,Command.PNG and chartData.PNG). I am having a hard time figuring out where the logic is falling apart and would appreciate if someone can shed some light.


Attachment: pics_289a29f7.zip

9 Replies

DB Dinesh Babu Yadav Syncfusion Team May 8, 2019 04:59 AM

Hi Teodor, 
 
Thanks for using Syncfusion products. 
 
We would like to inform you that we have prepared the sample that fetches data from json and adds into a chart which is loaded inside ItemTemplate of SfListView. We have attached the sample with chart and SfListView, please find the sample and code snippet for your reference from below. 
 
 
MainPage: 
<ContentPage> 
        <Grid> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="50"/> 
                <RowDefinition Height="*"/> 
            </Grid.RowDefinitions> 
            <Button Text="Add Chart" Clicked="Button_Clicked"/> 
            <syncfusion:SfListView x:Name="listView" Grid.Row="1" 
                                    ItemSize="120" BackgroundColor="#FFE8E8EC" 
                         DragStartMode="OnHold,OnDragIndicator" 
                                    SelectionMode="None"> 
 
                <syncfusion:SfListView.ItemTemplate> 
                    <DataTemplate> 
                        <Frame HasShadow="True" BackgroundColor="White" Padding="0"  
                           InputTransparent="true"> 
                            <chart:SfChart HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
                                <chart:SfChart.PrimaryAxis> 
                                    <chart:CategoryAxis/> 
                                </chart:SfChart.PrimaryAxis> 
                                <chart:SfChart.SecondaryAxis> 
                                    <chart:NumericalAxis/> 
                                </chart:SfChart.SecondaryAxis> 
                                <chart:LineSeries ItemsSource="{Binding Datas}" XBindingPath="Country" YBindingPath="Count"/> 
                            </chart:SfChart> 
                        </Frame> 
                    </DataTemplate> 
                </syncfusion:SfListView.ItemTemplate> 
 
            </syncfusion:SfListView> 
      </Grid> 
</ContentPage> 
 
Model 
public class FullModel 
{ 
        public String Message; 
        public String status; 
        public Model[] Gold; 
       public FullModel() 
        { 
        } 
} 
 
public class Model 
{ 
        public Model() 
        { } 
        public Model(string name, double count) 
        { 
            Country = name; 
            Count = count; 
        } 
} 
 
public class ChartModel 
{ 
        public ObservableCollection<Model> Datas { get; set; } 
 
        public ChartModel(ObservableCollection<Model> data) 
        { 
            Datas = data; 
        } 
} 
 
ViewModel 
 
public class ViewModel 
    { 
        public ObservableCollection<ChartModel> ChartData 
        { 
            get; 
            set; 
        } 
        public FullModel Items 
        { 
            get; private set; 
        } 
        public ViewModel() 
        { 
            string json = @"{'Gold':[{'Country':'USA','Count':46},{'Country':'China','Count':36},{'Country':'Japan','Count':63},{'Country':'Australia','Count':54}]}"; 
 
            Items= JsonConvert.DeserializeObject<FullModel>(json); 
            ChartData = new ObservableCollection<ChartModel>(); 
            Random r = new Random(); 
 
            for (int i=0;i<4;i++) 
            { 
                AddItem(); 
                 
            } 
        } 
 
        public void AddItem() 
        { 
            var data = new ChartModel(new ObservableCollection<Model>() 
                    { 
                        new Model() 
                        { 
                            Country=Items.Gold[0].department, 
                            Count=Items.Gold[0].count 
                        }, 
                         new Model() 
                        { 
                             Country=Items.Gold[1].department, 
                            Count=Items.Gold[1].count 
                        }, 
                        new Model() 
                        { 
                             Country=Items.Gold[2].department, 
                            Count=Items.Gold[2].count 
                        }, 
                        new Model() 
                        { 
                             Country=Items.Gold[3].department, 
                            Count=Items.Gold[3].count 
                        } 
                    }); 
            ChartData.Add(data); 
        } 
    } 
 
 
Please let us know whether sample meets your requirement. 
 
Regards, 
Dinesh Babu Yadav 
 



TN Teodor Nikolaev May 8, 2019 05:59 AM

Thank you that really helped, but i want to ask since i changed the information that the JSON contains, instead of carrying information of a new chart now it carries new values for already existing charts is it possible to somehow update those charts with the new values (for example i have a chart that tracks a value of something for the last 24 hours and when there is an update i get a json with new values and i need to update them to this chart, but i wanna keep it only for the last 24 hours)



DB Dinesh Babu Yadav Syncfusion Team May 9, 2019 02:17 AM

Hi Teodor,  
 
We would like to inform you that you can update the collection from json by adding timer in your sample like below. You can add time delay in timer based on your requirement and update the collection from viewmodel. We have attached the tested sample for your reference, please find the sample and code snippet from below. 
 
 
 
public partial class MainPage : ContentPage 
{ 
        ViewModel viewModel; 
        public MainPage() 
        { 
            InitializeComponent(); 
        } 
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
            viewModel = (listView.BindingContext as ViewModel); 
            Random r = new Random(); 
            viewModel.ChartData.Clear(); 
            InitializeTimer(); 
        } 
 
        private async void InitializeTimer() 
        { 
            await WaitAndExecute(1000, () => 
            { 
                viewModel.AddItem(); 
            }); 
        } 
 
        private async Task WaitAndExecute(int milisec, Action actionToExecute) 
        { 
            await Task.Delay(1000); 
            actionToExecute(); 
        } 
 
} 
 
 
Please let us know whether sample meets your requirement. 
 
Regards, 
Dinesh Babu Yadav 
 



TN Teodor Nikolaev May 9, 2019 10:35 PM

The previous example works fine but it's not exactly what i wanted, I am working with signalR and i get a real time data feed via json files and when i map those json files i was wondering whats the best practice to update the charts i already have rendered with new values (e.g. every hour i will be fed new data point for my line chart and i want to update the chart with it) also is there any animation to updating the chart?



DB Dinesh Babu Yadav Syncfusion Team May 10, 2019 02:28 AM

Hi Nikolaev,  
  
While using ObservableCollection, the chart will surely update for each data added to the collection. If you use list or any other collection, then chart update only performs at the time of item source change.   
  
Currently, chart will not perform animation for each data point update, animation only perform at the time of item source changed.   
  
  
Regards,  
Dinesh Babu Yadav. 



TN Teodor Nikolaev May 10, 2019 03:51 AM

So if i understand correctly, if i have a column chart and i change values inside the item source there would be no animation? I need to change the item source as a whole not it content for animations to happen?


Thank you for the explanation on updating!


BK Bharathiraja K Syncfusion Team May 12, 2019 11:18 PM

Hi Nikolaev,  
 
Yes. You are correct. You need to change the whole item source to animation happen. 
 
Regards, 
Bharathi. 



TN Teodor Nikolaev May 13, 2019 12:46 AM

Thank you for the help,

I have another question to ask that seems silly but i just cant figure it out for some reason. I have a doughnut Chart with 2 data points and i want data point 1 to be green and data point 2. Now the problem i have is that i am using a c# method to add Charts that looks like this: 

ChartData.Add(new ChartModel(
                new ChartSeriesCollection()
                {
                        new DoughnutSeries()
                        {
                            //or else bind your data model.
                            ItemsSource = itemSource,
                            XBindingPath = chart.PrimaryAxeTitle,
                            YBindingPath = chart.SecondaryAxeTitle,
                            ExplodeOnTouch = true,
                            DoughnutCoefficient = 0.6f,
                        },
                }, Color.White
            ));

I know i am missing something stupidly obvious but for the life of me i can't see what (also a side question, is there a way i can add code snippets to my questions in this forum?)


BK Bharathiraja K Syncfusion Team May 13, 2019 11:24 PM

Hi Nikolaev, 
 
We can achieve your requirement by using the ColorModel of ChartSeries and Custom Palette. Please refer the below help document link for more details. 
 
 
Regards, 
Bharathi. 


Loader.
Live Chat Icon For mobile
Up arrow icon