我尝试使用下面的代码在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>
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;
}
输出:
请帮我找出图表显示不正确的原因。
谢谢,
@保罗
答案 0 :(得分:2)
在您的布局中添加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;
有关更多详细信息,您可以阅读此native android example,其用法在C#中几乎相同。如果有任何问题,请随时询问。