在C#中如何使用teechart组件读取并绘制datatable中的数据并绘图? 100
2018-08-20 · 百度认证:云南新华电脑职业培训学校官方账号
1.添加teechart到工具箱。
步骤 工具箱上点右键,添加选项卡->选择项->选择teechart.dll确定即可。可以同时添加language.dll
2. 新建一个aspx页面,拖一个teechart到页面中,设置属性并添加个Button。代码如下:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="TeeChart" Namespace="Steema.TeeChart.Web" TagPrefix="tchart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<tchart:WebChart ID="WebChart1" runat="server" Width="800px" Height="600px" GetChartFile="GetChart.aspx" TempChart="Session" />
<asp:Button ID="Button1" runat="server" Text="确定" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
3.假如我们想画一个折线图,添加后台代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Steema.TeeChart.Styles;//方便添加各种类型的曲线
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = GetData();//获取数据
Line line = new Line();//teechart中的折线图
foreach (DataRow dr in dt.Rows)//添加数据
{
line.Add(Convert.ToInt32(dr[0]), Convert.ToDouble(dr[1]));
}
WebChart1.Chart.Series.Add(line);//所有的曲线必须添加到 series中
WebChart1.Chart.Aspect.View3D = false;//2D图片,否则是3D的
}
public DataTable GetData()//产生数据写的稍微有些麻烦
{
DataTable dt = new DataTable();
DataColumn dc0 = new DataColumn("Year");
DataColumn dc1 = new DataColumn("Value");
dt.Columns.Add(dc0);
dt.Columns.Add(dc1);
Random r = new Random();
Hashtable hashtable = new Hashtable();
int RowNum = 10;
for (int i = 0; hashtable.Count < RowNum; i++)
{
int value = r.Next(1, 50);
if (!hashtable.Contains(value))
{
hashtable.Add(i, value);
}
}
ArrayList keys = new ArrayList();
foreach (int key in hashtable.Keys)
{
keys.Add(key);
}
for (int i = 2000; i < 2010; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = hashtable[keys[i-2000]];
dt.Rows.Add(dr);
}
return dt;
}
}
4.说明 teechart中有GetChartFile="GetChart.aspx" TempChart="Session" 这两个属性是必须设置。否则图片将是一个X。
GetChart.aspx是个固定的页面什么原理没有研究,贴出代码如下
前台:<%@ Page language="c#" Inherits="WebForm.GetChart" CodeFile="GetChart.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD html 4.0 Transitional//EN" >
<html>
<head runat="server">
<title>GetChart</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form method="post" runat="server">
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebForm
{
/// <summary>
/// Summary description for GetChart.
/// </summary>
public partial class GetChart : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// *************************************************************
// Code to retrieve Session saved streamed Charts to a WebForm.
// This code should be included if the WebChart 'UseStreams' property is set to True.
// *************************************************************
string chartName=Request.QueryString["Chart"];
if (Session[chartName]!=null)
{
MemoryStream chartStream = new MemoryStream();
chartStream=((MemoryStream)Session[chartName]);
Response.OutputStream.Write(chartStream.ToArray(),0,(int)chartStream.Length);
chartStream.Close();
Session.Remove(chartName);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
运行之后效果如下所示:
2018-06-15 · 百度认证:云南新华电脑职业培训学校官方账号
网页链接这个网站您看一下有没有嫩给解决到您的问题