微软图表堆积柱形图有差距 [英] Microsoft chart stacked column chart has gaps

查看:378
本文介绍了微软图表堆积柱形图有差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是在.NET 4.0中图表库创建一个堆积柱形图与几大系列。我的目标是一个柱状图,显示了行动的报告(完工)每天在多个系列(教师)的累计数。经常有丢失数据(这一天由一个特定的老师没有任何活动)。

I'm using the Chart library in .Net 4.0 to create a stacked column chart with several series. My goal is a histogram that shows the cumulative number of actions (report completions) per day across several series (teachers). There is often missing data (no activity that day by a particular teacher).

我得到在酒吧的差距时,有一系列数据丢失:

I get gaps in the bars when there is missing data in a series:

我的code:

    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);
    }

我如何删除

推荐答案

我有办法,使这项工作。
很抱歉的长期答案,但我发现,你尝试实现这个答案,如果它工作与否将影响的方式。

I have a way to make this work.
Sorry for the long answer, but I found the manner with which you attempt to implement this answer will affect if it works or not.

您需要设置点手动零作为要添加点。 注:我是不是能够通过事后添加的零点,使这项工作

You need to set the point to zero manually as you are adding points. Note: I was not able to make this work by adding the zero points after the fact.

见旁边的例子和产生的截图:     chart1.Series.Clear();     chart1.Series.Add(新系列());     chart1.Series.Add(新系列());     chart1.Series.Add(新系列());     chart1.Series.Add(新系列());

See the next to examples and resulting screenshots: chart1.Series.Clear(); chart1.Series.Add(new Series()); chart1.Series.Add(new Series()); chart1.Series.Add(new Series()); chart1.Series.Add(new Series());

foreach (Series s in chart1.Series)
{
    s.ChartType = SeriesChartType.StackedColumn;
}

//chart1.Series[0].Points.Add(new DataPoint(0, 0));
chart1.Series[0].Points.Add(new DataPoint(1, 3));
chart1.Series[0].Points.Add(new DataPoint(2, 3));
chart1.Series[0].Points.Add(new DataPoint(3, 3));

chart1.Series[1].Points.Add(new DataPoint(0, 3));
//chart1.Series[1].Points.Add(new DataPoint(1, 0));
chart1.Series[1].Points.Add(new DataPoint(2, 3));
chart1.Series[1].Points.Add(new DataPoint(3, 3));

chart1.Series[2].Points.Add(new DataPoint(0, 3));
chart1.Series[2].Points.Add(new DataPoint(1, 3));
//chart1.Series[2].Points.Add(new DataPoint(2, 0));
chart1.Series[2].Points.Add(new DataPoint(3, 3));

chart1.Series[3].Points.Add(new DataPoint(0, 3));
chart1.Series[3].Points.Add(new DataPoint(1, 3));
chart1.Series[3].Points.Add(new DataPoint(2, 3));
//chart1.Series[3].Points.Add(new DataPoint(3, 0));

chart1.SaveImage("C:\\Before.png", ChartImageFormat.Png);

before.png的形象:

Image of "before.png":

现在删除该系列没有数据点在给定的x值的评论:

Now remove the comments for the series with no data points at a given x value:

(请注意,我发现,如果你在的值给定的x值加点,你让Y = 0在最后这是行不通的! - 又名之前,我保存图像顺序之分,在系列。似乎事项StackedColumn,我从来没有这种类型,除了研究如何回答这个问题,这样可能是常识的用户来说,这类型的)

(Note! I found it does not work if you add the points at a given x-value for values where you make y = 0 at the end - aka right before I save the image. Order of the points in the series appears to matter for StackedColumn, I have never worked with this type except for investigating how to answer this question so that might be common knowledge for users of this type)

chart1.Series.Clear();
chart1.Series.Add(new Series());
chart1.Series.Add(new Series());
chart1.Series.Add(new Series());
chart1.Series.Add(new Series());

foreach (Series s in chart1.Series)
{
    s.ChartType = SeriesChartType.StackedColumn;
}

chart1.Series[0].Points.Add(new DataPoint(0, 0));
chart1.Series[0].Points.Add(new DataPoint(1, 3));
chart1.Series[0].Points.Add(new DataPoint(2, 3));
chart1.Series[0].Points.Add(new DataPoint(3, 3));

chart1.Series[1].Points.Add(new DataPoint(0, 3));
chart1.Series[1].Points.Add(new DataPoint(1, 0));
chart1.Series[1].Points.Add(new DataPoint(2, 3));
chart1.Series[1].Points.Add(new DataPoint(3, 3));

chart1.Series[2].Points.Add(new DataPoint(0, 3));
chart1.Series[2].Points.Add(new DataPoint(1, 3));
chart1.Series[2].Points.Add(new DataPoint(2, 0));
chart1.Series[2].Points.Add(new DataPoint(3, 3));

chart1.Series[3].Points.Add(new DataPoint(0, 3));
chart1.Series[3].Points.Add(new DataPoint(1, 3));
chart1.Series[3].Points.Add(new DataPoint(2, 3));
chart1.Series[3].Points.Add(new DataPoint(3, 0));

// If you add the empty points here, it does not seem to work.  
// Empty points are as follows, and are already added above in the 'after' example.
// chart1.Series[0].Points.Add(new DataPoint(0, 0));
// chart1.Series[1].Points.Add(new DataPoint(1, 0));
// chart1.Series[2].Points.Add(new DataPoint(2, 0));
// chart1.Series[3].Points.Add(new DataPoint(3, 0));

chart1.SaveImage("C:\\After.png", ChartImageFormat.Png);

after.png的形象:

Image of "after.png":

所以,既然你不能在事后添加的零点(尽管你可以将它们插入?),你将需​​要修改code到这样的事情:

So, given that you can not add the zero points after the fact (although you may be able to insert them?) you will need to modify your code to something like this:

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);
            }
        }

抱歉长code块,但这个例子是要说明如何使它发挥作用,而不会进入加空点的陷阱(其中y = 0)的结尾,就不会的工作。

Sorry for the long code blocks, but the example was necessary to demonstrate how to make it work, without falling into the trap of adding the empty points (where y = 0) at the end, as that will not work.

让我知道如果你需要更多的帮助。

Let me know if you need more help.

这篇关于微软图表堆积柱形图有差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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