Charts Controls 开发系列2
系列目录:
- 创建Basic Chart -- Completed
- 了解Chart State Management -- Completed
- Chart 外观设置 -- ing
- Chart 数据加载
- 。。。
了解 Chart State Management
当我们在使用图表控件开发时,页面经常进行一些postback的操作。如果不了解有关于Chart 的状态管理的方面知识,肯定会遇到问题的。
比如说:页面postback之后,图表数据丢失问题?。。。
那如何存储页面上的图表相关数据呢?
让我们先来了解以下内容
- SerializationContents
- ChartSerializer
- ViewStateData
SerializationContents
主要作用:定义图表序列化的类型
这个Enum有四种
1.Appearance
仅序列化与图表外观有关系的数据内容,如:图表的线的颜色,图表的宽度,高度。。
2.Data
仅序列化图表中的Series,不包括与图表外观相关的数据
3.Default
仅序列化非默认的图表数据 如:图表数据,图表外表数据
4.All
序列化图表中所有的数据
我们来看看指定不同序列化类型之后生成数据
Appearance:
//仅序列化图表不包括ChartAreas
<Chart BackColor="211, 223, 240" BackGradientStyle="TopBottom"BackSecondaryColor="White" BorderColor="26, 59, 105" BorderWidth="2">
<BorderSkin SkinStyle="Emboss" />
</Chart>
Data
<Chart>
<Series>
<Series Name="Google" XValueType="DateTimeOffset" YValueType="Int32">
<Points>
<DataPoint YValues="3" AxisLabel="2010-03-12" />
</Points>
</Series>
</Series>
</Chart>
NonSerializableContent
语法:"Class.Property[,Class.Property]".
// 设置 有关于图表的BorderColor不进行序列化
resultChart.Serializer.NonSerializableContent = *.BorderColor";
ChartSerializer
主要作用:用于管理用户自定义的View State.
Serializer Data Format一般分四个种 Stream、String、XmlWriter、TextWriter
1.Stream
MemoryStream stream = new MemoryStream();
resultChart.Serializer.Save(stream);
resultChart.Serializer.Load(stream);
2.String
resultChart.Serializer.Save("ChartData.xml");
resultChart.Serializer.Load("ChartData.xml");
3.XmlWriter
XmlTextWriter xmlWriter = new XmlTextWriter("c:\\ChartData.xml", Encoding.ASCII);
resultChart.Serializer.Save(xmlWriter);
XmlTextReader xmlReader = new XmlTextReader("c:\\ChartData.xml");
resultChart.Serializer.Load(xmlReader);
4.TextWriter
private void SetDataToViewStateData () {
using ( var sw = new StringWriter () ) {
resultChart.Serializer.Content = SerializationContents.Data;
resultChart.Serializer.Save ( sw );
resultChart.ViewStateData = sw.ToString ();
}
}
private void GetDataFromViewStateData () {
try {
if ( null != resultChart.ViewStateData ) {
using ( var sr = new StringReader ( resultChart.ViewStateData ) ) {
resultChart.Serializer.Content = SerializationContents.Data;
resultChart.Serializer.Load ( sr );
}
}
}
catch ( Exception ) {
// Data Bind Again
SetDataToViewStateData ();
}
}
ViewStateData
此属性在Framework 3.5中主要是存储序列化数据,而在Framework 4.0中已经Obsolete。
MSDN: "ViewStateData has been deprecated. Please investigate Control.ViewState instead."
》Framework 3.5
using ( var sw = new StringWriter () ) {
resultChart.Serializer.Content = SerializationContents.Data;
resultChart.Serializer.Save ( sw );
resultChart.ViewStateData = sw.ToString ();
}
》Framework 4.0
Set:
using ( var sw = new StringWriter () ) {
resultChart.Serializer.Content = SerializationContents.All;
resultChart.Serializer.Save ( sw );
//resultChart.ViewStateData = sw.ToString ();
ViewState.Add ( "chartViewState" , sw.ToString () );
}
Get:
var query = from vs in ViewState.Keys.OfType<String> ()
where vs.Contains ( "chartViewState" )
select ViewState [vs] as String;
if ( null != query.Single () ) {
using ( var sr = new StringReader ( query.Single () ) ) {
resultChart.Serializer.Content = SerializationContents.All;
resultChart.Serializer.Load ( sr );
}
}
*当然除以上的方式还可以使用其他的方式维护数据状态.
提示
使用State management时,要记得设置EnableViewState属性为true,此属性默认设置为false.
嗯,差不多了。