简体   繁体   English

C# WinForms MSChart(StackedBar - 甘特图)带 Y 轴日期限制

[英]C# WinForms MSChart (StackedBar - Gantt Chart) With Y Axis Date Restrictions

UPDATE: From what I've seen, this has little to do with the date itself.更新:据我所知,这与日期本身无关。 It's simply a case of having a long bar on the chart, when focusing in on a short segment of it (Date/Time, Short value ranges, etc), then it stops coloring the bar.这只是图表上有一个长条的情况,当专注于它的一小段(日期/时间、短值范围等)时,它会停止为条着色。 Still no idea why or how to fix it, other than turning a blind eye to it.除了对它视而不见之外,仍然不知道为什么或如何解决它。

I've been attempting to solve a weird little error involving stacked charts and date restrictions.我一直在尝试解决一个涉及堆叠图表和日期限制的奇怪小错误。 Say you set the Y Axis as Dates, the X Axis is projects, and you use the bars as project completion, project deadline and project overdue.假设您将 Y 轴设置为日期,X 轴是项目,您将条形用作项目完成、项目截止日期和项目逾期。

Now if you set the minimum and maximum dates shown on the Y Axis to a few days apart, on a project expected to last months, you will see the problem.现在,如果您将 Y 轴上显示的最小和最大日期设置为相隔几天,在预计持续数月的项目中,您将看到问题。 The bar loses its color, though if you increase the gap between minimum and maximum then the problem goes away.该条失去了颜色,但如果您增加最小值和最大值之间的差距,那么问题就会消失。

Below is a snippit of the code involved:以下是涉及的代码片段:

    private void _Gantt_Load(object sender, EventArgs e)
    {
        chart1.MouseUp                      += new MouseEventHandler(MouseHandler);

        _dtpGraphStart.ValueChanged         += new EventHandler(DateTimeHandler);
        _dtpGraphEnd.ValueChanged           += new EventHandler(DateTimeHandler);

        string pOneName = "Project 1";
        string pTwoName = "Project 2";
        DateTime pOneStart = new DateTime(2011, 01, 01, 0, 0, 0);
        DateTime pTwoStart = new DateTime(2011, 02, 01, 12, 0, 0);
        DateTime pOneEnd = new DateTime(2011, 01, 01, 15, 0, 0);
        DateTime pTwoEnd = new DateTime(2011, 07, 01, 7, 0, 0);
        double pOneTotal = (pOneEnd - pOneStart).TotalDays;
        double pTwoTotal = (pTwoEnd - pTwoStart).TotalDays;
        double pOnePercent = 75;
        double pTwoPercent = 50;
        double pOneComplete = (pOnePercent / 100.0f) * pOneTotal;
        double pTwoComplete = (pTwoPercent / 100.0f) * pTwoTotal;

        chart1.Series["StartSeries"].Points.AddXY(pOneName, pOneStart);
        chart1.Series["StartSeries"].Points.AddXY(pTwoName, pTwoStart);
        chart1.Series["ProjectDurationSeries"].Points.AddXY(pOneName, pOneComplete);
        chart1.Series["ProjectDurationSeries"].Points.AddXY(pTwoName, pTwoComplete);
        chart1.Series["ProjectDurationSeries"].Points[0].Tag = "TestOne";
        chart1.Series["ProjectDurationSeries"].Points[1].Tag = "TestTwo";
        chart1.Series["ProjectRemainingSeries"].Points.AddXY(pOneName, pOneTotal - pOneComplete);
        chart1.Series["ProjectRemainingSeries"].Points.AddXY(pTwoName, pTwoTotal - pTwoComplete);
        chart1.Series["ProjectRemainingSeries"].Points[0].Tag = "TestCompleteOne";
        chart1.Series["ProjectRemainingSeries"].Points[1].Tag = "TestCompleteTwo";
        chart1.ChartAreas[0].AxisY.Minimum = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day,  0,  0,  0).ToOADate();
        chart1.ChartAreas[0].AxisY.Maximum = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59).ToOADate();
        chart1.DataBind();

        _dtpGraphEnd.MinDate = _dtpGraphStart.Value;
        _dtpGraphStart.MaxDate = _dtpGraphEnd.Value;
    }

    private void DateTimeHandler(object sender, EventArgs e)
    {
        DateTimePicker dtp = (DateTimePicker)sender;

        switch (dtp.Name)
        {
            case "_dtpGraphStart":
                chart1.ChartAreas[0].AxisY.Minimum = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 0, 0, 0).ToOADate();
                _dtpGraphEnd.MinDate = dtp.Value;
                break;
            case "_dtpGraphEnd":
                chart1.ChartAreas[0].AxisY.Maximum = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, 23, 59, 59).ToOADate();
                _dtpGraphStart.MaxDate = dtp.Value;
                break;
        }

        if ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) < 3)
            chart1.ChartAreas[0].AxisY.LabelStyle.Format = "T";
        if (((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) > 3) 
            && ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) < 30))
            chart1.ChartAreas[0].AxisY.LabelStyle.Format = "dd/MM";
        if ((chart1.ChartAreas[0].AxisY.Maximum - chart1.ChartAreas[0].AxisY.Minimum) > 30)
            chart1.ChartAreas[0].AxisY.LabelStyle.Format = "MM/yyyy";
    }

The above has Projects spanning from 01/01/2011 00:00:00 to 01/01/2011 15:00:00, and 01/02/2011 12:00:00 to 01/07/2011 07:00:00.上面的项目从 01/01/2011 00:00:00 到 01/01/2011 15:00:00 和 01/02/2011 12:00:00 到 01/07/2011 07:00:00 .

Initially the date restriction is on todays date, for no particular reason.最初的日期限制是在今天的日期,没有特别的原因。

Heres an image displaying the issue.这是显示问题的图像。 No other code has anything to do with what this graph displays, other than minor property changes, but for the sake of being thorough, I'll add those below from the designer file.除了较小的属性更改之外,没有其他代码与此图表显示的内容有关,但为了彻底,我将从设计器文件中添加以下代码。

        chartArea1.AxisX.MajorGrid.Enabled = false;
        chartArea1.AxisY.InterlacedColor = System.Drawing.Color.Lime;
        chartArea1.AxisY.LabelStyle.Format = "T";
        chartArea1.AxisY.MajorGrid.Enabled = false;
        chartArea1.Name = "ChartArea1";
        this.chart1.ChartAreas.Add(chartArea1);
        this.chart1.Location = new System.Drawing.Point(0, 0);
        this.chart1.Name = "chart1";
        series1.ChartArea = "ChartArea1";
        series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
        series1.Color = System.Drawing.Color.White;
        series1.Name = "StartSeries";
        series2.BorderColor = System.Drawing.Color.Black;
        series2.BorderWidth = 2;
        series2.ChartArea = "ChartArea1";
        series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
        series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
        series2.EmptyPointStyle.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
        series2.Name = "ProjectDurationSeries";
        series3.BackHatchStyle = System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
        series3.BackSecondaryColor = System.Drawing.Color.White;
        series3.BorderColor = System.Drawing.Color.Black;
        series3.BorderWidth = 2;
        series3.ChartArea = "ChartArea1";
        series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
        series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
        series3.Name = "ProjectRemainingSeries";
        series4.BackHatchStyle = System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
        series4.BorderColor = System.Drawing.Color.Black;
        series4.BorderWidth = 2;
        series4.ChartArea = "ChartArea1";
        series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
        series4.Color = System.Drawing.Color.Red;
        series4.Name = "Series4";
        this.chart1.Series.Add(series1);
        this.chart1.Series.Add(series2);
        this.chart1.Series.Add(series3);
        this.chart1.Series.Add(series4);
        this.chart1.Size = new System.Drawing.Size(727, 339);
        this.chart1.TabIndex = 0;
        this.chart1.Text = "chart1";

Heres an image displaying the problem: http://i55.tinypic.com/27zkv0w.jpg下面是显示问题的图像: http://i55.tinypic.com/27zkv0w.jpg

chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.AxisY.InterlacedColor = System.Drawing.Color.Lime;
chartArea1.AxisY.LabelStyle.Format = "T";
chartArea1.AxisY.MajorGrid.Enabled = false;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series1.Color = System.Drawing.Color.White;
series1.Name = "StartSeries";
series2.BorderColor = System.Drawing.Color.Black;
series2.BorderWidth = 2;
series2.ChartArea = "ChartArea1";
series2.ChartType = 
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte) 
(192)))), ((int)(((byte)(0)))));
series2.EmptyPointStyle.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), 
((int)(((byte)(192)))), ((int)(((byte)(0)))));
series2.Name = "ProjectDurationSeries";
series3.BackHatchStyle = 
System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series3.BackSecondaryColor = System.Drawing.Color.White;
series3.BorderColor = System.Drawing.Color.Black;
series3.BorderWidth = 2;
series3.ChartArea = "ChartArea1";
series3.ChartType = 
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)
(((byte)(128)))), ((int)(((byte)(255)))));
series3.Name = "ProjectRemainingSeries";
series4.BackHatchStyle = 
System.Windows.Forms.DataVisualization.Charting.ChartHatchStyle.ForwardDiagonal;
series4.BorderColor = System.Drawing.Color.Black;
series4.BorderWidth = 2;
series4.ChartArea = "ChartArea1";
series4.ChartType = 
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
series4.Color = System.Drawing.Color.Red;
series4.Name = "Series4";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(727, 339);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM