The page method checks whether the current requestfor the page postback
Chart State Management ❘689
private void GenerateRandomData(Series series)
{
const double MaxStep = 5.0;
series.Points.Clear();
var rand = new Random((int)DateTime.Now.Ticks);
var lastValue = 5.0;
for (int i = 0; i < 50; i++)
{
series.Points.AddXY(i + 1, lastValue);
lastValue += MaxStep * rand.NextDouble() - MaxStep/2; series.Points[i].YValues[0] = lastValue;
}
}private void AddNewSeries(string name, double distance)
{
RemoveSeries(name);
var baseSeries = Chart1.Series[“Default”];
var newSeries = new Series(name);
newSeries.ChartType = SeriesChartType.Spline;
newSeries.BorderWidth = baseSeries.BorderWidth;
Chart1.Series.Add(newSeries);
foreach (var point in baseSeries.Points)
{
newSeries.Points.AddXY(point.XValue, point.YValues[0] + distance);
}
}
continues
The key to the solution is the Form_Init method that turns on chart state management by setting the EnableViewState property to true . The Page_Load method checks whether the current request
for the page is a postback. If it is the fi rst call, the random series is generated. After the default series is either generated or loaded from the view state, the upper and lower splines are drawn according to the state of checkboxes.
Advanced Chart State Management
The BasicChartStateManagement.aspx page saves more state information than required, because it saves the state of the additional series that can be calculated (and it ’ s cheap) from the data generated for the base series. If there were a way you could tell the chart to save only the base series, you could save resources reserved by about a hundred data points. This is especially important when your chart contains more — let ’ s says a few hundred or thousand — data points.