发布:2021/10/15 15:50:42作者:管理员 来源:本站 浏览次数:937
一、从NuGet包管理器搜索 “OxyPlot.Xamarin.Android”控件,找到后安装。
二、添加PlotView控件到布局
activity_main.xml:
<OxyPlot.Xamarin.Android.PlotView
android:layout_width=“wrap_content”
android:layout_height=“200dp”
android:minWidth=“25px”
android:minHeight=“25px”
android:id="@+id/plotView1"/>
<OxyPlot.Xamarin.Android.PlotView
android:layout_width=“wrap_content”
android:layout_height=“200dp”
android:id="@+id/plotView2" />
<OxyPlot.Xamarin.Android.PlotView
android:layout_width=“wrap_content”
android:layout_height=“200dp”
android:id="@+id/plotView3" />
三、MainActivity类代码:
//绑定PlotModel对象
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
PlotView plotView1 = FindViewById<PlotView>(Resource.Id.plotView1);
plotView1.Model = CreatePlotModelLine();
PlotView plotView2 = FindViewById<PlotView>(Resource.Id.plotView2);
plotView2.Model = CreatePlotModelColumn();
PlotView plotView3 = FindViewById<PlotView>(Resource.Id.plotView3);
plotView3.Model = CreatePlotModelPie();
}
// ....
}
四、创建PlotModel对象
1、折线图
private PlotModel CreatePlotModelLine()
{
var plotModel = new PlotModel { Title = "OxyPlot Demo" };
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });
var series1 = new LineSeries
{
MarkerType = MarkerType.Circle,
MarkerSize = 4,
MarkerStroke = OxyColors.White
};
series1.Points.Add(new DataPoint(0.0, 6.0));
series1.Points.Add(new DataPoint(1.4, 2.1));
series1.Points.Add(new DataPoint(2.0, 4.2));
series1.Points.Add(new DataPoint(3.3, 2.3));
series1.Points.Add(new DataPoint(4.7, 7.4));
series1.Points.Add(new DataPoint(6.0, 6.2));
series1.Points.Add(new DataPoint(8.9, 8.9));
plotModel.Series.Add(series1);
return plotModel;
}
2、柱形图
private PlotModel CreatePlotModelColumn()
{
List<string> months = new List<string>(new[] {
"一月",
"二月",
"三月",
});
var plotModel = new PlotModel { Title = "OxyPlot Demo" };
plotModel.Axes.Add(new CategoryAxis { Position = AxisPosition.Bottom, ItemsSource = months });
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left });
ColumnSeries series = new ColumnSeries { ColumnWidth = 1, Title = "电能量" , LabelPlacement = LabelPlacement.Outside, LabelFormatString = "{0:0.##}"};
series.Items.Add(new ColumnItem(10));
series.Items.Add(new ColumnItem(20));
series.Items.Add(new ColumnItem(30));
plotModel.Series.Add(series);
return plotModel;
}
3、饼图
private PlotModel CreatePlotModelPie()
{
var plotModel = new PlotModel { Title = "OxyPlot Demo" };
PieSeries series = new PieSeries { StrokeThickness = 2.0, InsideLabelPosition = 0.8, AngleSpan = 360, StartAngle = 0 };
series.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false, Fill = OxyColors.PaleVioletRed });
series.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
series.Slices.Add(new PieSlice("Asia", 4157) { IsExploded = true });
series.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
series.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = true });
plotModel.Series.Add(series);
return plotModel;
}
五、运行效果图: