简体   繁体   English

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

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

I'm trying to do something that I believe should be easy. 我正在尝试做一些我认为应该很容易的事情。 I want to show the values 1 through 30, evenly spaced, because I do a run of data that represents 30 minutes of data. 我想显示均匀分布的1到30值,因为我进行了代表30分钟数据的数据运行。 Sometimes there are 500 data points, but sometimes there are thousands (I can easily determine the number of points though). 有时有500个数据点,但有时却有数千个(尽管我可以轻松确定点数)。

Basically, my code for the Xaxis looks like this, but it is not doing what I want. 基本上,我在Xaxis上的代码看起来像这样,但是它并没有满足我的要求。 Below the code is a screenshot of what is happening, and below that is a screenshot of what I want. 代码下面是正在发生的情况的屏幕截图,下面是我想要的屏幕截图。

        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
            }
        });

Current Bug chart (trying to put a minute label for every data point) 当前的错误图表(试图为每个数据点贴上分钟标签) 错误表 Need only 30 minute labels (maybe my algorithm to calculate step needs adjusting, currently I divide total number of data points by 30) 只需要30分钟的标签(也许我的算法计算步骤需要调整,目前我将数据点总数除以30) 期望 I looked at these two posts, but this is a different issue LiveChart doesn't show all Labels on x-axis WPF 我看了这两篇文章,但这是一个不同的问题, LiveChart并未在x轴WPF上显示所有标签

Force all axis labels to show 强制所有轴标签显示

The issues, is that live chart produces for each point labels, and the numbers of labels need to be the number of the points. 问题在于,实时图表会为每个点生成标签,标签的数量必须是点的数量。 There are two solutions for the problem. 有两种解决方案。

First You change the ChartValues<double> to ChartValues<ObservablePoint> 首先,将ChartValues<double> to ChartValues<ObservablePoint>更改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
        }

    }
};

See that in the obserablepoint you specify X and Y. Point could have different distance between each other (irregular intervals) 请注意,在可观察点中,您指定了X和Y。点之间的距离可能不同(不规则间隔)

The second solution could be that you got label array define in following way, so you make labels array as big as your count of points but you declare it that you got only 30 items, rest of them are empty. 第二种解决方案是按以下方式定义标签数组,因此使标签数组与点数一样大,但声明只有30个项目,其余项目为空。

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

This was the solution to the problem, but Kaspar's answer was educational and helped me arrive at this solution. 这是解决问题的方法,但是Kaspar的回答很有教育意义,并帮助我找到了解决方案。 Therefore, I accepted his answer since he deserves the credit. 因此,我接受了他的回答,因为他应得的荣誉。

The way I solved this problem was to do the following: While creating the charts I would bind to the listOfLableValues; 我解决此问题的方法是执行以下操作:创建图表时,我将绑定到listOfLableValues。 after creating the charts I would edit add to this list as shown in the bottom loop of my code: 创建图表后,我将编辑添加到此列表,如代码底部循环所示:

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("");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM