我使用MS图表以图形方式显示静态数据。它的.net框架3.5 VS2008。在初始阶段,我认为它很简单,我实现它。但我们有这么多的JS库和现代的东西..有没有更好的方法来做...代码片段如下
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp"%>
<!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>
<table width="100%">
<tr>
<td>
<asp:Button ID="btnFindXcell" runat="server" Text="Create Chart" OnClick="btnFindXcell_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="dgvEmployee" runat="server">
</asp:GridView>
<asp:Chart ID="Chart1" runat="server" Width="800px">
<series>
<asp:Series Name="Series1"></asp:Series>
</series>
<chartareas>
<asp:ChartArea BorderDashStyle="Solid" Name="ChartArea1"></asp:ChartArea>
</chartareas>
</asp:Chart>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Data.OleDb;
using Excel;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
public partial class Default : System.Web.UI.Page
{
private Color color;
private int b;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFindXcell_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string path = Server.MapPath(ConfigurationManager.AppSettings["xmlPath"]);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
//dt = FileUploadExcel(path);
ds = ImportExcelXLS(path, true);
dgvEmployee.DataSource = ds.Tables[0];
dgvEmployee.DataBind();
}
//LoadChartCurrencyTotal(ds.Tables[0]);
Chart1.Series["Series1"].ChartType = SeriesChartType.Column;
Chart1.Series["Series1"].Color = Random();
Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.Series["Series1"].IsValueShownAsLabel = true;
Chart1.DataSource = ds.Tables[0];
Chart1.Series["Series1"].XValueMember = "Name";
Chart1.Series["Series1"].YValueMembers = "ID";
Chart1.Series["Series1"].XAxisType = AxisType.Primary;
Chart1.Series["Series1"].XValueType = ChartValueType.String;
Chart1.Series["Series1"].IsValueShownAsLabel = true;
Chart1.Series["Series1"].LabelForeColor = Color.Black;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Angle = 90;
Chart1.ChartAreas["ChartArea1"].AxisX.LineColor = Color.DarkGray;
Chart1.ChartAreas["ChartArea1"].AxisY.LineColor = Color.Pink;
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Azure;
Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Peru;
Chart1.BorderlineColor = Color.PaleVioletRed;
Chart1.DataBind();
}
public Color Random()
{
Random r = new Random();
b = r.Next(1, 5);
switch (b)
{
case 1:
{
color = Color.SeaGreen;
}
break;
case 2:
{
color = Color.SlateGray;
}
break;
case 3:
{
color = Color.SkyBlue;
}
break;
case 4:
{
color = Color.SteelBlue;
}
break;
}
return color;
}
public static DataSet ImportExcelXLS(string FileName, bool hasHeaders)
{
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
DataSet output = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
try
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
}
}
}
}
return output;
}
public DataTable FileUploadExcel(string filepath)
{
DataTable dt = new DataTable();
System.IO.FileInfo fi = new FileInfo(filepath);
string FileExtension = fi.Extension;
FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader;
if (FileExtension == ".xls")
{
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else
{
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
excelReader.IsFirstRowAsColumnNames = true;
if (excelReader.AsDataSet().Tables[0].Rows.Count > 0)
{
dt = excelReader.AsDataSet().Tables[0];
}
return dt;
}
}
用于测试目的,从excel文件中读取数据并根据数据创建图表。
答案 0 :(得分:1)
当前时间最广泛使用的图表库之一是highcharts。我强烈推荐它,它易于使用,可以高度自定义,具有丰富的功能和用户体验。
答案 1 :(得分:0)
有很多jquery图表 一些高度使用的是遵循
答案 2 :(得分:0)
您可能还想看一下:
ZedGraph此对象同时实现为UserControl和WebControl。将它集成到ASP.NET中由this tutorial涵盖。
Google Visualization这是一个可以集成到任何网页上的JavaScript库。 Google有一个很好的Getting Started页面,可以告诉您该怎么做。