【发布时间】:2019-03-25 12:36:58
【问题描述】:
我已修改 scrollable example 以使用 Rachel Lim 的 VMMV navigation example 并从我们的数据库中获取数据。我还将一些代码隐藏逻辑移至 VM。 LiveCharts.ChartValues 一切正常,但使用 LiveCharts.Geared.GearedValues 时,库在放大/缩小到特定点时崩溃。
数据有 6 个带时间戳的小时值。我按小时对值进行分组和求和。时间戳和值永远不会为空,也不是计算的总和。图表绘制完成后我不更新数据。
如果我从数据库中获取 1000 个值(~1000/6 个数据点),当缩小大约 5 倍数据范围时,库会崩溃。 如果我获取 10000 个值(~10000/6 个数据点),一旦用户导航到图表所在的用户控件,就会发生崩溃。 如果我获取 100000 个值,则在放大到大约相同的最小值和最大值时会发生崩溃。
但是,当使用 ChartValues 而不是 GearedValues 或仅使用几个数据点时,我可以尽可能放大并缩小到 DateTime.minvalue。
我的 view.xaml(几乎是示例之一,但使用 ICommand RangeChangedCommand):
<UserControl
[ ....]
xmlns:geared="clr-namespace:LiveCharts.Geared;assembly=LiveCharts.Geared">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"></TextBlock>
<lvc:CartesianChart Grid.Row="1"
Zoom="X"
DisableAnimations="True"
Hoverable="False">
<lvc:CartesianChart.Resources>
<Style TargetType="lvc:Separator">
<Setter Property="StrokeThickness" Value="2.5"></Setter>
<Setter Property="Stroke" Value="#E7E7E7"></Setter>
<Style.Triggers>
<Trigger Property="AxisOrientation" Value="X">
<Setter Property="IsEnabled" Value="False"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</lvc:CartesianChart.Resources>
<lvc:CartesianChart.Series>
<geared:GLineSeries StrokeThickness="0"
Values="{Binding Values}"
Fill="#2194F1"
AreaLimit="0"
PointGeometry="{x:Null}"
LineSmoothness="0"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis LabelFormatter="{Binding Formatter}" RangeChangedCommand="{Binding Axis_OnRangeChangedCommand}"
MinValue="{Binding From, Mode=TwoWay}" MaxValue="{Binding To, Mode=TwoWay}"
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}"/>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
<lvc:CartesianChart Grid.Row="2" DisableAnimations="True"
ScrollMode="X"
ScrollHorizontalFrom="{Binding From, Mode=TwoWay}"
ScrollHorizontalTo="{Binding To, Mode=TwoWay}"
ScrollBarFill="#25303030"
DataTooltip="{x:Null}"
Hoverable="False"
Margin="20 10">
<lvc:CartesianChart.Resources>
<Style TargetType="lvc:Separator">
<Setter Property="IsEnabled" Value="False"></Setter>
</Style>
</lvc:CartesianChart.Resources>
<lvc:CartesianChart.Series>
<geared:GLineSeries Values="{Binding Values}"
Fill="Silver"
StrokeThickness="0"
PointGeometry="{x:Null}"
AreaLimit="0"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis IsMerged="True"
LabelFormatter="{Binding Formatter, Mode=OneTime}"
Foreground="#98000000"
FontSize="22"
FontWeight="UltraBold"/>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis ShowLabels="False" />
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
我的虚拟机.cs
class ScrollableVM : ObservableObject, IPageViewModel
{
public string Name => "Scrollable";
private double _from;
private double _to;
private Func<double, string> _formatter;
private ICommand _axis_OnRangeChanged;
public GearedValues<DateTimePoint> Values { get; set; }
//public ChartValues<DateTimePoint> Values { get; set; }
#region setget
public double From
{
get { return _from; }
set
{
SetProperty(ref _from, value);
}
}
public double To
{
get { return _to; }
set
{
SetProperty(ref _to, value);
}
}
public Func<double, string> Formatter
{
get { return _formatter; }
set
{
SetProperty(ref _formatter, value);
}
}
#endregion
public ScrollableVM()
{
var l = new List<DateTimePoint>();
using (/***getting data from db***/)
{
var q =(/***getting data from db***/).Take(1000).ToList();
var grouped = q.GroupBy(t => new DateTime(t.Stamp.Value.Year, t.Stamp.Value.Month, t.Stamp.Value.Day, t.Stamp.Value.Hour, 0, 0));
foreach (var item in grouped)
{
l.Add(new DateTimePoint((DateTime)item.Key, (double)item.Sum(x => x.value)));
}
}
//Crashes
//quality doesn't affect crashing
Values = l.AsGearedValues().WithQuality(Quality.High);
////Works
//Values = new GearedValues<DateTimePoint>() { new DateTimePoint(DateTime.Now, 0), new DateTimePoint(DateTime.Now.AddHours(1), 1) , new DateTimePoint(DateTime.Now.AddHours(2), 2) };
////Works
//Values = l.AsChartValues();
From = Values.Min(x => x.DateTime).Ticks;
To = Values.Max(x => x.DateTime).Ticks;
Formatter = x => new DateTime((long)x).ToString("yyyy");
}
private void Axis_OnRangeChanged(RangeChangedEventArgs eventargs)
{
var currentRange = eventargs.Range;
if (currentRange < TimeSpan.TicksPerDay * 2)
{
Formatter = x => new DateTime((long)x).ToString("t");
return;
}
if (currentRange < TimeSpan.TicksPerDay * 60)
{
Formatter = x => new DateTime((long)x).ToString("dd MMM yy");
return;
}
if (currentRange < TimeSpan.TicksPerDay * 540)
{
Formatter = x => new DateTime((long)x).ToString("MMM yy");
return;
}
Formatter = x => new DateTime((long)x).ToString("yyyy");
}
public ICommand Axis_OnRangeChangedCommand
{
get
{
if (_axis_OnRangeChanged == null)
{
_axis_OnRangeChanged = new RelayCommand(a => Axis_OnRangeChanged((RangeChangedEventArgs)a));
}
return _axis_OnRangeChanged;
}
}
}
view.xaml.cs 只有带有 InitializeComponent() 的构造函数
异常详情:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Specified argument was out of the range of valid values.
Parameter name: index
Source=WindowsBase
StackTrace:
at MS.Utility.FrugalStructList`1.Insert(Int32 index, T value)
at System.Windows.Media.PathSegmentCollection.Insert(Int32 index, PathSegment value)
at LiveCharts.Wpf.Points.HorizontalBezierPointView.DrawOrMove(ChartPoint previousDrawn, ChartPoint current, Int32 index, ChartCore chart)
at LiveCharts.SeriesAlgorithms.LineAlgorithm.Update()
at LiveCharts.ChartUpdater.Update(Boolean restartsAnimations, Boolean force)
at LiveCharts.Wpf.Components.ChartUpdater.UpdaterTick(Boolean restartView, Boolean force)
at LiveCharts.Wpf.Components.ChartUpdater.OnTimerOnTick(Object sender, EventArgs args)
at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
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.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
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.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at medidata.App.Main() in ....\source\repos\medidata\medidata\obj\Debug\App.g.cs:line 51
版本:
- 实时图表 0.9.7.0
- LiveCharts.Geared 1.2.8.2
- LiveCharts.Wpf 0.9.7
我的代码/逻辑中是否有一些时髦的东西,或者这是我应该报告的错误?我没有发现其他人报告的非常相似的问题。 提前谢谢你。
【问题讨论】:
-
我认为问题出在以下行: l.Add(new DateTimePoint((DateTime)item.Key, (double)item.Sum(x => x.value)));我认为缩放可能会使总和大于图形 y 的大小并产生溢出。 }
-
VM 构造函数只在启动时调用一次。缩放不会导致值更改或从数据库重新加载。
-
我有点同意,但目前还不清楚发生了什么。当图表正在更新并导致异常时,计时器是否可能正在关闭?该错误表明图表正在更新,但图表中没有数据。通常在更改事件中,您测试对象中的行数,如果它
-
感谢您的建议。我认为如果是这种情况,它也应该导致与非齿轮 ChartValues 相同的异常,对吗?构造函数中也没有遇到断点。
-
我不知道齿轮和非齿轮之间的点数。如果是时间问题,则当您有更多积分时可能会出现问题。您仍然不想在更改数据时进行更新。所以我会在更新时停止计时器。在作为稳健设计的一部分进行绘图之前,我还会检查对象中是否有行。
标签: c# wpf livecharts