in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

Alan's place

The XtraCharts Suite: Saving Layouts and State (Feature Preview)

I want to spend a few minutes describing the ways in which the XtraChart's layout and state can be saved in our forthcoming release v2006.2. Note: As always, all API and feature implementations discussed herein are subject to change, but I expect no changes to this API and you should look forward to using it once the next update is issued.

Saving the layout of a ChartControl

The XtraCharts Suite v2006.2 introduces four new methods for the ChartControl. They are declared as follows:

public void SaveLayoutToStream(Stream stream);
public void SaveLayoutToFile(string path);
public void RestoreLayoutFromStream(Stream stream);
public void RestoreLayoutFromFile(string path);

The code listed below demonstrates the use of these methods.

using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Design;
// …

string filePath = Application.StartupPath + @"\..\..\Charts\myChart.xml";

private void btnCreateChart_Click(object sender, EventArgs e) {
    ChartControl myChart = new ChartControl();
    ChartWizard.Run(myChart, null);
    myChart.SaveLayoutToFile(filePath);
}

Once saved, this layout can be restored for use at a later time and shown within a form, printed, or exported to one of the available formats.

private void btnShowChart_Click(object sender, EventArgs e) {
    ChartControl chart1 = new ChartControl();
    chart1.RestoreLayoutFromFile(filePath);

    Form form1 = new Form();
    form1.Controls.Add(chart1);
    chart1.Dock = DockStyle.Fill;

    form1.Show();
}

private void btnPrintChart_Click(object sender, EventArgs e) {
    ChartControl chart1 = new ChartControl();
    chart1.RestoreLayoutFromFile(filePath);
    chart1.ShowPrintPreview();
}

private void btnExportChart_Click(object sender, EventArgs e) {
    ChartControl chart1 = new ChartControl();
    chart1.RestoreLayoutFromFile(filePath);
    chart1.ExportToPdf("Chart.pdf");   
}

That's about all you'll need to do to implement this new capability in your application once v2006.2 is released.

ViewState for the WebChartControl

With XtraCharts v2006.2 you're able to activate save/restore for the WebChartControl's ViewState. I think that most of you who are developing projects in ASP .NET are familiar with the EnableViewState property. For those that are not, the following information from MSDN may be of use to you for familiarization purposes:

"Control.EnableViewState property
Gets or sets a value indicating whether the server control persists its view state, and the view state of any child controls it contains, to the requesting client."

Hence, this feature enables you to save the chart's state between round trips to a client. This means that the state of a WebChartControl is persisted to a string variable and sent to the client and back as a hidden variable. Upon postback, the page framework parses the input string from the hidden variable and populates the ViewState property of a WebChartControl. To help clarify the benefits of ViewState, let's review the following simple example:

A web page contains a WebChartControl and a Button. Initially, the chart is empty, and every time an end-user clicks the button, a random series point is added to the chart's series. Note that after a point has been added, a page is reloaded, hence it's necessary to enable the ViewState for the WebChartControl, so as to persist all points which were added previously.

This code enables the ViewState for the WebChartControl:

protected void Page_Load(object sender, EventArgs e) {
    WebChartControl1.EnableViewState = true;
}

And the following demonstrates how a series point with the current time as an argument and a random value (1; 10) can be added to a web chart.

protected void Button1_Click(object sender, EventArgs e) {
    Random random = new Random();
    double value = Math.Round(random.NextDouble() * 10, 1);

    SeriesPoint point = new SeriesPoint(DateTime.Now, new double[] { value });

    WebChartControl1.Series[0].Points.Add(point);
}

Note that this code can be different in a real-life application in that you can add any point or make any other changes to the chart on the Button.Click event. The main idea is that after a chart has been changed, the page is reloaded and the last change is applied to the chart along with all the previous changes.

That's how saving/restoring a chart will work in XtraCharts v2006.2.

Comments are welcome!

Published Jun 06 2006, 02:22 AM by Alan (Developer Express)

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add

About Alan (Developer Express)

Joined Developer Express on August 22, 2003
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED