0

我有以下代码创建一个包含三个系列的图表,第一个系列是柱形栏,第二个和第三行。当有从返回的数据时创建图表,但当没有sqlcommand返回行时,它应该至少显示 x 和 y 轴。

C#代码:

string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["AVISConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(DatabaseConnectionString);
conn.Open();
DataTable dtSr = new DataTable();
SqlCommand cmd = null;
SqlDataReader reader = null;
cmd = new SqlCommand("Select case when KWmittel is null then '0' else KWmittel  end as [KWmittel], case when YTD is null then '0' else YTD  end as [YTD], case when soll is null then '0' else soll end as [soll] from dbo.Ausbringung_Wiegen_2013 a inner join dbo.ytd y on a.weekID = y.weekID inner join dbo.sollno s on y.weekID = s.weekID where a.weekID < datepart(week,getdate())", conn);
reader = cmd.ExecuteReader();
if (!reader.Read())
{
    //I don't know how to handle here, I just gave a try but doesn't work
    Chart4.ChartAreas["Chartarea1"].AxisX.Minimum = 0;
    Chart4.ChartAreas["Chartarea1"].AxisY.Minimum = 0;
    Chart4.Series["s1"].YValueMembers = null;
    Chart4.Series["s2"].YValueMembers = null;
    Chart4.Series["s3"].YValueMembers = null;
}
dtSr.Load(reader);            
Chart4.DataSource = dtSr;
Chart4.Series["s1"].YValueMembers = "KWmittel";
Chart4.Series["s2"].YValueMembers = "YTD";
Chart4.Series["s3"].YValueMembers = "soll";            
Chart4.DataBind();
cmd = null;
dtSr.Clear(); 

我真的很感激任何帮助和想法。谢谢 :)

4

1 回答 1

0

我自己找到了这个问题的答案,代码如下:

dtSr.Load(reader);
        if(dtSr.Columns==null)
        {
            for (int i = 0; i < seriesa.Length; i++)
            {
                Chart3.Series[seriesa[i]].Points.AddY(0);
            }
        }
        else
        {
            Chart3.DataSource = dtSr;                                                    
            Chart3.Series["s2"].YValueMembers = "YTD";
            Chart3.Series["s3"].YValueMembers = "soll";
            Chart3.Series["s1"].YValueMembers = "KW_Mittel";
            Chart3.DataManipulator.InsertEmptyPoints(1, System.Web.UI.DataVisualization.Charting.IntervalType.Number, Chart3.Series["s3"]);
            Chart3.DataManipulator.InsertEmptyPoints(1, System.Web.UI.DataVisualization.Charting.IntervalType.Number, Chart3.Series["s2"]);
            Chart3.Series["s3"].EmptyPointStyle.Color = System.Drawing.Color.Transparent;
            Chart3.Series["s2"].EmptyPointStyle.Color = System.Drawing.Color.DarkGreen;
            Chart3.Series["s2"].BorderWidth = 2;
            Chart3.DataBind();
        }           
于 2013-07-24T14:13:02.063 回答