想做一个曲线图,根据数据库里的数据变化。不知道怎么做,连这方面的资料都没有。希望高人指点阿
解决方案 »
- 如何取出datagridview的值然后插入到access
- C#当选中DATAGRIDVIEW中某一行时的颜色怎么改变
- GetWindowModuleFileName 为什么总是取得当前窗口的路径?
- 怎样让LinkLable不会得到焦点?
- DataGrid的数据绑定问题数据提交,给出解决方法的另起一贴给分(30)
- WinForm的DataGrid中加入CheckBox的方法?
- 弱问:分是做什么用的?散分,送分是怎么回事?
- 求.net2003工具详细使用方法和技巧?
- 怎么播放音频文件!??
- ADO.NET直接联paradox的连接字符串(特急)
- 谁帮我解决下,急!!!
- datagridview中如何合并两个DataTable
或是可以用一些第三方的组件.如可以用owc试试.
在实际项目中我们经常需要绘制一些实时的数据图片,比如当前各公司的用水量、用电量还有播放声音视频时实时显示当前的声频等等,在我们最熟悉的任务管理器也有这么一个功能,用来表示当前CPU的使用频率,最近笔者刚刚给朋友完成了一个类似的功能图,用曲线图来实时表示一些实际数据,由于形象直观,很受客户欢迎。不过由于某些原因,本人不能将实际项目中的代码拿出来给大家分享,只能模拟了一个简单的实现,代码没有过多优化,所以还存在很多可以优化的地方,希望有兴趣的朋友自己完善。
using Microsoft.Office.Interop.Owc11;//添加Office组件引用public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ //连接数据库并获取特定字符串
string strSeriesName = "图例1";
string ConnectString = "Server=Reba_li;DataBase=www;Uid=sa;Pwd=123";
string Sql = "SELECT month,Allcount FROM Chart";
SqlConnection myConn = new SqlConnection(ConnectString);
myConn.Open();
SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
DataSet ds = new DataSet();
Da.Fill(ds); //存放月
string[] MonNum = new string[12];
//存放数据
string[] MonCount = new string[12];
//为数组赋值
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
MonNum[i] = ds.Tables[0].Rows[i][0].ToString();
MonCount[i] = ds.Tables[0].Rows[i][1].ToString();
}
//为x轴指定特定字符串,以便显示数据
string strXdata = String.Empty;
foreach (string strData in MonNum)
{
strXdata += strData + "\t";
}
string strYdata = String.Empty;
//为y轴指定特定的字符串,以便与x轴相对应
foreach (string strValue in MonCount)
{
strYdata += strValue + "\t";
} //创建ChartSpace对象来放置图表
ChartSpace laySpace = new ChartSpaceClass(); //在ChartSpace对象中添加图表
ChChart InsertChart = laySpace.Charts.Add(0); //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
// InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
// InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图 //指定图表是否需要图例标注
InsertChart.HasLegend = false;
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = "2008年alex每个月花销流水账";//标题名称 //为x,y轴添加图示说明
InsertChart.Axes[0].HasTitle = true;
InsertChart.Axes[0].Title.Caption = "";//月份
InsertChart.Axes[1].HasTitle = true;
InsertChart.Axes[1].Scaling.SplitMinimum = 200;
InsertChart.Axes[1].Title.Caption = "数量"; //添加一个series系列
InsertChart.SeriesCollection.Add(0); //给定series系列的名字
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName); //给定分类
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata); //给定值
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
//输出文件.
string strAbsolutePath = (Server.MapPath(".")) + "\\ShowData.gif";
laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250); //创建GIF文件的相对路径.
string strRelativePath = "./ShowData.gif"; //把图片添加到placeholder中,并在页面上显示
string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
}