在图表控件中将范围列数据与 x 轴标签对齐
Aligning range column data with x axis labels in chart control
我正在尝试使用 C# .NET 中的图表控件绘制一组代理任务的范围柱状图。我在 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++;
}
看似 'misalignment' 的原因是您总共添加了五个系列,但每个系列的每个 X 值只有一个(一组)数据点。
然后将这个存在的 DataPoint 与四个不存在的 DataPoint 合并,其中五个并排显示为一个块,以 X-Values/Labels.[=37= 为中心] 这看起来只适合中间系列,实际上有中间点。
你可以加几个其他的点看看效果..:[=15=]
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++;
}
我正在尝试使用 C# .NET 中的图表控件绘制一组代理任务的范围柱状图。我在 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++;
}
看似 'misalignment' 的原因是您总共添加了五个系列,但每个系列的每个 X 值只有一个(一组)数据点。
然后将这个存在的 DataPoint 与四个不存在的 DataPoint 合并,其中五个并排显示为一个块,以 X-Values/Labels.[=37= 为中心] 这看起来只适合中间系列,实际上有中间点。
你可以加几个其他的点看看效果..:[=15=]
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++;
}