首页 > 解决方案 > 如果没有结果,在图表区域插入标签

问题描述

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

标签: c#winformscharts

解决方案


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

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 ) 会将 top.left 放在图表的中心(不是图表区域)..

注意:要ChartArea显示 及其轴,您仍然需要添加一个虚拟点;Empty如果你愿意就做吧..;

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

推荐阅读