没有在Winform应用程序中获得BarChart

时间:2014-09-10 09:43:09

标签: c# winforms bar-chart

我正在尝试在winform应用程序中显示来自数据库的动态条形图,但是在Argument out of Exception var p2 = series.Points[arrlocationSTD];时,它不会出现arrlocationSTD=1时出现 void LoadBarChart(string qurystring) { if (calltype.Equals("TRANSFERED")) { totalTransfered = dr["SummaryOfCalls"].ToString(); intTRANSFERED = int.Parse(totalTransfered, CultureInfo.InvariantCulture); if (i == 0) { arrlocationTransferred = i; series.Points.Add(intTRANSFERED); var p7 = series.Points[arrlocationTransferred]; p7.Color = Color.Yellow; p7.AxisLabel = "TRANSFERED"; p7.LegendText = "TRANSFERED"; p7.Label = totalTransfered; i++; } else { arrlocationTransferred = i; series.Points.Add(intTRANSFERED); var p7 = series.Points[arrlocationTransferred]; p7.Color = Color.Yellow; p7.AxisLabel = "TRANSFERED"; p7.LegendText = "TRANSFERED"; p7.Label = totalTransfered; } } } barChart.Invalidate(); pnlBar.Controls.Add(barChart); } 错误。这是我在c#中的代码..

{{1}}

请帮我解决这个问题。 提前谢谢..

1 个答案:

答案 0 :(得分:0)

您需要添加其他处理,但以下内容可能有所帮助。

我强烈建议您在开始更改颜色属性之前正确显示数据图表。

void LoadBarChart(string qurystring)
{
    String conn = Strings.ConnectionString; // You fill this in.

    Dictionary<String,int> callSummariesByTypeOfCall =
        new Dictionary<String,int>();

    MySqlConnection con = new MySqlConnection(conn);
    MySqlCommand comm = new MySqlCommand(qurystring, con);
    con.Open();
    MySqlDataReader dr = comm.ExecuteReader();

    // Get the data into a dictionary
    while (dr.Read())
    {
        String calltype = dr["TypeOfCall"].ToString();
        int summary = int.Parse(dr["Calls"].ToString(), CultureInfo.InvariantCulture);
        callSummariesByTypeOfCall[calltype] = summary;
    }

    // Do any other processing you need here

    // Bind the data onto the Series
    Series series = new Series
    {
        Name = "series2",
        IsVisibleInLegend = false,
        ChartType = SeriesChartType.Column
    };
    series.Points.DataBindXY(
        callSummariesByTypeOfCall.Keys,
        callSummariesByTypeOfCall.Values);
    barChart.Series.Add(series);
    barChart.Invalidate();

    pnlBar.Controls.Add(barChart);
}