如果没有结果,则在图表区插入标签



我正在使用 C# Windows 窗体图表,我想知道当图表中没有任何结果时,是否可以在图表区显示一个大标签,上面写着"无结果"。

这将添加一个文本注释。每次添加/删除/绑定点时都需要调用它。

void testEmpty(Chart chart)
{
bool empty = true;
foreach (var s in chart.Series)
{
if (s.Points.Any(x => !x.IsEmpty)) {empty = false; break; }
}
if (chart.Annotations.Contains(chart.Annotations["Empty"]))
chart.Annotations.Remove(chart.Annotations["Empty"]);
if (empty)
{
TextAnnotation ta = new TextAnnotation();
ta.Name = "Empty";
ta.X = 30;
ta.Y = 45;
ta.Text = "No Data!";
ta.Font = new Font(Font.FontFamily, 30f);
chart.Annotations.Add(ta);
}
}

你可以玩数字;(50, 50 ( 会将左上角放在图表的中心(不是图表区域(。

注意:要显示ChartArea及其轴,您仍然需要添加一个虚拟点; 如果需要,请将其设为Empty..;

chart.Series.First().Points.Add(new DataPoint() { IsEmpty = true });

最新更新