首页 文章

Wpf Toolkit折线图不显示数据

提问于
浏览
0

我正在尝试使用wpf工具包向我的应用程序添加折线图 .

图表本身显示没有问题,但不显示数据 . 如果我设置一个断点来查看数据,我可以清楚地看到,Observable Collection中填充了所需的数据 .

这是我的XAML中的图表:

<charting:Chart x:Name="Gehtdas" Grid.Row="1" Width="Auto" Height="Auto">
        <charting:Chart.Style>
            <Style>
                <Setter Property="charting:Chart.Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger
                        Binding="{Binding ElementName=TypeSelection, Path=SelectedItem}" Value="Line_Chart">
                        <Setter Property="charting:Chart.Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </charting:Chart.Style>
        <charting:Chart.Axes>
            <charting:LinearAxis Title="Tyres" Orientation="Y" Interval="100" Visibility="Visible" Maximum="1000" Minimum="0"/>
            <charting:DateTimeAxis x:Name="TimeAxis" Title="Time" Orientation="X"/>                
        </charting:Chart.Axes>
        <charting:LineSeries DataContext="{Binding}" Title="Tyres per Interval" DependentValueBinding="{Binding Path=Value}"
                             IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding LineChartValues}" 
                             IsSelectionEnabled="True" AnimationSequence="FirstToLast" />
    </charting:Chart>

LineChartValues是一个像这样的KeyValuePair Observable Collection .

private ObservableCollection<KeyValuePair<DateTime?, decimal?>> _lineChartValues;
    public ObservableCollection<KeyValuePair<DateTime?, decimal?>> LineChartValues
    {
        get
        {
            return _lineChartValues;
        }
        set
        {
            _lineChartValues = value;
        }
    }

Viewmodel通过ISite绑定到xaml .

谁能告诉我我做错了什么?我现在非常绝望,非常感谢任何帮助 .

编辑:如果我删除DataContext = 部分,我在填充Observable Collection时会得到NullReferenceException .

EDIT2:窗口加载的值的数量:Xaml:

<charting:Chart x:Name="Gehtdas" Grid.Row="1" Width="Auto" Height="Auto">
        <!--<charting:Chart.Style>
            <Style>
                <Setter Property="charting:Chart.Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger
                        Binding="{Binding ElementName=TypeSelection, Path=SelectedItem}" Value="Line_Chart">
                        <Setter Property="charting:Chart.Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </charting:Chart.Style>-->
        <charting:Chart.Axes>
            <charting:LinearAxis Title="Tyres" Orientation="Y" Interval="100" Visibility="Visible" Maximum="1000" Minimum="0"/>
            <charting:DateTimeAxis x:Name="TimeAxis" Title="Time" Orientation="X"/>                
        </charting:Chart.Axes>
        <charting:LineSeries Title="Tyres per Interval" DependentValueBinding="{Binding Path=Value}"
                             IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding}" 
                             IsSelectionEnabled="True"/>
    </charting:Chart>

代码隐藏:

public ObservableCollection<KeyValuePair<DateTime, int?>> valueList = new ObservableCollection<KeyValuePair<DateTime, int?>>();
void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        // workaround for Bug in DateTimeAxis which causes the Axis to divide the Default IntervalType of Year into given IntervalType and Interval. 
        //Setting the IntervalType and Interval this way, prevents it from calculating the interval for a whole year.
        this.TimeAxis.Minimum = new DateTime(2016, 02, 10, 0, 0, 0);
        this.TimeAxis.Maximum = new DateTime(2016, 02, 10, 16, 0, 0);
        //this.TimeAxis.Minimum = DateTime.Now.Date;
        //this.TimeAxis.Maximum = DateTime.Now;
        this.TimeAxis.IntervalType = System.Windows.Controls.DataVisualization.Charting.DateTimeIntervalType.Hours;
        this.TimeAxis.Interval = 1;

        valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 10, 0, 0), 60));
        valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 11, 0, 0), 40));
        valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 12, 0, 0), 50));
        valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 13, 0, 0), 30));
        valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 14, 0, 0), 40));
        Gehtdas.DataContext = valueList;
    }

编辑3:Exception Stacktrace:

at 

System.Windows.Controls.DataVisualization.Charting.LineAreaBaseSeries`1.UpdateDataPoint(DataPoint dataPoint)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointActualIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointActualIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.set_ActualIndependentValue(Object value)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.LineAreaBaseSeries`1.OnDataPointIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.TreeWalkHelper.InvalidateTreeDependentProperty(TreeChangeInfo info, DependencyObject d, FrameworkObject& fo, DependencyProperty dp, FrameworkPropertyMetadata fMetadata, Style selfStyle, Style selfThemeStyle, ChildRecord& childRecord, Boolean isChildRecordValid, Boolean hasStyleChanged, Boolean isSelfInheritanceParent)
   at System.Windows.TreeWalkHelper.InvalidateTreeDependentProperties(TreeChangeInfo info, FrameworkElement fe, FrameworkContentElement fce, Style selfStyle, Style selfThemeStyle, ChildRecord& childRecord, Boolean isChildRecordValid, Boolean hasStyleChanged, Boolean isSelfInheritanceParent)
   at System.Windows.FrameworkElement.InvalidateTreeDependentProperties(TreeChangeInfo parentTreeState, Boolean isSelfInheritanceParent)
   at System.Windows.FrameworkElement.OnAncestorChangedInternal(TreeChangeInfo parentTreeState)
   at System.Windows.TreeWalkHelper.InvalidateOnTreeChange(FrameworkElement fe, FrameworkContentElement fce, DependencyObject parent, Boolean isAddOperation)
   at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
   at System.Windows.FrameworkElement.AddLogicalChild(Object child)
   at System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement element)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.AddDataPoint(DataPoint dataPoint)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.AddObject(Object dataContext)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.LoadDataPoints(IEnumerable newItems, IEnumerable oldItems)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.Refresh()
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.OnDataContextChanged(DependencyObject contextElement)
   at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
   at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.TreeWalkHelper.OnInheritablePropertyChanged(DependencyObject d, InheritablePropertyChangeInfo info, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
   at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
   at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
   at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
   at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
   at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
   at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
   at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
   at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
   at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
   at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
   at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
   at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
   at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
   at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
   at System.Windows.DescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode)
   at System.Windows.TreeWalkHelper.InvalidateOnInheritablePropertyChange(FrameworkElement fe, FrameworkContentElement fce, InheritablePropertyChangeInfo info, Boolean skipStartNode)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Mfc.Statistic.StoragePerformance.MainWindow_Loaded(Object sender, RoutedEventArgs e) in d:\Source_Wista\2014-103141 GDC Bridgestone Burgos Materialfluss\MFC-Solution\Mfc\Statistic\StoragePerformance.xaml.cs:line 91
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at Wista.App.Main() in d:\Source_Wista\Wista\Wista\obj\Release\App.g.cs:line 0

EDIT4:

所以我在一个新的空解决方案中尝试了简单的静态数据方法,并在那里工作 . 我想问题在于工具包和其他一些框架之间的一些冲突 .

1 回答

  • 0

    我想问题是图表不知道如何显示你的Nullable值(DateTime?,decimal?) . 您可能需要一个转换器来从Nullable装饰器中提取值 .

    这是我的例子:MainWindow.xaml

    <Window x:Class="WpfToolkitChart.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="1031" Width="855" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="0,-28,0,28">
            <Grid Height="921">
    
                <chartingToolkit:Chart  Name="lineChart" Title="Line Series Demo" VerticalAlignment="Top" Margin="33,611,440,0" Height="254">
                    <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"/>
                </chartingToolkit:Chart>
            </Grid>
        </ScrollViewer>
    
    </Window>
    

    MainWindow.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfToolkitChart
    {
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          showColumnChart();
        }
    
        public ObservableCollection<KeyValuePair<string, int?>> valueList = new ObservableCollection<KeyValuePair<string, int?>>();
    
        private void showColumnChart()
        {
    
          valueList.Add(new KeyValuePair<string, int?>("Developer",60));
          valueList.Add(new KeyValuePair<string, int?>("Misc", 20));
          valueList.Add(new KeyValuePair<string, int?>("Tester", 50));
          valueList.Add(new KeyValuePair<string, int?>("QA", 30));
          valueList.Add(new KeyValuePair<string, int?>("Project Manager", 40));
    
          //Setting data for line chart
          lineChart.DataContext = valueList;
        }
    
      }
    }
    

相关问题