【问题标题】:Aligning range column data with x axis labels in chart control在图表控件中将范围列数据与 x 轴标签对齐
【发布时间】:2015-08-12 04:49:47
【问题描述】:

我正在尝试使用 C# .NET 中的 Chart 控件绘制一组代理任务的范围列图。我在 x 轴上绘制代理编号,在 y 轴上绘制任务时间。我唯一的问题是列数据不会与 x 轴上的代理编号正确对齐。有谁知道如何将列与相应的 x 轴标签对齐?

这是我的图表的图像:

这是我的代码:

    chartSchedule.Titles.Add("Agent / Task Schedule");
    chartSchedule.ChartAreas[0].AxisX.Title = "Agent";
    chartSchedule.ChartAreas[0].AxisY.Title = "Time";

    int index = 0;
    foreach ( Agent a in _agents )
    {
        // Create a series for each agent and set up display details
        Series agentSeries = chartSchedule.Series.Add("Agent " + a.Id);
        agentSeries.ChartType = SeriesChartType.RangeColumn;

        // Alternate colours of series lines
        if ( index % 2 > 0 )
          agentSeries.Color = Color.DodgerBlue;
        else
          agentSeries.Color = Color.Blue;

        // Display start and end columns of every task
        List<DataPoint> timeData = new List<DataPoint>();
        foreach ( NodeTask t in a.AssignedTasks )
        {
            agentSeries.Points.AddXY(index + 1, t.StartTime, t.EndTime);
        }

        index++;
    }

【问题讨论】:

  • 谢谢。我编辑了帖子以包含指向我的图形图像的链接
  • 这太棒了!我试过了,它完全符合我的要求。我只需要更改 Legends[0].CustomItems 行,它是 chartSchedule 而非 agentSeries 的属性。非常感谢您的帮助。

标签: c# charts alignment series


【解决方案1】:

看似“未对齐”的原因是您总共添加了五个系列,但每个系列的每个 X 值只有一个(一组)数据点。

然后将这个存在的 DataPoint 与四个不存在的 DataPoint 组合在一起,其中五个并排显示为以 X 值/标签为中心的一个块。 这看起来很正确对于中间的Series,其实是有中间点的。

你可以添加一些其他的点来看看效果..:

    agentSeries.Points.AddXY(1, 1, 4);
    agentSeries.Points.AddXY(2, 1, 2);
    agentSeries.Points.AddXY(4, 1, 3);

所以最自然的解决方案是不添加缺少数据的系列。

不确定您是否对此解决方案感到满意,或者是否有更好的方法,但结果看起来还不错..

我不再添加所有这些系列,而是将所有数据添加到同一个系列

为了创建图例,我通过将其颜色设置为透明来隐藏常规图例。 (它必须在那里。)然后我添加新的 Legend CustomItems 并像你一样给它们颜色和名称。

这是我使用的代码,除了我模拟的实际数据:

    chartSchedule.Series.Clear();
    ChartArea CA = chartSchedule.ChartAreas[0];

    chartSchedule.Titles.Add("Agent / Task Schedule");
    chartSchedule.ChartAreas[0].AxisX.Title = "Agent";
    chartSchedule.ChartAreas[0].AxisY.Title = "Time";

    // our only Series
    Series agentSeries = chartSchedule.Series.Add(" " );
    agentSeries.ChartType = SeriesChartType.RangeColumn;
    agentSeries.Color = Color.Transparent;  // hide the default series entry!

    agentSeries["PixelPointWidth"] = "20"; // <- your choice of width!

    int index = 0;
    foreach (Agent a in _agents)
    {
        // Alternate colours 
        Color color = index % 2 == 0 ? Color.DodgerBlue : Color.Blue;

        // Display start and end columns of every task
        List<DataPoint> timeData = new List<DataPoint>();  ///???
        foreach (NodeTask t in a.AssignedTasks)
        {
            int p = agentSeries.Points.AddXY(index +1, t.StartTime, t.EndTime);
            agentSeries.Points[p].Color = color;
        }

        chartSchedule.Legends[0].CustomItems.Add(color, "Agent " + index);
        index++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多