我正在使用.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);
}
如何删除
答案 0 :(得分:9)
我有办法让这项工作 很抱歉这个答案很长,但我发现你尝试实施这个答案的方式会影响它是否有效。
您需要在添加点时手动将点设置为零。 注意:我无法通过在事实之后添加零点来完成这项工作。
请参阅示例和结果屏幕截图旁边的内容: 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, 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”的图片:
那么,鉴于您无法在事后添加零点(虽然您可以插入它们?),您需要将代码修改为:
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)的陷阱,因为这将无效。 / p>
如果您需要更多帮助,请与我联系。
答案 1 :(得分:6)
自己陷入同样的问题,并希望分享正确的解决方案:
使用DataManipulator插入缺少的(x,y)值:
foreach (Series s in chart.Series)
{
chart.DataManipulator.Sort(PointSortOrder.Ascending, "X", s);
chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, s);
}
答案 2 :(得分:0)
由于某些原因,循环播放系列并不适合我,但下一个解决方案就像魅力一样:
Chart1.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, "Series1, Series2, Series3, Series4")