5
votes

I have created a RangeBar MS Chart control that binds with a datatable. This datatable is created based on the below list data. The problem is that x axis is showing multiple points for same item with different range.

List enter image description here
MS Chart
X axis represents the 1st column of the list and Y axis values are 3rd and 4th columns. "dt" is the datatable name

enter image description here

Code

chChart.Series["Series1"].ChartType = SeriesChartType.RangeBar;
chChart.Series["Series1"].Points.DataBind(dt.DefaultView, "Number", "Start Time,Stop Time","ToolTip=Name,Label=Name");

Tried binding as different series but still not working.

  var IETable = (dt as System.ComponentModel.IListSource).GetList();
             chChart.DataBindCrossTable(IETable, "Number", "Number", "Start Time,Stop Time","");

                foreach (Series sr in chChart.Series)
                {
                    sr.ChartType = SeriesChartType.RangeBar;
                    sr.YValueType = ChartValueType.Time;

                    sr.XValueType = ChartValueType.String;


                }

Is there a way to group x axis value for same item so that the bars are in same line?
Note - When using custom labels, only one value is shown for each x axis label.

2
You could try pulling all the data first into a more managable form and then adding it back into the chart, chart.ChartAreas[0].AxisY.CustomLabels.Add(...) for labels and the same for x axis. You would need to post a bit more with what you are working forVajura
@Vajura I have edited my question for more clarity.Thomas Mathew
You say that X axis is the first column(zzz-2 etc) but on the graph on the picture its on the Y axis (vertical axis = y axis). And you dont do anything with the second column?Vajura
@Vajura : In case of "RangeBar" chartype, x axis is the vertical one and y axis is the horizontal one. No, i am not using the second column in the list.Thomas Mathew
To put bars on a same row, all the values must be in separate seriesJaska

2 Answers

1
votes

enter image description here

Add 1 Series into the chart. Then for the Series, make sure YValuesPerPoint = 2. Add new DataPoints so that the XValue represents the line (in the example for line ABC1, XValue = 1 and line ABC2, XValue = 2. For the DataPoint.AxisLabel you can set the label of that axis (in this case 'ABC1' and 'ABC2'). Then for the YValues, you specify values comma separated, for example 1,2 or 7,20.

Example:

        chChart.Series["Series1"].Points.Add(new DataPoint() { AxisLabel = "ABC1", XValue = 1, YValues = new double[] { 2,5 } });
        chChart.Series["Series1"].Points.Add(new DataPoint() { AxisLabel = "ABC1", XValue = 1, YValues = new double[] { 6,7 } });
        chChart.Series["Series1"].Points.Add(new DataPoint() { AxisLabel = "ABC1", XValue = 1, YValues = new double[] { 9,10 } });

        chChart.Series["Series1"].Points.Add(new DataPoint() { AxisLabel = "ABC2", XValue = 2, YValues = new double[] { 3,6 } });
        chChart.Series["Series1"].Points.Add(new DataPoint() { AxisLabel = "ABC2", XValue = 2, YValues = new double[] { 7,8 } });

So the key is that the XValue must be same (double?) for all the bars on same line.

0
votes

Manipulating datapoints individually for each series did the trick. Based on the index of the custom label, i modified the Xvalue of the datapoint. "vn" is a string array that has custom label names.

var IETable = (dt as System.ComponentModel.IListSource).GetList();
chChart.DataBindCrossTable(IETable, "Number","","Start Time,StopTime","");

                  foreach (Series sr in chChart.Series)
                  {

                      sr.ChartType = SeriesChartType.RangeBar;
                      sr.XValueType = ChartValueType.String;
                      foreach (DataPoint p in sr.Points)
                      {
                          int dpIndex=0;
                          // foreach unique x axis value, increment dpIndex
                          //ToDoCode

                         //aligning the datapoint's X axis value in the middle

                          p.XValue = dpIndex + 0.5;

                      }

                  }