专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

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

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:56:12       共计:3592 浏览
一、从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;
        }

  

五、运行效果图:

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Xamarin.Android 使用ListView绑定数据 | ·下一条:使用xamarin实现全屏播放rtmp之类的直播视频

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有