2012-09-10 54 views
0

我正在使用.Net圖表。我正在嘗試生成一個柱形圖。我在我的數據集中有兩行。這是我的代碼。列在.Net柱狀圖中合併

    DataTable DT1 = new DataTable(); 
        DT1 = chartViewSummaryList; 
        DataView DV = DT1.DefaultView; 
        DV.ToTable(false, new string[] { "REVENUE_TOTAL", "WEEK_END_DATE" }); 

        DataSet ds = new DataSet(); 
        ds.Tables.Add(DT1.Copy()); 

        ds.Tables[0].Rows[0]["MARGIN"] = "0"; 
        ds.Tables[0].Rows[0]["REVENUE_TOTAL"] = "3776169.61"; 
        ds.Tables[0].Rows[0]["MARGIN_PCT"] = "0"; 
        ds.Tables[0].Rows[0]["VISIT_REVENUE_AVG"] = "29.28"; 
        ds.Tables[0].Rows[0]["VISIT_COUNT"] = "614041"; 
        ds.Tables[0].Rows[0]["REVENUE_SALE"] = "1840387.18"; 
        ds.Tables[0].Rows[0]["REVENUE_REGULAR"] = "1935782.43"; 
        ds.Tables[0].Rows[0]["WEEK_END_DATE"] = "1/1/2012 12:00:00 AM"; 

        ds.Tables[0].Rows.Add(); 

        ds.Tables[0].Rows[1]["MARGIN"] = "1"; 
        ds.Tables[0].Rows[1]["REVENUE_TOTAL"] = "5776169.61"; 
        ds.Tables[0].Rows[1]["MARGIN_PCT"] = "0"; 
        ds.Tables[0].Rows[1]["VISIT_REVENUE_AVG"] = "49.28"; 
        ds.Tables[0].Rows[1]["VISIT_COUNT"] = "814041"; 
        ds.Tables[0].Rows[1]["REVENUE_SALE"] = "3840387.18"; 
        ds.Tables[0].Rows[1]["REVENUE_REGULAR"] = "3935782.43"; 
        ds.Tables[0].Rows[1]["WEEK_END_DATE"] = "1/1/2012 12:00:00 AM"; 



        Chart1.DataSource = ds; 

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
        { 
         Chart1.Titles.Add(storeLevelCaption).Font = new System.Drawing.Font("Verdana", 11, FontStyle.Bold); 
         Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Weeks"; 
         Chart1.ChartAreas["ChartArea1"].AxisX.TitleFont = new System.Drawing.Font("Verdana", 10, FontStyle.Bold); 
         Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Font = new Font(new FontFamily("Verdana"), 7.25F, FontStyle.Bold); 
         string series = "Series" + i; 
         Chart1.Series.Add(series); 

         Chart1.Series[series].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column; 

         Chart1.Series[series].XValueMember = "WEEK_END_DATE"; 
         Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false; 

         Color color = System.Drawing.ColorTranslator.FromHtml("#B93B8F"); 
         Chart1.Series[series].Color = color; 

         Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Total Revenue"; 
         Chart1.ChartAreas["ChartArea1"].AxisY.TitleFont = new System.Drawing.Font("Verdana", 10, FontStyle.Bold); 
         Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Font = new Font(new FontFamily("Verdana"), 7.25F, FontStyle.Bold); 
         Chart1.Series[series].YValueMembers = "REVENUE_TOTAL"; 
         Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false; 

         Chart1.Series[series].ToolTip = "Total Revenue ($) , " + ds.Tables[0].Rows[i]["WEEK_END_DATE"].ToString() + " , " + ds.Tables[0].Rows[i]["REVENUE_TOTAL"].ToString(); 

         Chart1.Series[series].Font = new Font(new FontFamily("Verdana"), 7.25F, FontStyle.Bold); 
         //Chart1.Series[series].Name = "Total Revenue"; 
        } 
        Chart1.ChartAreas[0].AxisY.Interval = 1; 
        Chart1.DataBind(); 

我得到圖表,其中兩列組合在一起並顯示爲一列。我在哪裏出錯了...

回答

0

我不能使用WinForm圖表組件重現您的問題。但是,你可能想嘗試使用不同的日期,因爲它們綁定到你的X值。

ds.Tables[0].Rows[0]["WEEK_END_DATE"] = "1/1/2012 12:00:00 AM"; 
ds.Tables[0].Rows[1]["WEEK_END_DATE"] = "8/1/2012 12:00:00 AM"; // a different date 

僅供參考,我改變了你的代碼minimaly

 // DT1 = chartViewSummaryList; 
     // replace unknown object with this code 
     DT1.Columns.Add(new DataColumn("MARGIN")); 
     DT1.Columns.Add(new DataColumn("REVENUE_TOTAL")); 
     DT1.Columns.Add(new DataColumn("MARGIN_PCT")); 
     DT1.Columns.Add(new DataColumn("VISIT_REVENUE_AVG")); 
     DT1.Columns.Add(new DataColumn("VISIT_COUNT")); 
     DT1.Columns.Add(new DataColumn("REVENUE_SALE")); 
     DT1.Columns.Add(new DataColumn("REVENUE_REGULAR")); 
     DT1.Columns.Add(new DataColumn("WEEK_END_DATE")); 

,並手動

 ds.Tables.Add(DT1.Copy()); 
     ds.Tables[0].Rows.Add(); // Added this 

添加了一行,並從你的繪圖代碼有兩列。

+0

感謝您的回覆... – RobinHood

+0

請您好好看看... http://stackoverflow.com/questions/12382247/date-in-x-axis-net-charts – RobinHood