C# 从windows窗体C中的列表绘制图表
我正试图从我的数据库中绘制一个叠加图。我有10个下拉列表。用户最多可以选择10个属性。数值都是双倍的。这些值应标准化并取平均值。规格化意味着将值的范围移动到0和1之间。我没有从阅读和计算中得到任何错误,但当我试图绘制图表时,它给了我超出范围的索引错误。我不确定我做错了什么。这是我的全部代码:C# 从windows窗体C中的列表绘制图表,c#,C#,我正试图从我的数据库中绘制一个叠加图。我有10个下拉列表。用户最多可以选择10个属性。数值都是双倍的。这些值应标准化并取平均值。规格化意味着将值的范围移动到0和1之间。我没有从阅读和计算中得到任何错误,但当我试图绘制图表时,它给了我超出范围的索引错误。我不确定我做错了什么。这是我的全部代码: SqlCommand cmd = new SqlCommand(command, con); SqlDataReader reader ; SqlDataAdapter da = new SqlDataAd
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;
}
我还检查了select字符串,但它是正确的。我认为我没有为图表使用正确的代码。有人能帮忙吗 应该在下面的行中抛出错误, this.chart1.Series[S.Name].Points.AddXYAVGDist[i],lst[i]
序列已使用S.Name编制索引。在可以在上面的行中访问之前,应该将S.Name设置为某个字符串。图表的代码是正确的。看看你的代码,你的AVGDist的索引似乎超出了这一行的范围
this.chart1.Series[S.Name].Points.AddXY(AVGDist[i],lst[i]);
在此处添加断点以进一步调试。forint i=0;i
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;
}