Xamarin圖表開發基礎教程(4)OxyPlot框架

Xamarin圖表開發基礎教程(4)OxyPlot框架

XamaminAndroid中繪製線圖OxyPlotAndroidDemo

【示例1-1:OxyPlotAndroidDemo】下面實現線圖的繪製。具體的操做步驟以下:android

(1)打開Xamarin.Android項目。app

(2)將OxyPlot.Xamarin.Android組件添加到項目中的引入中。框架

(3)打開activity_main.axml文件,使用PlotView進行佈局。代碼以下:ide

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

  <OxyPlot.Xamarin.Android.PlotView

      android:id="@+id/plot_view"

      android:layout_width="match_parent"

      android:layout_height="match_parent"/>

</RelativeLayout>

(4)打開MainActivity.cs文件,在此文件中實現剩餘的步驟,即繪製圖表並設置顯示模式。代碼以下:佈局

using Android.App;

using Android.OS;

using Android.Support.V7.App;

using Android.Runtime;

using Android.Widget;

using OxyPlot.Xamarin.Android;

using OxyPlot;

using OxyPlot.Axes;

using OxyPlot.Series;

namespace OxyPlotAndroidDemo

{

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

    public class MainActivity : AppCompatActivity

    {

        protected override void OnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);

           

            // Set our view from the "main" layout resource

            SetContentView(Resource.Layout.activity_main);

            PlotView view = FindViewById<PlotView>(Resource.Id.plot_view);

            view.Model = CreatePlotModel();                                                       //設置顯示模式

        }

        //繪製圖表

        private PlotModel CreatePlotModel()

        {

            //建立圖表模式

            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

            {

                Title= "Data",

                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;

        }

    }

}

運行程序,顯示的圖表如圖1.1所示。spa

圖1.1  Xamarin.Android平臺的線圖效果3d

相關文章
相關標籤/搜索