Articles in this section
Category / Section

How to add the Xamarin.Forms Charts in a ListView

2 mins read

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

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied