无法在 xamarin 中使用 MPAndroidChart 创建图表 android

Not able to create chart using MPAndroidChart in xamarin android

我尝试使用下面的代码在 xamarin.android 项目中使用 MPAndroidChart 创建饼图。它没有给出任何错误,但未绘制图表。

无法使用 xamarin.adroid 找到任何示例。请指导我。

使用的包:

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

布局:

<?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>

片段:

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;
    }

输出:

请帮我找出图表显示不正确的原因。

谢谢,

@保罗

像这样在布局中添加 PieChart :

<?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>

然后在您的 Fragment OnCreateView 方法中添加颜色 :

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.

更多详细信息,您可以阅读这篇native android example,它的用法在C#中几乎相同。如有问题,欢迎随时提问