如何消除堆叠条中的间隙 [英] How to remove gaps in stacked bar

查看:17
本文介绍了如何消除堆叠条中的间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C# 制作甘特图,我使用的是 StackedBar 图表类型.此图表的目标是显示如何根据机器"的数量安排任务".在我的算法中,图表上的任务"之间不应该有空闲空间.每个任务"都作为新系列添加.在第一个栏上,它像它应该的那样工作,但在其他栏上,任务"在前一个栏结束后开始.我需要帮助来消除这些差距.有些任务需要在两台机器上分配,当我这样做时,这个任务会像它应该的那样显示在第二列,从栏的开始.

我试图添加零数据点,就像堆栈上的一些帖子中建议的那样,但对我来说没有帮助.(

-- 编辑--

按要求提供数据图表:http://i.imgur.com/64X3SNy.png

堆叠条形图会将所有系列彼此堆叠在一起,为此它需要所有 x 值 (*) 的数据点每个系列的强>.所以图表中的差距来自数据点的差距.

要获得您可能想要的结果:

你需要用 0 值填充所有缺失的点.我在列表 points 中使用了类 daz 而不是你的 items,但你会得到点"..:

 points.Add(new daz("0", 0, 3));点.添加(新daz(0",1,0));//<-填充物点.添加(新daz(0",2,0));//<-填充物点.添加(新daz(1",0,7));点.添加(新daz(1",1,0));//<-填充物点.添加(新daz(1",2,0));//<-填充物点.添加(新daz(2",0,1));点.添加(新daz(2",1,0));//<-填充物点.添加(新daz(2",2,0));//<-填充物点.添加(新daz(3",0,1));点.添加(新daz(3",1,7));点.添加(新daz(3",2,0));//<-填充物点.添加(新daz(4",0,0));//<-填充物点.添加(新daz(4",1,3));点.添加(新daz(4",2,0));//<-填充物点.添加(新daz(5",0,0));//<-填充物点.添加(新daz(5",1,1));点.添加(新daz(5",2,0));//<-填充物点.添加(新daz(6",0,0));//<-填充物点.添加(新daz(6",1,1));点添加(新 daz(6",2,2));点.添加(新daz(7",0,0));//<-填充物点.添加(新daz(7",1,1));点添加(新 daz(7",2,3));点.添加(新daz(8",0,0));//<-填充物点.添加(新daz(8",1,0));//<-填充物点添加(新 daz(8",2,2));点.添加(新daz(9",0,0));//<-填充物点添加(新 daz(9",1,0));//<-填充物点添加(新 daz(9",2,3));

根据您的数据结构和来源,您应该在收集数据时执行此操作,或者从预填充零的数据结构开始,或者稍后使用一点 linq 来填补空白..

(*) 注意 xy 在条形图中是在视觉上切换的,所以 x 轴垂直 在这里!

I'm trying to make gantt chart in C#, I'm using StackedBar chart type. The goal of this chart is to show, how "tasks" can be schedule on number of "machines". In my algorithm there shouldn't be free spaces between "tasks" on chart. Each "task" is added as new series. On first bar it's working like it should be, but in others "task" starts after ending on the previous bar. I need help with removing these gaps. Some task need to be divided on two machines, and when I do it, then this task is showing on second column like it should, from the begining of bar.

I was trying to add zero DataPoints like suggested in some post on stack, but it didn't help in my case. (Microsoft chart stacked column chart has gaps)

Below is my code to create this chart:

foreach (var item in tasks)
{
     scheduleChart.Series.Add(item.Name);
     scheduleChart.Series[item.Name].ChartType = SeriesChartType.StackedBar;
     scheduleChart.Series[item.Name].LabelForeColor = Color.White;
}

for (int i = 0; i < mcNaugthon.Machines.Count; i++)
{
     foreach (var item in mcNaugthon.Machines[i].Tasks)
     {
          scheduleChart.Series[item.Name].Points
              .Add(new DataPoint(i, item.Time));
          scheduleChart.Series[item.Name].Label = item.Name;    
      }
 }

-- edit --

As requested chart with data: http://i.imgur.com/64X3SNy.png

解决方案

A stacked bar chart will stack all series right upon each other and to do so it expects data points for all x-values (*) of each series. So the gaps in your chart come from gaps in the data points.

To get the result you probably want:

you need to fill in all the missing points with values of 0. I have used a class daz in a list points instead of your items, but you will get the 'point'..:

    points.Add(new daz("0", 0, 3));
    points.Add(new daz("0", 1, 0));  //<-filler
    points.Add(new daz("0", 2, 0));  //<-filler
    points.Add(new daz("1", 0, 7));
    points.Add(new daz("1", 1, 0));  //<-filler
    points.Add(new daz("1", 2, 0));  //<-filler
    points.Add(new daz("2", 0, 1));
    points.Add(new daz("2", 1, 0));  //<-filler
    points.Add(new daz("2", 2, 0));  //<-filler
    points.Add(new daz("3", 0, 1));
    points.Add(new daz("3", 1, 7));
    points.Add(new daz("3", 2, 0));  //<-filler
    points.Add(new daz("4", 0, 0));  //<-filler
    points.Add(new daz("4", 1, 3));
    points.Add(new daz("4", 2, 0));  //<-filler
    points.Add(new daz("5", 0, 0));  //<-filler
    points.Add(new daz("5", 1, 1));
    points.Add(new daz("5", 2, 0));  //<-filler
    points.Add(new daz("6", 0, 0));  //<-filler
    points.Add(new daz("6", 1, 1));
    points.Add(new daz("6", 2, 2));
    points.Add(new daz("7", 0, 0));  //<-filler
    points.Add(new daz("7", 1, 1));
    points.Add(new daz("7", 2, 3));
    points.Add(new daz("8", 0, 0));  //<-filler
    points.Add(new daz("8", 1, 0));  //<-filler
    points.Add(new daz("8", 2, 2));
    points.Add(new daz("9", 0, 0));  //<-filler
    points.Add(new daz("9", 1, 0));  //<-filler
    points.Add(new daz("9", 2, 3));

Depending on your data structure and source you should either do it when collecting the data or start out with a zero-pre-filled data structure or maybe use a little linq to fill in the gaps later..

(*) Note that x and y are switched visually in bar charts, so the x-axis is vertical here!

这篇关于如何消除堆叠条中的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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