Search code examples
c#chartsxamarin.androidmpandroidchart

Not able to create chart using MPAndroidChart in xamarin android


I tried below code to create a pie chart using MPAndroidChart in xamarin.android project..it is not giving any error but chart is not drawn.

Not able to find any example using xamarin.adroid. Please guide me.

PACKAGE USED:

<package id="MPAndroidChart" version="3.0.2" targetFramework="monoandroid60" />

LAYOUT:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout
    android:id="@+id/chartlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  </LinearLayout>  
</LinearLayout>

FRAGMENT:

public class ChartMainFragment : Android.Support.V4.App.Fragment
{

    private PieChart _chart;
    private float[] yData = { 5, 10, 15, 30, 40 };
    private String[] xData = { "Sony", "Huawei", "LG", "Apple", "Samsung" };
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        View view = inflater.Inflate(Resource.Layout.chart_main_layout, container, false);

        _chart = new PieChart(this.Activity);
        var chartlayout = view.FindViewById<LinearLayout>(Resource.Id.chartlayout);
        chartlayout.AddView(_chart);

        List<PieEntry> entries = new List<PieEntry>();

        entries.Add(new PieEntry(18.5f, "Green"));
        entries.Add(new PieEntry(26.7f, "Yellow"));
        entries.Add(new PieEntry(24.0f, "Red"));
        entries.Add(new PieEntry(30.8f, "Blue"));

        PieDataSet set = new PieDataSet(entries, "");
        PieData data = new PieData(set);
        _chart.Data=data;
        _chart.Invalidate(); // refresh
        return view;
    }

OUTPUT:

enter image description here

Please help me to find out reason why chart is not showing properly.

Thanks,

@Paul


Solution

  • Add the PieChart in your layout like this :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
      <LinearLayout
          android:id="@+id/chartlayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
       <com.github.mikephil.charting.charts.PieChart
          android:id="@+id/chart1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
           />
      </LinearLayout>  
    </LinearLayout>
    

    Then in your Fragment OnCreateView method add color :

    var mChart = view.FindViewById<PieChart>(Resource.Id.chart1);
    
    List<PieEntry> entries = new List<PieEntry>();
    
    entries.Add(new PieEntry(18.5f, "Sony"));
    entries.Add(new PieEntry(26.7f, "Huawei"));
    entries.Add(new PieEntry(24.0f, "Apple"));
    entries.Add(new PieEntry(30.8f, "Samsung"));
    
    PieDataSet set = new PieDataSet(entries, "");
    
    List<Integer> colors = new List<Integer>();
    
    foreach (Integer c in ColorTemplate.VordiplomColors)
        colors.Add(c);
    
    foreach (Integer c in ColorTemplate.JoyfulColors)
        colors.Add(c);
    
    foreach (Integer c in ColorTemplate.ColorfulColors)
        colors.Add(c);
    
    foreach (Integer c in ColorTemplate.LibertyColors)
        colors.Add(c);
    
    foreach (Integer c in ColorTemplate.PastelColors)
        colors.Add(c);
    
    colors.Add((Integer)ColorTemplate.HoloBlue);
    
    set.Colors = colors;
    
    PieData data = new PieData(set);
    mChart.Data = data;
    mChart.Invalidate(); // refresh
    return view;
    

    Effect.

    For more detail information, you could read this native android example, its usage is almost the same in C#. If there is any problem, please feel free to ask.