我在试着做一些我认为应该很容易的事情。我希望显示从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
}
});当前Bug图表(尝试为每个数据点添加分钟标签)

只需要30分钟的标签(可能我的算法计算步长需要调整,目前我将数据点总数除以30)

我看了这两个帖子,但这是另一个问题LiveChart doesn't show all Labels on x-axis WPF
发布于 2019-01-26 01:43:16
问题是,实时图表为每个点生成标签,标签的数量需要是点的数量。这个问题有两种解决方案。
首先,您需要更改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" };发布于 2019-01-26 03:43:49
这是问题的解决方案,但Kaspar的答案很有教育意义,并帮助我得出了这个解决方案。因此,我接受了他的回答,因为他值得称赞。
我解决这个问题的方法如下:在创建图表时,我将绑定到listOfLableValues;在创建图表后,我将编辑add to this list,如代码的底部循环所示:
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("");
}https://stackoverflow.com/questions/54369202
复制相似问题