问题
How can I set the X-Axis labels using radChart as I am getting the IndexOutOfRange Exception I do not what is the reason behaind this.
public void setchart()
{
radChart.Clear();
radChart.BringToFront();
radChart.ChartTitle.TextBlock.Text = "Total Number Of Units Per Rack";
this.Controls.Add(radChart);
radChart.Dock =DockStyle.Fill;
radChart.PlotArea.Appearance.Dimensions.Height = 500;
radChart.PlotArea.XAxis.AxisLabel.Visible = true;
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "UnitProcessDetailType";
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.XAxis.Appearance.Width = 1;
radChart.PlotArea.XAxis.Appearance.Color = System.Drawing.Color.Red;
radChart.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 45;
radChart.PlotArea.YAxis.AxisLabel.Visible = true;
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "Number Of Units";
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.YAxis.Appearance.Width = 1;
radChart.PlotArea.YAxis.Appearance.Color = System.Drawing.Color.Red;
radChart.Chart.Skin = "Sunset";
Telerik.Charting.ChartSeries chartSeries = new Telerik.Charting.ChartSeries();
chartSeries.Name = "Number Of Units Per Type";
chartSeries.Type = Telerik.Charting.ChartSeriesType.Bar;
chartSeries.Appearance.BarWidthPercent = 70;
radChart.PlotArea.XAxis.AutoScale = false;
ReportGrid.Controls.Add(radChart);
List<string> xaxisLabel = new List<string>();
List<string> yaxislabel = new List<string>();
if (masterTab1.HeaderGrid.CurrentRow.Cells[0].Value.ToString() == "Sales Quotes by Quote Status")
{
DataTable table = new DataTable();
table = Adapter.SalesQuotesbyQuoteStatus(GroupList.SelectedText.ToString(), TimePeriodList.SelectedText.ToString());
int number = table.Rows.Count;
int column = table.Columns.Count;
radChart.PlotArea.XAxis.AddRange(1, number, 1);
radChart.PlotArea.YAxis.AddRange(1 , 1 , 1);
foreach (DataRow dr in table.Rows)
{
xaxisLabel.Add(dr["X_Axis"].ToString());
yaxislabel.Add(dr["Y_Axis"].ToString());
}
for (int i = 0; i < table.Rows.Count; i++)
{
chartSeries.AddItem(Convert.ToDouble(yaxislabel[i]), xaxisLabel[i]);
}
for (int xaxis = 0; xaxis < xaxisLabel.Count; xaxis++)
{
radChart.PlotArea.XAxis[xaxis].TextBlock.Text = xaxisLabel[xaxis];
}
for (int yaxis = 0; yaxis < yaxislabel.Count; yaxis++)
{
radChart.PlotArea.YAxis[yaxis].TextBlock.Text = yaxislabel[yaxis];
}
radChart.Series.Add(chartSeries);
}
}
The List also contain values as I have checked it again and again , now I do not know what is the actual problem?
回答1:
I dont know the exact problem but the reason behind the error IndexOutOfRangeException may be like this as follows,
For Example: In Main, we use a new array of length 100. This means that the array is a reference to object data of size 100 integers. You can access the array elements through array subscripts, as with array[0], array[1], through array[99].
Tip:The top index you can access equals the total length minus one. If you access an index past 99, you get an IndexOutOfRangeException.
来源:https://stackoverflow.com/questions/22349591/how-to-set-the-y-axis-label-of-radchart-values-using-liststring