ASP.NET Chart Management - Dynamically Add or Remove a Data Series

If you are familiar with ASP.NET chart controls, the Chart object contains several series objects — these are series of data that you can chart. Each series can be visualized differently (line or point or line) in one diagram.

I have a custom control that I use to create and delete and modify Series lists in the user interface. When a button is clicked, a chart is created using these Series. However, if I try to redisplay the diagram (even with identical series), it explodes and throws a NullReferenceException.

Here is the relevant code - I have a series of wrapping objects (because I have some custom properties)

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 , ?

, , , , - DataSeries , - , , Points, . ListOfSeries , . , , Series - customfields ( - ). , , .

, ?

+3
1

100%, ,

chart.Series.Clear();

Series seriesList

chart.Series.Add(s.Series);

Series .

, : , , ( ) .

, :

set { seriesList = value; }

chart.Series.Clear();

: Series seriesList .

, ?

, , , , , , Series [Serializable].

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

!

+5

Source: https://habr.com/ru/post/1721400/


All Articles