.net-4.0 – Microsoft图表堆积柱形图存在差距

前端之家收集整理的这篇文章主要介绍了.net-4.0 – Microsoft图表堆积柱形图存在差距前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用.Net 4.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);
    }

我怎么能删除

我有办法让这项工作.
对不起,答案很长,但我发现你尝试实施这个答案的方式会影响它是否有效.

添加点时,需要手动将点设置为零.
注意:我无法通过在事后添加零点来完成这项工作.

请参阅示例和结果屏幕截图旁边的内容
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”的图片

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

(注意!我发现如果你在给定y值为0的值的最后给你的值添加点也不起作用 – 也就是在我保存图像之前.系列中点的顺序似乎很重要对于StackedColumn,我从来没有使用过这种类型,除了调查如何回答这个问题,这对于这类用户来说可能是常识)

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,3));
chart1.Series[1].Points.Add(new DataPoint(1,3));
chart1.Series[2].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”的图片

那么,鉴于您无法在事后添加零点(尽管您可以插入它们?),您需要将代码修改为以下内容

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

对于长代码块感到抱歉,但是示例是必要的,以演示如何使其工作,而不会陷入在末尾添加空点(其中y = 0)的陷阱,因为这将无效.

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

原文链接:/windows/364128.html

猜你在找的Windows相关文章