MSChart:如何使用文本标签而不是索引对 RangeBar 数据进行分组?

2023-11-29

我希望在 RangeBar 图表区域 (System.Windows.Forms.DataVisualization.Charting) 上绘制数据,当我在垂直轴上有标签的文本值时,我在对数据进行分组时遇到问题。

这是相关代码:

var connectedTimeRangeSeries = theChart.Series.Add("ConnectedTimeRangeSeries");
var connectedTimeRangesChartArea = theChart.ChartAreas["PerItemConnectedChartArea"];

connectedTimeRangeSeries.ChartArea = connectedTimeRangesChartArea.Name;
connectedTimeRangeSeries.ChartType = SeriesChartType.RangeBar;
connectedTimeRangeSeries.XValueType = ChartValueType.Auto;
connectedTimeRangeSeries.YValueType = ChartValueType.Auto;

for (int i = 0; i < timeRanges.Count; i++)
{
    string theLabel = timeRanges[i].ItemLabel;

    connectedTimeRangeSeries.Points.AddXY(timeRanges[i].ItemId + 1, timeRanges[i].StartConnectionTime, timeRanges[i].StopConnectionTime);
}

timeRanges是一个包含具有这些成员类型的项目的列表(通过具有相应大写名称的公共属性访问):

private int itemId;
private string itemLabel;
private DateTime startConnectionTime;
private DateTime stopConnectionTime;

当我打电话时DataSeries.Points.AddXY()将 X 类型作为整数(回想一下,X 是范围栏上的垂直轴),它可以满足我的要求。时间范围根据底部图表中的索引进行分组:

good chart

但是,当我尝试使用文本标签对它们进行分组时,通过替换AddXY有了这个:

connectedTimeRangeSeries.Points.AddXY(theLabel, timeRanges[i].StartConnectionTime, timeRanges[i].StopConnectionTime);

数据不再分组:

enter image description here

就像每个人都有自己的垃圾箱一样,我只想将它们按标签组合起来。 (每个索引有且只有一个标签。)

有任何想法吗?谢谢!


使用 DataPoint 的 AxisLabel 属性。 一种方法是这样的:

for (int i = 0; i < timeRanges.Count; i++)
{
    string theLabel = timeRanges[i].ItemLabel;
    int pointIndex = connectedTimeRangeSeries.Points.AddXY(timeRanges[i].ItemId + 1,   timeRanges[i].StartConnectionTime, timeRanges[i].StopConnectionTime);
    connectedTimeRangeSeries.Points[pointIndex].AxisLabel = theLabel;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MSChart:如何使用文本标签而不是索引对 RangeBar 数据进行分组? 的相关文章

随机推荐