【问题标题】:ASP.NET Charting Control - Dynamically Adding and Removing Series of DatapointsASP.NET 图表控件 - 动态添加和删除一系列数据点
【发布时间】:2009-10-29 14:31:54
【问题描述】:

如果您熟悉 ASP.NET 的 Charting 控件,Chart 对象包含许多 Series 对象 - 这些是可以绘制图表的数据点系列。每个系列都可以在同一张图表上以不同的方式(条形或点或线)进行可视化。

我有一个自定义控件,用于在 UI 中创建、删除和修改系列列表。单击按钮后,将使用这些系列创建图表。但是,如果我尝试重新显示图表(即使系列相同),它也会爆炸并抛出 NullReferenceException。

这是相关代码 - 我有一个对象包装系列(因为我有一些自定义属性)

public class DataSeries
{
    private Series _series = new Series();
    ... (bunch of other properties)
    public Series Series
    {
        get { return _series; }
        set { _series = value; }
    }
}

在控件本身中,我将这些列表存储为属性(我只在非回发期间创建对象,因为我希望列表保持不变):

private static List<DataSeries> seriesList;

public List<DataSeries> ListOfSeries
    {
        get { return seriesList; }
        set { seriesList = value; }
    }

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            seriesList = new List<DataSeries>();
        }
    }

然后我有一些控件的组合,我可以使用这些控件来添加新的 DataSeries、删除它们并修改它们的属性。一切正常。

当他们点击页面上的“创建图表”时,会执行以下代码:

protected void refreshChart(object sender, EventArgs e)
{
    chart.Series.Clear();

        foreach (DataSeries s in seriesControl.ListOfSeries)
        {
            string propertyName = s.YAxisProperty;

            //List of data to display for this series 
            List<Sampled_Data> sampleList = Sample.GetSamplesById(s.ComponentId);

            foreach (Sampled_Data dSample in sampleList)
            {
                //GetPropertyValue returns a float associated with the propertyname
                //selected for displaying as the Y Value
                s.Series.Points.AddY(BindingLib.GetPropertyValue(dSample, propertyName));
            }
            chart.Series.Add(s.Series);
        }
    }

我第一次执行那段代码时,它就像一个魅力。 第二次单击执行“refreshChart”的按钮时,我得到一个 NullReferenceException,因为“s.Series.Points”的值为 null。而且我无法创建 Points 属性类型的新对象 - 它的构造函数是私有的或受保护的。

如果我没有在此函数的后续调用之间操作 Points 属性,为什么它会变为 null?

我可能会想到一些解决方案 - 让 DataSeries 继承系列而不是拥有一个 - 如果错误仍然存​​在,我很可能可以新建 Points 属性。我也可以深拷贝 ListOfSeries 看看是否能解决我的问题。我也可以将我所有的自定义属性推到 Series 对象中——它有一个 customfields 属性(或类似名称的东西)。如果我没有传递包裹另一个对象的对象,那可能会消除问题。

任何想法为什么会发生这种情况以及如何解决?

【问题讨论】:

    标签: c# asp.net data-binding charts


    【解决方案1】:

    我不是 100% 确定,但我怀疑这条线

    chart.Series.Clear();
    

    直接清除你的静态成员seriesList的内部Series 因为这条线

    chart.Series.Add(s.Series);
    

    只需将对 Series 的引用传递给图表。

    我认为这不应该发生:您可能在其他地方自己填写(并清除)该成员。

    您可以通过删除 getter 来检查这个假设:

    set { seriesList = value; }
    

    此时代码应该在

    处引发异常
    chart.Series.Clear();
    

    当您刷新时:不允许图表再清除您的静态成员seriesList 中的Series

    您使用 static 成员有什么原因吗?

    如果不是this answer,那么通过您的静态成员提供系列的深层副本并将其传递到图表将起作用,而且不幸的是Series 未标记为 [Serializable]。

    // Do you want to write this Property? copying all fields of Series Manually?
    chart.Series.Add(s.DeepCopyOfMySeries); 
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多