如何在Winform .Net 4.0图表范围栏(C#)中显示阴影区域

时间:2013-10-31 12:24:10

标签: c# .net winforms charts

我正在显示一张Winform .Net Chart范围栏图,显示员工在白天工作的门票。如果可以的话,我希望从08:00到17:00的区域是浅绿色。我尝试放置一系列系列,但不允许这样做。我想我可以使用postpaint方法来做到这一点,但我无法弄清楚如何找到绘制矩形的位置。

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();
        }
    }
}

1 个答案:

答案 0 :(得分: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();
}