我的数据如下所示。我想以这样一种方式显示数据:时间(图中没有列名和#34;列)应该沿着Y轴,而testiD和queryId应该沿着X轴。我需要这样一种方式,即具有相同testId的项目应该被分组(在X轴上)并且应该显示相应的时间(Y轴)
这是我使用的代码,它不能像我预期的那样工作。
protected internal Chart GenerateChart(DataTable dtChartDataSource,Chart chart,int intType )
{
ChartArea chartArea = new ChartArea() { Name = "ChartArea" };
chart.ChartAreas.Add(chartArea);
chart.Palette = ChartColorPalette.BrightPastel;
string series = string.Empty;
if (dtChartDataSource != null)
{
foreach (DataColumn dc in dtChartDataSource.Columns)
{
if (chart.Series.FindByName(dc.ColumnName) == null)
{
series = dc.ColumnName[1].ToString();
chart.Series.Add(series);
chart.Series[series].ChartType = (SeriesChartType)intType;
}
foreach (DataRow dr in dtChartDataSource.Rows)
{
double dataPoint = 0;
double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint);
DataPoint objDataPoint = new DataPoint() { AxisLabel = "series", YValues = new double[] { dataPoint } };
chart.Series[series].Points.Add(dataPoint);
}
}
}
return chart;
}
我可以通过以下代码完成我的要求。
公共图表fnTestChart(图表图表,DataTable dt) { DataTable dtUniqueCols = dt.DefaultView.ToTable(true," Test ID"); chart.ChartAreas.Add("区域&#34); chart.ChartAreas [" area"]。AxisX.Minimum = 0; //chart.ChartAreas["area"].AxisX.Interval = 1; chart.ChartAreas [" area"]。AxisY.Minimum = 0; //chart.ChartAreas["area"].AxisY.Interval = 1;
foreach (DataRow dr in dtUniqueCols.Rows)
{
chart.Series.Add(dr[0].ToString());
}
foreach (DataRow dr in dt.Rows)
{
chart.Series[dr[0].ToString()].Points.AddXY(dr[1].ToString(), dr[2].ToString());
}
return chart;
}
答案 0 :(得分:-3)