.net 4.0 Microsoft图表堆叠柱形图有间隙

.net 4.0 Microsoft图表堆叠柱形图有间隙,.net-4.0,mschart,.net 4.0,Mschart,我正在使用.NET4.0中的图表库创建包含多个系列的堆叠柱状图。我的目标是一个柱状图,显示几个系列(教师)每天的累计行动数(完成报告)。数据经常丢失(那天某位老师没有活动) 当序列中缺少数据时,我会在条中找到间隙: 我的代码: public ActionResult CompletionHistogram(int sid, int width, int height) { Site site = SiteRepository.Get(sid);

我正在使用.NET4.0中的图表库创建包含多个系列的堆叠柱状图。我的目标是一个柱状图,显示几个系列(教师)每天的累计行动数(完成报告)。数据经常丢失(那天某位老师没有活动)

当序列中缺少数据时,我会在条中找到间隙:

我的代码:

    public ActionResult CompletionHistogram(int sid, int width, int height)
    {
        Site site = SiteRepository.Get(sid);
        if (site == null)
            return new HttpNotFoundResult();

        Chart chart = new Chart();
        chart.Height = height;
        chart.Width = width;
        ChartArea area = chart.ChartAreas.Add("Default");

        // Treat each teacher as a series
        foreach (Teacher t in site.Teachers)
        {
            Series series = chart.Series.Add(t.FullName);
            series.ChartType = SeriesChartType.StackedColumn;
            series.Name = t.FullName;

            // Group completions by day (filter out incomplete reports and null timestamps)
            var groups = t.StudentReports
                .Where<StudentReport>(rep => rep.IsComplete && rep.FirstSaveTimestamp.HasValue)
                .GroupBy<StudentReport, DateTime>(rep => rep.FirstSaveTimestamp.Value.Date);

            bool hasPoints = false;
            foreach (var g in groups)
            {
                series.Points.AddXY(g.Key, g.Count());
                hasPoints = true;
            }

            series.IsValueShownAsLabel = true;
            series.ToolTip = "#VALX";

            if (hasPoints)
                chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Days, series);
        }


        area.AxisX.LabelStyle.Format = "ddd M/d";
        return new ChartResult(chart);
    }
public ActionResult完成直方图(int-sid、int-width、int-height)
{
Site=SiteRepository.Get(sid);
if(site==null)
返回新的HttpNotFoundResult();
图表=新图表();
图表.高度=高度;
图表宽度=宽度;
ChartArea=chart.ChartAreas.Add(“默认”);
//将每位教师视为一个系列
foreach(现场教师t.教师)
{
Series系列=chart.Series.Add(t.FullName);
series.ChartType=SerieChartType.StackedColumn;
series.Name=t.FullName;
//按天分组完成情况(过滤掉不完整的报告和空时间戳)
变量组=t.StudentReports
.Where(rep=>rep.IsComplete&&rep.FirstSaveTimestamp.HasValue)
.GroupBy(rep=>rep.FirstSaveTimestamp.Value.Date);
bool hasPoints=false;
foreach(组中的变量g)
{
AddXY(g.Key,g.Count());
hasPoints=true;
}
series.IsValueShownAsLabel=true;
series.ToolTip=“#VALX”;
如果(hasPoints)
图表.DataManipulator.InsertEmptyPoints(1,IntervalType.Days,系列);
}
area.axix.LabelStyle.Format=“ddd M/d”;
返回新的ChartResult(图表);
}

我怎样才能删除该文件?我有办法使其生效。
很抱歉回答太长,但我发现您尝试实现此答案的方式将影响它是否有效

添加点时,需要手动将点设置为零。 注意:我无法通过在事实之后添加零点来实现这一点

请参见下面的示例和生成的屏幕截图: 图1.Series.Clear(); 图表1.Series.Add(newseries()); 图表1.Series.Add(newseries()); 图表1.Series.Add(newseries()); 图表1.Series.Add(newseries())

“before.png”的图像:

现在删除给定x值下没有数据点的序列的注释:

(注意!我发现如果你在给定的x值处添加点,使y=0的值在最后-也就是在我保存图像之前-这不起作用。序列中的点的顺序似乎对StackedColumn很重要,我从来没有使用过这种类型,除了研究如何回答这个问题,这可能是f的常识。)或此类型的用户)

“after.png”的图像:

因此,考虑到您不能在事件发生后添加零点(尽管您可能可以插入它们?),您需要将代码修改为以下内容:

var allPossibleGroups = t.StudentReports;

var groups = t.StudentReports
            .Where<StudentReport>(rep => rep.IsComplete && rep.FirstSaveTimestamp.HasValue)
            .GroupBy<StudentReport, DateTime>(rep => rep.FirstSaveTimestamp.Value.Date);

        bool hasPoints = false;
        foreach (var g in allPossibleGroups)
        {
            if(groups.ContainsKey(g))
            {
                series.Points.AddXY(g.Key, g.Count());
                hasPoints = true;
            }
            else
            {
                series.Points.AddXY(g.Key, 0);
            }
        }
var allPossibleGroups=t.StudentReports;
变量组=t.StudentReports
.Where(rep=>rep.IsComplete&&rep.FirstSaveTimestamp.HasValue)
.GroupBy(rep=>rep.FirstSaveTimestamp.Value.Date);
bool hasPoints=false;
foreach(所有可能的组中的变量g)
{
if(组。容器(g))
{
AddXY(g.Key,g.Count());
hasPoints=true;
}
其他的
{
系列点AddXY(g键,0);
}
}
抱歉,代码块太长了,但这个示例是演示如何使其工作的必要条件,而不会落入在末尾添加空点(其中y=0)的陷阱,因为这样做行不通


如果您需要更多帮助,请告诉我。

我自己也遇到了同样的问题,希望与大家分享正确的解决方案:

使用数据操纵器插入缺少的(x,y)值:


由于某些原因,环通系列对我来说不起作用,但下一个解决方案很有魅力:

Chart1.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, "Series1, Series2, Series3, Series4")

这实际上为我固定了列宽,但仍然存在差距。
var allPossibleGroups = t.StudentReports;

var groups = t.StudentReports
            .Where<StudentReport>(rep => rep.IsComplete && rep.FirstSaveTimestamp.HasValue)
            .GroupBy<StudentReport, DateTime>(rep => rep.FirstSaveTimestamp.Value.Date);

        bool hasPoints = false;
        foreach (var g in allPossibleGroups)
        {
            if(groups.ContainsKey(g))
            {
                series.Points.AddXY(g.Key, g.Count());
                hasPoints = true;
            }
            else
            {
                series.Points.AddXY(g.Key, 0);
            }
        }
foreach (Series s in chart.Series)
{
    chart.DataManipulator.Sort(PointSortOrder.Ascending, "X", s);
    chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, s);
}
Chart1.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, "Series1, Series2, Series3, Series4")