0

大家,早安,

我正在从 C# 中的变量 DataTable 创建图表。我看过不同的示例代码,我尝试应用所有内容,但我看不到图表中反映的数据。这是我用数据填充图表的代码:

//Aggregates the series - For our app we're using from the third to the last column generated from the datatable used for our chart.
for (i = 2; i < myAux.Columns.Count; i++)
{
    this.chartReport.Series.Add(myAux.Columns[i].ColumnName);
}

//Assigns the value of the XAxis according to the name of the first column from our datatable assigned to the datasource of the chart.
for (i = 0; i < this.chartReport.Series.Count; i++)
{
    this.chartReport.Series[i].XValueMember = myAux.Columns[0].ColumnName;
    this.chartReport.Series[i].YValueMembers = myAux.Columns[1].ColumnName;
    this.chartReport.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
}

//Add values X and Y to the chart
for (i = 1; i < myAux.Rows.Count; i++)
{
    int m = 0;
    for (j = 2; j < myAux.Columns.Count; j++)
    {
        this.chartReport.Series[m].Points.Add(Convert.ToDouble(myAux.Rows[i][j].ToString()));
        m++;
    }
}

在我的代码中,变量myAux是使用用户提供的参数创建的数据表,我将其用作主表的副本,显示用户之前制作的报告数据。

我不知道我在这里错过了什么,但我确信这是它避免显示我图表的条形的东西。如果有人有想法,我会非常感激答案,因为我被困了几天!

提前非常感谢。

编辑:我发现了问题的一部分:如果我在代码中注释第二个,它会带来数据,但现在我遇到了问题,因为我无法显示 XAxis 和 YAxis 中的内容。如果您知道我应该做什么,我将不胜感激!!

4

2 回答 2

1

我发现问题出在哪里......我在系列和 YAxis 值之间分配了错误的值。所以我所做的是改变整个创作和分配:

//Aggregates the series - For our app we're using from the third to the last column generated from the datatable used for our chart.
Series mySerie = new Series();
mySerie.Name = myAux.Columns[1].ColumnName;
mySerie.XValueMember = myAux.Columns[0].ColumnName;

for (i = 2; i < myAux.Columns.Count; i++)
{
    mySerie.YValueMembers = myAux.Columns[i].ColumnName;
}

this.chartReport.Series.Add(mySerie);

有了这个,我解决了没有出现的值的整个问题。现在我必须处理图表的展示:)

于 2013-07-04T07:50:55.943 回答
0

我想我可能知道出了什么问题。该系列从未创建。首先为您的图表创建一个系列。请注意,应创建每个系列。

mychart.Series[0] = new Series("MySeries");
于 2013-07-03T08:34:33.187 回答