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

发布于 2024-11-29 21:47:26 字数 6686 浏览 1 评论 0原文

更新:据我所知,这与日期本身关系不大。这只是图表上有一个长条的情况,当关注它的一小部分(日期/时间、短值范围等)时,它会停止为条着色。除了对此视而不见之外,仍然不知道为什么或如何解决它。

我一直在尝试解决一个涉及堆叠图表和日期限制的奇怪小错误。假设您将 Y 轴设置为日期,X 轴设置为项目,并使用条形图作为项目完成情况、项目截止日期和项目逾期情况。

现在,如果您将 Y 轴上显示的最小和最大日期设置为相隔几天,对于预计持续数月的项目,您将看到问题。条形图会失去颜色,但如果增大最小值和最大值之间的差距,问题就会消失。

以下是所涉及代码的片段:

    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";
    }

上面的项目涵盖从 01/01/2011 00:00:00 到 01/01/2011 15:00:00 和 01/02/2011 12:00:00 到 01 /07/2011 07:00:00。

最初,日期限制是今天的日期,没有特殊原因。

这是显示问题的图像。除了较小的属性更改之外,没有其他代码与该图显示的内容有任何关系,但为了彻底起见,我将从设计器文件中添加以下代码。

        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";

这是显示问题的图像: http://i55.tinypic.com/27zkv0w.jpg

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.

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. 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-12-06 21:47:26
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";
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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文