如何在Winforms中制作带有重叠点的甘特图 [英] How can I make a gannt chart with overlapping points in winforms

查看:79
本文介绍了如何在Winforms中制作带有重叠点的甘特图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类似8:00-17:00的时间间隔,在这个时间间隔中发生了多次任务,例如9:00-9:20,11:00-12:00和13:00-13:20,我想在winforms中制作一个图表,以显示任务何时以这种方式发生.那么我可以使用DEV甘特图来做到这一点吗?还是我可以使用任何控件?

There is a time interval like 8:00-17:00.In this time interval happens a task several times for example 9:00-9:20 , 11:00-12:00, 13:00-13:20.I want to make a chart in winforms to show when the task happens like this. So can I do this with DEV gannt chart? Or is there any Control I can use to make it?

我想在winforms中执行此操作.

I want to do this in winforms.

推荐答案

使用MSChart和ChartType RangeBar相当容易.

This is quite easy with MSChart and the ChartType RangeBar.

这里是一个例子:

要获得此结果,您需要

  • 从数据工具箱添加MSChart控件
  • 添加using子句: using System.Windows.Forms.DataVisualization.Charting;
  • 然后您可以为图表设置样式.
  • ..并可能设置大小限制

以下是设置代码:

void setUpGantt(Chart chart)
{
    chart.Series.Clear();
    Series s = chart.Series.Add("gantt");
    s.ChartType = SeriesChartType.RangeBar;
    s.YValueType = ChartValueType.DateTime;
    s.AxisLabel = "";
    s.IsVisibleInLegend = false;
    Axis ax = chart.ChartAreas[0].AxisX;
    Axis ay = chart.ChartAreas[0].AxisY;
    ax.MajorGrid.Enabled = false;
    ax.MajorTickMark.Enabled = false;
    ax.LabelStyle.Format = " ";
    ax.Enabled = AxisEnabled.False;
    ay.LabelStyle.Format = "HH:mm";
    ay.MajorGrid.Enabled = false;
    ay.MajorTickMark.Enabled = false;
    ay.LineColor = chart.BackColor;
    limitGantt(chart, "8:00", "17:00");
}

void limitGantt(Chart chart, string start, string end)
{
    Axis ax = chart.ChartAreas[0].AxisX;
    ax.Minimum = 0.5;  // we have only one slot
    ax.Maximum = 1.5;  // the bar is centered on its value (1)

    Axis ay = chart.ChartAreas[0].AxisY;
    ay.Minimum = fromTimeString(start).ToOADate();  // we exclude all times..
    ay.Maximum = fromTimeString(end).ToOADate();    // ..outside a given range
}

请注意,为方便起见,我使用时间 strings .当然,您可以更改为直接使用 DateTimes .为了将时间 string 转换为当前日期的 DateTime ,使用此函数:

Note that I used time strings for convenience. Of course you can change to using DateTimes directly. For the conversion of a time string to a DateTime of the current day this function is used:

DateTime fromTimeString(string time)
{
    var p = time.Split(':');
    int sec = p.Length == 3 ? Convert.ToInt16(p[2]) : 0;
    TimeSpan t = new TimeSpan(Convert.ToInt16(p[0]), Convert.ToInt16(p[1]), sec);
    return DateTime.Today.Add(t);
}

请注意,所有代码都没有任何检查!

Note that all the code is lacking any checks!

要添加任务,请使用此方法:

To add a task this method is used:

void addGanttTask(Series s, string start, string end, Color c, int slot )
{
    DateTime start_ = fromTimeString(start);
    DateTime end_ = fromTimeString(end);
    int pt = s.Points.AddXY(slot, start_, end_);
    s.Points[pt].Color = c;
}

请注意,它既包含系列又包含插槽".插槽用于x值,在您的情况下,它们都是相同的.但是,人们可以轻松地为拥有更多资源的规划师创建形象,其中包括用于各种资源的多个栏,例如各种房间或团队.

Note that it contains both a Series and a 'slot'. The slots are used for the x-values, which in your case all are the same. But one can easily image a more comples planner with several bars for several resources, like various rooms or teams..

系列"参数将允许覆盖第二个系列,就像您在

The Series parameter would allow to overlay a second series like you can see in this nice example from MSDN..

这是我填写图表的方式:

Here is how I filled the chart:

setUpGantt(chart1);

Series s = chart1.Series[0];
addGanttTask(s, "8:00", "17:00", Color.LimeGreen, 1);
addGanttTask(s, "9:00", "9:20", Color.DarkSlateBlue, 1);
addGanttTask(s, "11:00", "12:00", Color.DarkSlateBlue, 1);
addGanttTask(s, "13:00", "13:20", Color.DarkSlateBlue, 1);

请注意,不同的范围可能重叠并且可能彼此隐藏.在我们的示例中,绿色栏首先添加,其他栏位于顶部.在MSDN示例中,您将看到黄色条形如何变窄以使其下方的条形可见.他们属于第二系列.

Note that different ranges may overlap and might hide each other. In our example the green bar is added first and the others lie on top. In the MSDN example you see how the yellow bars are narrower to keep the bars under them visible. They belong to a 2nd series.

要更改条的宽度,请使用

To change the bars' widths use

series.SetCustomProperty("PixelPointWidth",  "15");

这篇关于如何在Winforms中制作带有重叠点的甘特图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆