Xamarin.Android PlotView控件之折线图+柱形图+饼图使用

一、从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;
        }

五、运行效果图:
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值