2013-10-31 90 views
1

我正在顯示Winform .Net Chart範圍欄圖,顯示一天中員工工作的門票。如果可以的話,我想在08:00到17:00之間的區域採用淺綠色。我試圖把範圍系列,但它不會允許。我想我可以使用postpaint方法來做到這一點,但我無法弄清楚如何找到繪製矩形的位置。如何在Winform .Net 4.0 Chart Rangebar(C#)中顯示陰影區域

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Linq; 
using System.Data.Objects; 
using System.Diagnostics; 
using System.Data.SqlClient; 
using System.Windows.Forms.DataVisualization.Charting; 

namespace CWHelper 
{ 
    public partial class DailyTimeEntryDisplay : CWHelper.BaseForm 
    { 
     Object dataLock = new object(); 
     public DailyTimeEntryDisplay(string member_ID) 
     { 
      InitializeComponent(); 
      mMember_ID = member_ID; 
     } 
     string mMember_ID; 

     private void DailyTimeEntryDisplay_Load(object sender, EventArgs e) 
     { 
      DateTime minDate = new DateTime(1900, 1, 1); 
      chart1.ChartAreas[0].AxisY.Minimum = minDate.ToOADate(); 
      chart1.ChartAreas[0].AxisY.Maximum = minDate.AddDays(1).ToOADate(); 
      chart1.ChartAreas[0].AxisY.Interval = 1; 
      chart1.ChartAreas[0].AxisY.LabelStyle.Format = "HH:mm"; 
      chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1; 
      chart1.ChartAreas[0].AxisY.LabelStyle.Angle = 45; 
      chart1.ChartAreas[0].AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours; 

      chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1; 
      chart1.ChartAreas[0].AxisX.Interval = 1; 

      chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeBar; 
      chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String; 
      chart1.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime; 

      double rangeStart = new DateTime(1900,1,1,8,0,0).ToOADate(); 
      double rangeEnd = new DateTime(1900,1,1,17,0,0).ToOADate(); 
      double[] range = new double[] {rangeStart,rangeEnd}; 

      DisplayData(); 
     } 

     private void DisplayData() 
     { 
       try 
       { 

        DateTime sd = dateTimePicker1.Value; 
        DateTime? dt = new DateTime(sd.Year, sd.Month, sd.Day); 
        cwwebapp_drsEntities dc = new cwwebapp_drsEntities(); 

        var q = dc.GetTimeEntry(mMember_ID, dt); 
        var ql = q.ToList(); 
        var chartData = (from x in ql 
            select new { SR_Service_RecID = x.SR_Service_RecID != 0 ? x.SR_Service_RecID.ToString() : "Time Entry:", 
             Time_Start = x.Time_Start.ToOADate(), 
             Time_End = x.Time_End.ToOADate(), 
             tooltip = x.Company_Name + " : " + x.Notes, 
             summary = x.Summary 
            }).ToList(); 


        chart1.Series[0].Points.DataBind(chartData, "SR_Service_RecID", "Time_Start,Time_End", "Tooltip=tooltip,Label=summary"); 

       } 
       catch (Exception ex) 
       { 
        Debug.Print(ex.Message); 
        if (ex.InnerException != null) 
        { 
         Debug.Print(ex.InnerException.Message); 
        } 
       } 


     } 




     private void dateTimePicker1_ValueChanged(object sender, EventArgs e) 
     { 
      DisplayData(); 
     } 

     private void LeftButton_Click(object sender, EventArgs e) 
     { 
      dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1); 
     } 

     private void RightButton_Click(object sender, EventArgs e) 
     { 
      dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1); 
     } 

     private void ReloadButton_Click(object sender, EventArgs e) 
     { 
      DisplayData(); 
     } 
    } 
} 
+0

也許你想尋找到帶狀線。參見[這裏的一個例子(http://stackoverflow.com/questions/38958646/winforms-chart-how-to-enable -background-color-gauge/38959173#38959173) – TaW

回答

0

IMO,你不需要任何PostPaint方法。 .Net Charting可以讓你做到這一點。請嘗試以下代碼:

private void DailyTimeEntryDisplay_Load(object sender, EventArgs e) 
{ 
    chart1.Palette = ChartColorPalette.None; 
    chart1.PaletteCustomColors = new Color[] { Color.LightGreen }; 

    //DateTime minDate = new DateTime(1900, 1, 1); 
    //chart1.ChartAreas[0].AxisY.Minimum = minDate.ToOADate(); 
    //chart1.ChartAreas[0].AxisY.Maximum = minDate.AddDays(1).ToOADate(); 

    chart1.ChartAreas[0].AxisY.Interval = 1; 
    chart1.ChartAreas[0].AxisY.LabelStyle.Format = "HH:mm"; 
    chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1; 
    chart1.ChartAreas[0].AxisY.LabelStyle.Angle = 45; 
    chart1.ChartAreas[0].AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours; 

    chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1; 
    chart1.ChartAreas[0].AxisX.Interval = 1; 
    chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Auto; 

    chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeColumn; // "RangeColumn" instead of "RangeBar" 
    chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String; 
    chart1.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time; // "Time" instead of "DateTime" 

    DisplayData(); 
} 
+0

感謝代碼簡化了時間顯示。 PaletteCustomColors代碼只會改變我的酒吧的顏色。更改爲RangeColumn只需翻轉x和y軸。儘管我希望能夠遮蔽背景的一部分。 –

+0

刪除axisY最小和最大值也停止顯示24小時,所以我不得不回到我原來的代碼。無論如何,我都希望看到整整一天。 –