1

我正在显示一个 Winform .Net 图表范围条图,显示员工白天处理的票证。如果可以的话,我想要从 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();
        }
    }
}
4

1 回答 1

1

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();
}
于 2013-11-02T18:27:49.037 回答