简体   繁体   English

从Windows窗体C#中的列表列表绘制图表

[英]Drawing a chart from a List of list in windows forms C#

I am trying to draw a stacked graph from my database. 我正在尝试从数据库中绘制堆积图。 I have 10 comboboxes(drop down list). 我有10个组合框(下拉列表)。 The user can select up to 10 attributes. 用户最多可以选择10个属性。 The values are all double. 这些值都是双精度的。 The values should be normalized and averaged. 该值应归一化并平均。 Normalizing means to move the range of the values between 0 and 1. I get no errors from reading and calculations but when I am trying to draw the chart it gives me index error (out of range). 归一化意味着将值的范围移动到0到1之间。读取和计算没有错误,但是当我尝试绘制图表时,它给了我索引错误(超出范围)。 I am not sure what I am doing wrong. 我不确定自己在做什么错。 Here is my whole code: 这是我的整个代码:

SqlCommand cmd = new SqlCommand(command, con);
SqlDataReader reader ;
SqlDataAdapter da = new SqlDataAdapter(cmd);

con.Open();
reader = cmd.ExecuteReader();
DataTable dt = new DataTable();

List<List<double>> list = new List<List<double>>();
List<double> inner;
List<double> Outdistance = new List<double>();
//reading the columns and storing them into list. the coulmns 0 and 2 are not needed. 
while (reader.Read())
{
    inner = new List<double>();
    for(int i = 3;i<reader.FieldCount;i++)
    {
        var temp = reader.GetDouble(i);
        inner.Add(temp);
    }
    list.Add(inner);
    var temp2 = reader.GetDouble(1);
    Outdistance.Add(temp2);
}

con.Close();
List<List<double>> normallist = new List<List<double>>();
List<double> AVGDist = average(Outdistance, 10);

//normalizing the data. data-min/range 
foreach(List<double> l in list)
{
    normallist.Add(normalizer(l));
}
List<List<double>> avgList = new List<List<double>>();

//reducing the amount by averaging every 10 element. 
foreach (List<double> l in normallist)
{
    avgList.Add(average(l,10));
}

//drawing the chart 
foreach (List<double> lst in avgList)
{
    Series S = new Series();
    S.ChartType = SeriesChartType.StackedArea;
    chart1.Series.Add(S);
    for(int i = 0 ; i < lst.Count ; i++)
    {
        this.chart1.Series[S.Name].Points.AddXY(AVGDist[i],lst[i]);
    }
}

public List<double> average(List<double> T , int n)
{
    var currentElement = 0;
    var currentSum = 0.0;
    var newList = new List<double>();

    foreach (var item in T)
    {
        currentSum += item;
        currentElement++;

        if (currentElement == n)
        {
            newList.Add(currentSum / n);
            currentElement = 0;
            currentSum = 0.0;
        }
    }

    if (currentElement > 0)
    {
        newList.Add(currentSum / currentElement);
    }

    return newList;
}

public List<double> normalizer( List<double> T)
{
    double min, max, range;
    min = T.Min();
    max = T.Max();
    range = max - min;

    for (int i = 0; i < T.Count;i++ )
    {
        T[i] = (T[i] - min) / range;
    }
    return T;
}

I also checked my select string but it was correct. 我还检查了我的选择字符串,但这是正确的。 I think I am not using the correct code for the charts. 我认为我没有为图表使用正确的代码。 Can anyone please help? 谁能帮忙吗?

The error should be thrown in the below line, this.chart1.Series[S.Name].Points.AddXY(AVGDist[i],lst[i]); 该错误应在以下行中抛出:this.chart1.Series [S.Name] .Points.AddXY(AVGDist [i],lst [i]);

Series has been indexed using S.Name. 系列已使用S.Name索引。 S.Name should be set to some string, before it can be accessed in the above line. 在上一行可以访问S.Name之前,应将其设置为某个字符串。

The code for your charts is correct. 图表的代码正确。 And looking at your code, it looks like the index for your AVGDist is going out of bounds in this line 看一下您的代码,您的AVGDist的索引似乎超出了这一行

this.chart1.Series[S.Name].Points.AddXY(AVGDist[i],lst[i]);

Add a breakpoint here to debug further. 在此处添加断点以进一步调试。

for(int i=0; i < T.Count -1; i++) << I think the -1 is the problem add it and try. for(int i = 0; i <T.Count -1; i ++)<<我认为-1是问题,将其添加并尝试。

public List<double> normalizer( List<double> T)
{
    double min, max, range;
    min = T.Min();
    max = T.Max();
    range = max - min;

    for (int i = 0; i < T.Count -1;i++ )
    {
        T[i] = (T[i] - min) / range;
    }
    return T;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM