Overview of Chart in Xamarin.Forms Part 1 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (192)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (131)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (882)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (125)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (613)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (488)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (41)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (368)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (577)What's new  (313)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Overview of Chart in Xamarin.Forms Part 1

The Syncfusion chart control for Xamarin.Forms can plot more than 25 chart types, ranging from line charts to specialized financial charts. Its rich feature set includes functionalities such as data binding, multiple axes, legends, animation, data labels, annotations, trackball, tooltips, and zooming.
In this blog, we are going to explain how to add the Syncfusion chart reference to your Xamarin.Forms application and configure some basic elements that are required in typical use cases of the chart. It includes creating and binding data to the chart and enabling legend, title, and data labels. The following image represents the final output of the chart we are going to build.

Note: The chart creation and configuration in this blog is explained entirely with XAML code. However, it can also be done in C# code.

 

Adding the chart reference

The chart reference can be added to a Xamarin.Forms project in different ways. The quickest and easiest way is to add it as a NuGet package. The Syncfusion Xamarin.Forms components are available on nuget.org, so the chart can be installed directly from NuGet just like any other NuGet installation. Follow these steps to add the components from the NuGet server.
  1. Right-click the solution and choose Manage NuGet Packages for Solution.
  2. Select nuget.org from the Package source drop-down.
  3. Search for SfChart using the search box, and then select Syncfusion.Xamarin.SfChart from the search results.
  4. Select all the target platform projects along with the PCL or .NET Standard library from the Projects box.
  5. Install the package.

Refer to the following documentation to learn more about referencing the Syncfusion chart in a Xamarin.Forms application and how to configure it:

https://help.syncfusion.com/xamarin/charts/getting-started#adding-sfchart-reference

 

Creating a chart

Create an instance of the SfChart class and add it to the layout that suits your requirements. The SfChart class is inherited from View, so you can use all configurations of View in SfChart such as background color, margin, behaviors, animation, and more. The following code sample illustrates how to set the SfChart to be the content of ContentPage and initialize the axes.

 

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
             
             
             
             x_Class="ChartGettingStarted.MainPage">

    <chart:SfChart>
        <chart:SfChart.PrimaryAxis>
            <chart:CategoryAxis/>
        </chart:SfChart.PrimaryAxis>
        <chart:SfChart.SecondaryAxis>
            <chart:NumericalAxis/>
        </chart:SfChart.SecondaryAxis>
    </chart:SfChart>

</ContentPage>

After configuring the code, run the app on all targeted platforms to make sure the chart is properly set up and added in the correct position of the screen.

Populating data

Typically, data comes from a server in most real-time applications. The same data can be bound to the chart by specifying the properties of the underlying model. This allows data to be fetched without having to make any duplicate data collection for the chart. Here, we are populating the chart with dummy data to demonstrate how to bind it with the chart and how to specify the binding paths to fetch the data from the model.
public class Model
{
    public string Year { get; set; }

    public double Value { get; set; }
}

public class ViewModel
{
    public List<Model> ProductA { get; set; }

    public List<Model> ProductB { get; set; }

    public ViewModel()
    {
        ProductA = new List<Model>();
        ProductB = new List<Model>();

        ProductA.Add(new Model { Year = "2010", Value = 5 });
        ProductA.Add(new Model { Year = "2011", Value = 4.3 });
        ProductA.Add(new Model { Year = "2012", Value = 4.8 });
        ProductA.Add(new Model { Year = "2013", Value = 5.2 });
        ProductA.Add(new Model { Year = "2014", Value = 5.7 });
        ProductA.Add(new Model { Year = "2015", Value = 6.7 });

        ProductB.Add(new Model { Year = "2010", Value = 4.2 });
        ProductB.Add(new Model { Year = "2011", Value = 4.9 });
        ProductB.Add(new Model { Year = "2012", Value = 5.3 });
        ProductB.Add(new Model { Year = "2013", Value = 4.3 });
        ProductB.Add(new Model { Year = "2014", Value = 4.9 });
        ProductB.Add(new Model { Year = "2015", Value = 5.5 });
    }
}
Set the ViewModel instance as the BindingContext of your page. This is done to bind ViewModel properties to SfChart.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
             
             
             
             x_Class="ChartGettingStarted.MainPage">

    <ContentPage.BindingContext>
        <local:ViewModel/>
    </ContentPage.BindingContext>
</ContentPage>
Bind the data to the chart and set XBindingPath and YBindingPath properties to fetch the values from the respective properties in the data model to plot the series.
<chart:SfChart>
…
    <chart:SplineAreaSeries ItemsSource="{Binding ProductA}" XBindingPath="Year" YBindingPath="Value"></chart:SplineAreaSeries>
    <chart:SplineAreaSeries ItemsSource="{Binding ProductB}" XBindingPath="Year" YBindingPath="Value"></chart:SplineAreaSeries>
</chart:SfChart>

Setting a title

The title informs users what to look for in the chart. The text of the title can be set and customized using the Title property of SfChart, as demonstrated in the following code.
<chart:SfChart>

    <chart:SfChart.Title>
        <chart:ChartTitle Text="Average Sales Comparison"></chart:ChartTitle>
    </chart:SfChart.Title>

</chart:SfChart>

Enabling the legend

The legend contains information about the series or data points in a chart. It also allows users to toggle the visibility of series or data points. The value of the Label property of chart series will be displayed in the legend items. In accumulation charts like pie, doughnut, pyramid, and funnel, the series’ x values will be displayed in the legend. The following code is used to enable the legend and set the labels for the series.
<chart:SfChart>
    
    <chart:SfChart.Legend>
        <chart:ChartLegend></chart:ChartLegend>
    </chart:SfChart.Legend>
   
    <chart:SplineAreaSeries ItemsSource="{Binding ProductA}" Label="Product A" XBindingPath="Year" YBindingPath="Value"></chart:SplineAreaSeries>
    
    <chart:SplineAreaSeries ItemsSource="{Binding ProductB}" Label="Product B" XBindingPath="Year" YBindingPath="Value"></chart:SplineAreaSeries>
    
</chart:SfChart>

Enabling data labels

Data points can be easily annotated with labels to improve the readability of the data. The readability can also be further enhanced by adding markers or customizable symbols. Labels are generally used when the chart has a small amount of data, otherwise the trackball or tooltip features are used to view information about data points for their good performance in more densely populated charts.

<chart:SfChart>

    <chart:SplineAreaSeries ItemsSource="{Binding ProductA}" Label="Product A" XBindingPath="Year" YBindingPath="Value">
        <chart:SplineAreaSeries.DataMarker>
            <chart:ChartDataMarker></chart:ChartDataMarker>
        </chart:SplineAreaSeries.DataMarker>
    </chart:SplineAreaSeries>

    <chart:SplineAreaSeries ItemsSource="{Binding ProductB}" Label="Product B" XBindingPath="Year" YBindingPath="Value">
        <chart:SplineAreaSeries.DataMarker>
            <chart:ChartDataMarker></chart:ChartDataMarker>
        </chart:SplineAreaSeries.DataMarker>
    </chart:SplineAreaSeries>

</chart:SfChart>

The following code sample is the complete chart configuration.

<chart:SfChart>

    <chart:SfChart.Legend>
        <chart:ChartLegend></chart:ChartLegend>
    </chart:SfChart.Legend>

    <chart:SfChart.Title>
        <chart:ChartTitle text="Average Sales Comparison"></chart:ChartTitle>
    </chart:SfChart.Title>

    <chart:SfChart.Legend>
        <chart:ChartLegend></chart:ChartLegend>
    </chart:SfChart.Legend>

    <chart:SfChart.ColorModel>
        <chart:ChartColorModel Palette="Natural"></chart:ChartColorModel>
    </chart:SfChart.ColorModel>
 
   <chart:SfChart.PrimaryAxis>
        <chart:CategoryAxis EdgeLabelsDrawingMode="Shift">
            <chart:CategoryAxis.Title>
                <chart:ChartAxisTitle Text="Year"></chart:ChartAxisTitle>
            </chart:CategoryAxis.Title>
        </chart:CategoryAxis>
    </chart:SfChart.PrimaryAxis>

    <chart:SfChart.SecondaryAxis>
        <chart:NumericalAxis>
            <chart:NumericalAxis.Title>
                <chart:ChartAxisTitle Text="Sales Amount in Millions"></chart:ChartAxisTitle>
            </chart:NumericalAxis.Title>
        </chart:NumericalAxis>
    </chart:SfChart.SecondaryAxis>

    <chart:SplineAreaSeries ItemsSource="{Binding ProductA}" Label="Product A" XBindingPath="Year" Opacity="0.5" YBindingPath="Value">
        <chart:SplineAreaSeries.DataMarker>
            <chart:ChartDataMarker></chart:ChartDataMarker>
        </chart:SplineAreaSeries.DataMarker>
    </chart:SplineAreaSeries>

    <chart:SplineAreaSeries ItemsSource="{Binding ProductB}" Label="Product B" XBindingPath="Year" Opacity="0.5" YBindingPath="Value">
        <chart:SplineAreaSeries.DataMarker>
            <chart:ChartDataMarker></chart:ChartDataMarker>
        </chart:SplineAreaSeries.DataMarker>
    </chart:SplineAreaSeries>

</chart:SfChart>
In this blog, we kick-started the configuration of the Syncfusion chart control in a Xamarin.Forms application and have shown how easy it is to create and configure a beautiful chart in just a few lines of code. There are many advanced features not discussed here that will help us meet any application requirements.

What’s next

If you’re already a Syncfusion user, you can download the Xamarin setup here. If you’re not yet a Syncfusion user, you can download a free, 30-day trial here. You can also explore the chart samples available on Google Play, Windows Store and the iOS App Store, and learn about advanced features in our documentation.

Part 2 of our overview of the chart in Xamarin.Forms is available here.

If you have any questions or require clarification about this feature, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top