Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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):

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. The time ranges are grouped according to the index in the bottom graph:

good chart

However, when I change to try to use a text label to group them, by replacing AddXY with this:

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

the data are no longer grouped:

enter image description here

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!

like image 635
John Avatar asked Dec 17 '25 20:12

John


1 Answers

Use AxisLabel property of the DataPoint. 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;
}
like image 168
Ivar Ragnarsson Avatar answered Dec 19 '25 09:12

Ivar Ragnarsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!