实时图表 WPF 需要始终在 X 轴上显示硬编码步骤

Live Charts WPF need to always show hardcoded steps in X axis

我正在尝试做一些我认为应该很容易的事情。 我想显示 1 到 30 之间的值,间隔均匀,因为我做的 运行 数据代表 30 分钟的数据。有时有 500 个数据点,但有时有数千个(不过我可以很容易地确定点数)。

基本上,我的 Xaxis 代码看起来像这样,但它没有按照我的要求进行。代码下面是正在发生的截图,下面是我想要的截图。

        cartChart.AxisX.Add(new Axis
        {
            // TODO fix this to acutally show real time
            Title = "Minutes",
            //Labels= { "1", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30" },
            Separator = new LiveCharts.Wpf.Separator
            {
                Step = listOfChartValues[i].Count / 30 > 0 ? listOfChartValues[i].Count / 30 : 1, // this is making the assumtion that data arrivals follow uniform distribution
                IsEnabled = false
            }
        });

当前错误图表(尝试为每个数据点贴上分钟标签) 只需要 30 分钟的标签(也许我计算步长的算法需要调整,目前我将数据点总数除以 30) 我看了这两个帖子,但这是一个不同的问题

Force all axis labels to show

问题在于,实时图表会为每个点生成标签,并且标签的数量需要是点的数量。有两种解决方法。

首先你改变ChartValues<double> to ChartValues<ObservablePoint>

chart.Series = new LiveCharts.SeriesCollection()
{
    new LineSeries()
    {
        Title = "Some series",
        Values = new ChartValues<ObservablePoint>
        {
            new ObservablePoint(1,5),
            new ObservablePoint(1.5,7.6),
            new ObservablePoint(2,21),
            new ObservablePoint(5,25),
            new ObservablePoint(10,30),
            new ObservablePoint(17,30),
            new ObservablePoint(19.6,30),
            new ObservablePoint(30,40),

        }
    }
};
chart.AxisX = new LiveCharts.Wpf.AxesCollection()
{
    new LiveCharts.Wpf.Axis()
    {
        Title= "Minutes",
        Separator = new LiveCharts.Wpf.Separator()
        {
            Step = 1.0,
            IsEnabled = false
        }

    }
};

看到在您指定 X 和 Y 的可观察点。点之间可以有不同的距离(不规则间隔)

第二种解决方案可能是您按照以下方式定义了标签数组,因此您使标签数组与您的点数一样大,但您声明它只有 30 个项目,其余的都是空的。

chart.AxisX.First().Labels = new List<string>() { "1", "","2","","","3",.... "30" };

这是问题的解决方案,但 Kaspar 的回答很有教育意义,帮助我找到了这个解决方案。因此,我接受了他的回答,因为他值得信任。

我解决这个问题的方法是执行以下操作:在创建图表时,我将绑定到 listOfLableValues;创建图表后,我将编辑添加到此列表,如代码底部循环所示:

private List<string> listOfLableValues = new List<string>();

//... other WPF code
// loop through all charts and to create them
       SeriesCollection = new SeriesCollection
       {
            new StepLineSeries
            {
                Values = listOfChartValues[i],
                PointGeometry = null,
                // Fill = Brushes.Transparent
             },
       };
//... other Livecharts code

        int lableCount = 1;
        for (int i = 0; i < listOfChartValues.Count; i++)
        {
            listOfChartValues[i].AddRange(de.ListOfListPlottedAmpData[i]);


            if (0 == i % (listOfChartValues[i].Count / 30))
                listOfLableValues.Add(lableCount++.ToString());
            else
                listOfLableValues.Add("");
        }