简体   繁体   English

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

[英]MSChart: How to group RangeBar data with text labels instead of indexes?

I'm looking to plot data on a RangeBar chart area (System.Windows.Forms.DataVisualization.Charting) and I'm having issues grouping the data when I have text values for the labels on the vertical axis. 我想在RangeBar图表区域上绘制数据(System.Windows.Forms.DataVisualization.Charting),当我在垂直轴上有标签的文本值时,我遇到了分组数据的问题。

Here's the pertinent code: 这是相关的代码:

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 is a list with items having these member types (accessed through public properties with corresponding capitalized names): timeRanges是一个包含具有这些成员类型的项目的列表(通过具有相应大写名称的公共属性访问):

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

When I call DataSeries.Points.AddXY() with the X type as an integer (recall X is the vertical axis on the range bar) it does what I want. 当我将X类型作为整数调用DataSeries.Points.AddXY()时(调用X是范围栏上的垂直轴),它可以完成我想要的操作。 The time ranges are grouped according to the index in the bottom graph: 时间范围根据底部图表中的索引进行分组:

好图表

However, when I change to try to use a text label to group them, by replacing AddXY with this: 但是,当我更改为尝试使用文本标签对它们进行分组时,通过将AddXY替换为:

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

the data are no longer grouped: 数据不再分组:

在此输入图像描述

It's like each one gets its own bin, and I just want them to be combined by label. 这就像每个人都有自己的垃圾箱,我只想让它们按标签组合。 (Each index has one and only one label.) (每个索引都有一个且只有一个标签。)

Any ideas? 有任何想法吗? Thanks! 谢谢!

Use AxisLabel property of the DataPoint. 使用DataPoint的AxisLabel属性。 One way to do it would be something like this: 一种方法是这样的:

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

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

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