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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add the Xamarin.Forms Charts in a ListView

Platform: Xamarin.Forms |
Control: SfChart

Xamarin.Forms Charts provides support to add the chart inside the ListView control. You can add the chart in the ListView with the help of the ItemTemplate property as shown in the following code sample.


[XAML]

…
    
    <ListView x:Name="listView" ItemsSource="{Binding SeriesItems}" RowHeight="200" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout >
                        <chart:SfChart  VerticalOptions="FillAndExpand" Series="{Binding Series}" >
                            <chart:SfChart.Title>
                                <chart:ChartTitle Text="{Binding ChartTitle}"  FontAttributes="Bold"  TextColor="Aqua"/>
                            </chart:SfChart.Title>
                            <chart:SfChart.PrimaryAxis>
                                <chart:CategoryAxis/>
                            </chart:SfChart.PrimaryAxis>
                            <chart:SfChart.SecondaryAxis>
                                <chart:NumericalAxis  />
                            </chart:SfChart.SecondaryAxis>
                        </chart:SfChart>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

 

[C#]

public class ViewModel
    {
        public ObservableCollection<SeriesViewModel> SeriesItems { get; set; }
 
        public ViewModel()
        {
            SeriesItems = new ObservableCollection<SeriesViewModel>();
            SeriesViewModel model = new SeriesViewModel();
 
            SeriesItems.Add(new SeriesViewModel()
            {
                ChartTitle = "ColumnChart",
                Series = new ChartSeriesCollection(){ new ColumnSeries(){
                        ItemsSource=model.Data2
                    } },
            });
 
            ….
 
            SeriesItems.Add(new SeriesViewModel()
            {
                ChartTitle = "PieChart",
                Series = new ChartSeriesCollection(){ new PieSeries(){
                        ItemsSource=model.Data1
                    } },
            });
        }
    }
 
 

 

[C#]

    public class SeriesViewModel
    {
        public SeriesViewModel()
        {
            Data1 = new ObservableCollection<Model>();
            Data2 = new ObservableCollection<Model>();
            Data3 = new ObservableCollection<Model>();
 
            Data1.Add(new Model("Jan", 10));
            ..
            Data1.Add(new Model("May", 10));
 
            Data2.Add(new Model("EEE", 20));
            …
            Data2.Add(new Model("IT", 14));
 
            Data3.Add(new Model("Benz", 13));
            …
            Data3.Add(new Model("Jaguar", 19));
        }
 
        public ObservableCollection<Model> Data1 { get; set; }
 
        public ObservableCollection<Model> Data2 { get; set; }
 
        public ObservableCollection<Model> Data3 { get; set; }
 
        public string ChartTitle { get; set; }
 
        public ChartSeriesCollection Series { get; set; }
    }

 

[C#]

  public class Model
    {
        public Model(string x, double y)
        {
            XValue = x;
            YValue = y;
        }
 
        public string XValue { get; set; }
 
        public double YValue { get; set; }
    }

Output

Display a list of chart in Xamarin.Forms

View the complete sample in GitHub.


See also

How to populate the Json data to Xamarin.Forms Charts

How to bind the SQLite Database to the Xamarin.Forms Charts

How to bind series from MVVM pattern in Xamarin.Forms Charts

How to localize the labels in Xamarin.Forms Charts

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile