在Winform应用程序中未获取条形图

本文关键字:获取 条形图 Winform 应用程序 | 更新日期: 2023-09-27 18:21:24

我试图在winform应用程序中显示数据库中的动态条形图,但它没有出现,并且在arrlocationSTD=1时在var p2 = series.Points[arrlocationSTD];处给我Argument out of Exception错误。这是我在c#中的代码。。

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

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

在Winform应用程序中未获取条形图

您需要添加额外的处理,但以下内容可能会有所帮助。

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

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