3、Chart保存
将Chart保存到缓存中:看下面代码:
@{
var chartKey = Request["key"];
if (chartKey != null) { var cachedChart = Chart.GetFromCache(key: chartKey);
if (cachedChart == null)
{
cachedChart = new Chart(600, 400);
cachedChart.AddTitle("Cached Chart -- Cached at " + DateTime.Now);
cachedChart.AddSeries( name: "Employee", axisLabel: "Name", xValue: new[]
{ "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"},
yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"});
cachedChart.SaveToCache(key: chartKey,minutesToCache: 2,slidingExpiration: false);
}
Chart.WriteFromCache(chartKey);
}
}
var chartKey = Request["key"];
if (chartKey != null) { var cachedChart = Chart.GetFromCache(key: chartKey);
if (cachedChart == null)
{
cachedChart = new Chart(600, 400);
cachedChart.AddTitle("Cached Chart -- Cached at " + DateTime.Now);
cachedChart.AddSeries( name: "Employee", axisLabel: "Name", xValue: new[]
{ "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"},
yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"});
cachedChart.SaveToCache(key: chartKey,minutesToCache: 2,slidingExpiration: false);
}
Chart.WriteFromCache(chartKey);
}
}
Chart.GetFromCache(key: chartKey)将根据key从缓存中取出Chart,cachedChart.SaveToCache(key: chartKey,minutesToCache: 2,slidingExpiration: false)是将Chart缓存起来。看下图:
当再次请求的时候,就直接从缓存中取数据。设置缓存两分钟,两分钟之后这次的缓存失效。
将Chart保存为图片:
使用下面代码将图形保存为图片:
@{
var filePathName = "_ChartFiles/chart01.jpg";
if (!File.Exists(Server.MapPath(filePathName)))
{
var chartImage = new Chart(600, 400);
chartImage.AddTitle("Chart Title");
chartImage.AddSeries( name: "Employee", axisLabel: "Name", xValue: new[]
{ "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"}, yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"}); chartImage.Save(path: filePathName);
}
}
var filePathName = "_ChartFiles/chart01.jpg";
if (!File.Exists(Server.MapPath(filePathName)))
{
var chartImage = new Chart(600, 400);
chartImage.AddTitle("Chart Title");
chartImage.AddSeries( name: "Employee", axisLabel: "Name", xValue: new[]
{ "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"}, yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"}); chartImage.Save(path: filePathName);
}
}
保存之后的图片:
将Chart保存为保存为XML:
@{
Chart chartXml;
var filePathName = "_ChartFiles/XmlChart.xml";
if (File.Exists(Server.MapPath(filePathName)))
{
chartXml = new Chart(width: 600,height: 400,templatePath: filePathName);
}
else
{
chartXml = new Chart(width: 600,height: 400);
chartXml.AddTitle("Chart Title -- Saved at " + DateTime.Now);
chartXml.AddSeries(
name: "Employee", axisLabel: "Name", xValue: new[] { "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"}, yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"});
chartXml.SaveXml(path: filePathName);
}
chartXml.Write();
}
Chart chartXml;
var filePathName = "_ChartFiles/XmlChart.xml";
if (File.Exists(Server.MapPath(filePathName)))
{
chartXml = new Chart(width: 600,height: 400,templatePath: filePathName);
}
else
{
chartXml = new Chart(width: 600,height: 400);
chartXml.AddTitle("Chart Title -- Saved at " + DateTime.Now);
chartXml.AddSeries(
name: "Employee", axisLabel: "Name", xValue: new[] { "一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份"}, yValues: new[] { "2", "6", "4", "5", "3","4","9","2","5"});
chartXml.SaveXml(path: filePathName);
}
chartXml.Write();
}
从上面代码我们可以看到,可以通过templatePath将XML转换成Chart。通过SaveXml可以将Chart保存为XML。生成的XML如下:
<Chart Width="600" Height="400">
<Series>
<Series Name="Employee" XValueType="String" YValueType="String" ChartArea="Default" AxisLabel="Name">
<Points>
<DataPoint YValues="2" AxisLabel="一月份" />
<DataPoint YValues="6" AxisLabel="二月份" />
<DataPoint YValues="4" AxisLabel="三月份" />
<DataPoint YValues="5" AxisLabel="四月份" />
<DataPoint YValues="3" AxisLabel="五月份" />
<DataPoint YValues="4" AxisLabel="六月份" />
<DataPoint YValues="9" AxisLabel="七月份" />
<DataPoint YValues="2" AxisLabel="八月份" />
<DataPoint YValues="5" AxisLabel="九月份" />
</Points>
</Series>
</Series>
<ChartAreas>
<ChartArea Name="Default">
</ChartArea>
</ChartAreas>
<Titles>
<Title Name="Title1" Text="Chart Title -- Saved at 2010/10/19 23:41:02">
</Title>
</Titles>
</Chart>
<Series>
<Series Name="Employee" XValueType="String" YValueType="String" ChartArea="Default" AxisLabel="Name">
<Points>
<DataPoint YValues="2" AxisLabel="一月份" />
<DataPoint YValues="6" AxisLabel="二月份" />
<DataPoint YValues="4" AxisLabel="三月份" />
<DataPoint YValues="5" AxisLabel="四月份" />
<DataPoint YValues="3" AxisLabel="五月份" />
<DataPoint YValues="4" AxisLabel="六月份" />
<DataPoint YValues="9" AxisLabel="七月份" />
<DataPoint YValues="2" AxisLabel="八月份" />
<DataPoint YValues="5" AxisLabel="九月份" />
</Points>
</Series>
</Series>
<ChartAreas>
<ChartArea Name="Default">
</ChartArea>
</ChartAreas>
<Titles>
<Title Name="Title1" Text="Chart Title -- Saved at 2010/10/19 23:41:02">
</Title>
</Titles>
</Chart>
总结:本文就三个方面介绍了ASP.NET MVC 3 Beta中的Chart。包括它的数据源配置、显示以及保存。