Avalonia学习(十五)-OxyPlot

发布时间:2023年12月31日

今天开始继续Avalonia练习。展示一些样例,尤其是第三方库的使用。

本节:OxyPlot

1.引入OxyPlot.Avalonia

2.项目引入

在Main方法里增加OxyPlotModule.EnsureLoaded()方法调用。

public static void Main(string[] args)
{
    OxyPlotModule.EnsureLoaded();
    AppBuilder.Configure<App>()
        .UsePlatformDetect()
        .StartWithClassicDesktopLifetime(args);
}

在App.xaml文件中增加样式引用。

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="OxyPlotAvalonia.App"
             xmlns:local="using:OxyPlotAvalonia"
             RequestedThemeVariant="Default">
             <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

    <Application.DataTemplates>
        <local:ViewLocator/>
    </Application.DataTemplates>
  
    <Application.Styles>
        <FluentTheme />
      <StyleInclude Source="avares://OxyPlot.Avalonia/Themes/Default.axaml"/>
     
    </Application.Styles>
</Application>

前台代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:OxyPlotAvalonia.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:oxy="clr-namespace:OxyPlot.Avalonia;assembly=OxyPlot.Avalonia" 
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="OxyPlotAvalonia.Views.MainWindow"
       
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="OxyPlotAvalonia">

    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>
      <oxy:PlotView Model="{Binding Model}" />
    <!--<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>-->

</Window>

后台代码

using OxyPlot;
using OxyPlot.Series;
using System.Collections.Generic;

namespace OxyPlotAvalonia.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
#pragma warning disable CA1822 // Mark members as static
        public string Greeting => "Welcome to Avalonia!";
#pragma warning restore CA1822 // Mark members as static
        public PlotModel Model { get; private set; }
        public MainWindowViewModel()
        {

         //   OxyPlot.Avalonia.PlotView plotView=new OxyPlot.Avalonia.PlotView();
         //   plotView.Model = Model;
            // Create the plot model
            var tmp = new PlotModel { Title = "Simple example", Subtitle = "using OxyPlot" };

           // OxyPlot.Series.Series series=new  
            // Create two line series (markers are hidden by default)
          
            var series1 = new LineSeries { Title = "Series 1", MarkerType = MarkerType.Circle };
            List<DataPoint> points = new List<OxyPlot.DataPoint>();
            points.Add(new DataPoint(0, 0));
            points.Add(new DataPoint(10, 18));
            points.Add(new DataPoint(20, 12));
            points.Add(new DataPoint(30, 8));
            points.Add(new DataPoint(40, 15));
            series1.ItemsSource = points;

            var series2 = new LineSeries { Title = "Series 2", MarkerType = MarkerType.Square };
            List<DataPoint> points1 = new List<OxyPlot.DataPoint>();
            points.Add(new DataPoint(0, 0));
            points.Add(new DataPoint(10, 18));
            points.Add(new DataPoint(20, 12));
            points.Add(new DataPoint(30, 8));
            points.Add(new DataPoint(40, 15));
            points1.Add(new DataPoint(0, 4));
            points1.Add(new DataPoint(10, 12));
            points1.Add(new DataPoint(20, 16));
            points1.Add(new DataPoint(30, 25));
            points1.Add(new DataPoint(40, 5));
         
            // Add the series to the plot model
            // tmp.Series.Add(series1);
            // tmp.Series.Add(series2);
            tmp.Series.Add(series1);
            this.Model = tmp;
        }
    }
}

运行效果

经过测试,最新版Avalonia11.0.6会报错,Oxyplot.Avalonia没有对应更新,你要直接用最新Avalonia,可以引入Oxyplot.AvaloniaCore包测试。

文章来源:https://blog.csdn.net/jinyuttt/article/details/135317302
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。