C# 当缩放到特定位置时,Livecharts将抛出未处理的ArgumentOutOfRangeException

C# 当缩放到特定位置时,Livecharts将抛出未处理的ArgumentOutOfRangeException,c#,wpf,livecharts,C#,Wpf,Livecharts,我已经修改了使用Rachel Lim的VMMV,并从我们的数据库中获取数据。我还将一些代码隐藏逻辑移到了VM中。使用LiveCharts.ChartValues时一切正常,但使用LiveCharts.Geared.GearedValues时,库在放大/缩小到特定点时崩溃 数据有6个带有时间戳的小时值。我按小时对值进行分组和求和。时间戳和值从不为空,也不为计算的和。绘制图表后,我不会更新数据 如果我从数据库中获取1000个值(~1000/6个数据点),当缩小大约5倍的数据范围时,库就会崩溃。 如果

我已经修改了使用Rachel Lim的VMMV,并从我们的数据库中获取数据。我还将一些代码隐藏逻辑移到了VM中。使用LiveCharts.ChartValues时一切正常,但使用LiveCharts.Geared.GearedValues时,库在放大/缩小到特定点时崩溃

数据有6个带有时间戳的小时值。我按小时对值进行分组和求和。时间戳和值从不为空,也不为计算的和。绘制图表后,我不会更新数据

如果我从数据库中获取1000个值(~1000/6个数据点),当缩小大约5倍的数据范围时,库就会崩溃。 如果我获取10000个值(~10000/6个数据点),那么一旦用户导航到图表所在的usercontrol,就会发生崩溃。 如果我获取100000个值,当放大到大约相同的minvalue和maxvalue时会发生崩溃

但是,当使用ChartValues而不是GearedValues或仅使用几个数据点时,我可以尽可能地放大并缩小到DateTime.minvalue

My view.xaml(与示例差不多,但使用ICommand RangeChangedCommand):

版本:

  • 动态图表0.9.7.0
  • 动态图表.齿轮传动1.2.8.2
  • LiveCharts.wpf0.9.7
我的代码/逻辑中是否有一些古怪的东西,或者这是我应该报告的错误?我没有发现其他人报告过类似的问题。
提前谢谢。

这花了一天的时间,但我想我发现了我的实现与官方示例相比有什么不同列表顺序。

如果我在调用AsGearedValues()之前按x轴上的属性对原始数据进行排序,则不会发生崩溃。这在免费版本中不是问题。我猜这与虚拟化/优化有关,而AsGearedValues不够聪明,无法对列表本身进行排序以供将来使用。文件中也没有提到这一点。 我将在github上就此提出一个问题

简单演示:

MainWindow.xaml.cs

using LiveCharts.Defaults;
using LiveCharts.Geared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace bughunt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public IGearedValues Values { get; set; }

        public MainWindow()
        {
            const int count = 1000;

            //Not sorting causes crashes when zooming deep in and back
            const bool SortBeforePassing = false;

            var r = new Random();

            var datepointlist = new List<DateTimePoint>();

            for (int i = 0; i < count; i++)
            {
                datepointlist.Add(new DateTimePoint(DateTime.Now.AddHours(-i), (double)r.Next(1, 150)));
            }
            if (SortBeforePassing)
            {
                Values = datepointlist.OrderBy(x => x.DateTime).AsGearedValues().WithQuality(Quality.High);
            }
            else
            {
                Values = datepointlist.AsGearedValues().WithQuality(Quality.High);
            }

            DataContext = this;
            InitializeComponent();
        }
    }
}
使用LiveCharts.Defaults;
使用动态图表;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
命名空间错误查找
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共IGearedValues值{get;set;}
公共主窗口()
{
常数整数计数=1000;
//如果不进行排序,则在放大和缩小时会导致崩溃
const bool SortBeforePassing=false;
var r=新的随机变量();
var datepointlist=新列表();
for(int i=0;ix.DateTime).AsGearedValues().WithQuality(Quality.High);
}
其他的
{
Values=datepointlist.AsGearedValues(),具有质量(Quality.High);
}
DataContext=this;
初始化组件();
}
}
}
MainWindow.xaml

<Window x:Class="bughunt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:bughunt"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns:geared="clr-namespace:LiveCharts.Geared;assembly=LiveCharts.Geared"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <lvc:CartesianChart Zoom="X" 
                            DisableAnimations="True"
                            Hoverable="False">

            <lvc:CartesianChart.Series>
                <geared:GLineSeries
                                    Values="{Binding Values}"
                                    PointGeometry="{x:Null}"/>
            </lvc:CartesianChart.Series>
        </lvc:CartesianChart>
    </Grid>
</Window>

我遇到了完全相同的问题:我在x轴上调整了日期时间值,缩放时抛出了
ArgumentOutOfRangeException
。问题是,当放大太多时,x轴值格式化程序接收到一个负值,这对于DateTime当然是不正确的(负刻度)。有一个
PreviewRangeChanged
事件,您可以根据需要绑定并取消缩放,但我的解决方法不同:

// what caused the exception
XFormatter = val => new DateTime((long)val).ToString("HH:mm:ss");
// solve it by testing the value and return 0 if negative
XFormatter = val => val < 0.0 ? (new DateTime((long)0.0).ToString("HH:mm:ss")) : (new DateTime((long)val).ToString("HH:mm:ss"));
//是什么导致了异常
XFormatter=val=>newdatetime((long)val).ToString(“HH:mm:ss”);
//通过测试该值进行求解,如果为负,则返回0
XFormatter=val=>val<0.0?新的DateTime((long)0.0.ToString(“HH:mm:ss”):(新的DateTime((long)val.ToString(“HH:mm:ss”));

因此,如果您只是防止值格式化程序获得负值,那么您就完全可以解决这个问题。这可能不是最聪明的解决方案,但对于我的情况来说,它足够简单和好。

我认为问题在于以下行:l.Add(new DateTimePoint((DateTime)item.Key,(double)item.Sum(x=>x.value));我认为缩放可能使总和大于图形y的大小,并导致溢出。}VM构造函数在启动时只调用一次。缩放不会导致值更改或从数据库中重新加载。我有点同意,但发生的情况并不明显。是否有可能在图表更新时计时器关闭并导致异常?此错误表示正在更新图表,但图表中没有数据。通常在更改事件中,您会测试对象中的行数,如果行数足够,请感谢您的建议。我认为,如果是这样的话,它也应该导致非齿轮传动图表值的相同异常,对吗?在构造函数中也没有断点。我不知道齿轮和非齿轮之间的点数。如果是时间问题,当你有更多的分数时,问题可能会发生。但是,您不希望在更改数据时进行更新。所以我会在更新时停止计时器。在作为稳健设计的一部分进行打印之前,我还会检查对象中是否有行。
using LiveCharts.Defaults;
using LiveCharts.Geared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace bughunt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public IGearedValues Values { get; set; }

        public MainWindow()
        {
            const int count = 1000;

            //Not sorting causes crashes when zooming deep in and back
            const bool SortBeforePassing = false;

            var r = new Random();

            var datepointlist = new List<DateTimePoint>();

            for (int i = 0; i < count; i++)
            {
                datepointlist.Add(new DateTimePoint(DateTime.Now.AddHours(-i), (double)r.Next(1, 150)));
            }
            if (SortBeforePassing)
            {
                Values = datepointlist.OrderBy(x => x.DateTime).AsGearedValues().WithQuality(Quality.High);
            }
            else
            {
                Values = datepointlist.AsGearedValues().WithQuality(Quality.High);
            }

            DataContext = this;
            InitializeComponent();
        }
    }
}
<Window x:Class="bughunt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:bughunt"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns:geared="clr-namespace:LiveCharts.Geared;assembly=LiveCharts.Geared"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <lvc:CartesianChart Zoom="X" 
                            DisableAnimations="True"
                            Hoverable="False">

            <lvc:CartesianChart.Series>
                <geared:GLineSeries
                                    Values="{Binding Values}"
                                    PointGeometry="{x:Null}"/>
            </lvc:CartesianChart.Series>
        </lvc:CartesianChart>
    </Grid>
</Window>
// what caused the exception
XFormatter = val => new DateTime((long)val).ToString("HH:mm:ss");
// solve it by testing the value and return 0 if negative
XFormatter = val => val < 0.0 ? (new DateTime((long)0.0).ToString("HH:mm:ss")) : (new DateTime((long)val).ToString("HH:mm:ss"));