2017-06-22 55 views
2

我正在使用WPF中的OxyPlot并且有一些问题。我试图创建一个应用程序,并希望使用OxyPlot创建图表。一切正常,但情节/数据不会显示出来。我似乎无法弄清楚为什么。在WPF中创建一个OxyPlot

下面是我的一些XAML代码:

<UserControl x:Class="myNameSpace.MainPanel" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:app="clr-namespace:myNameSpace" 
      xmlns:oxy="http://oxyplot.org/wpf" 
      mc:Ignorable="d" 
      d:DesignHeight="1200" 
      d:DesignWidth="1920"> 
    <UserControl.DataContext> 
     <local:MainViewModel/> 
    </UserControl.DataContext> 
    .... 
    <oxy:Plot Title="{Binding Title}" Margin="760,689,606,228" Width="504" Height="283"> 
      <oxy:Plot.Series> 
       <oxy:LineSeries ItemsSource="{Binding Points}"/> 
      </oxy:Plot.Series> 
     </oxy:Plot> 

和我MainViewPanel类看起来是这样的:

public class MainViewModel 
     { 
      public MainViewModel() 
      { 
       this.Title = "Example 2"; 
       this.Points = new List<DataPoint> 
           { 
            new DataPoint(0, 4), 
            new DataPoint(10, 13), 
            new DataPoint(20, 15), 
            new DataPoint(30, 16), 
            new DataPoint(40, 12), 
            new DataPoint(50, 12) 
           }; 
      } 

      public string Title { get; private set; } 

      public IList<DataPoint> Points { get; private set; } 
    } 

这是图表的外观,当我运行我的代码就像

Screen shot

+0

您提供的代码对我来说工作正常。这是一个外部的机会,但我认为VM中的Points列表是OxyPlot.DataPoint的列表,而不是其他地方的DataPoint列表。 –

+0

忽略我以前的评论,再次看看你的照片,它看起来像标题不具约束力。 –

+2

您确定您的xaml中引用的MainViewModel是正确的类型。我没有看到定义的本地命名空间前缀。如果MainViewModel位于AnnaEmilie命名空间中,请将本地:MainViewModel更改为app:MainViewModel –

回答

1

我没有看到定义的本地命名空间前缀。如果MainViewModel位于AnnaEmilie命名空间中,请将local:MainViewModel更改为app:MainViewModel

0

删除边距,因为它们会遮盖图形可见。

<oxy:Plot Title="{Binding Title}" Width="504" Height="283"> 
    <oxy:Plot.Series> 
     <oxy:LineSeries ItemsSource="{Binding Points}"/> 
    </oxy:Plot.Series> 
</oxy:Plot> 
+0

不确定这个回答Anna的问题。她正在看图表,它只是没有任何数据点。我认为大幅度利润正在被用于绝对定位。 –

+0

你是对的@JanisS,我现在只能看到图中的一半,我现在使用边距(它以前工作过)。但是,如果我删除它,我不能有绝对的定位,图形不会结束在我想要的位置。 – Anna

+0

@Anna尝试使用相对定位。最简单的方法之一是“叠加”你的元素,然后使用边距和填充位置,如你所期望的或使用网格(https://www.codeproject.com/Articles/140613/WPF-Tutorial-Layout -Panels的容器,布局反式)。 –