前一些时间,在一个油田生产管理系统中遇到了一个我很感兴趣的工作,就是要开发出许多由生产到销售的一系列图形报表。在此之前没有做过这方面的工作呀,于是Google呀,也找到了许多现成的控件可以用,但是都是收费的,公司不愿掏Money。没办法只能找免费的,Free的真的很少啊。另外在网上也看到了许多朋友自己写的图形报表,但是看了效果自己不是很理想。最后向公司的另外以为高手请教,哈哈,他给了一个国外破解的图形报表控件,这个控件真的很不错,现在拿出来和大家一起来分享。
先看一下效果:
下面是它的一个示例代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dundas.Charting.WebControl;
using JH.WySystem.DataBaseSQLHelper;
using JH.WySystem.BLL.Sj_Fx;
namespace WySysTemWeb.Sj_Fx
{
public partial class WySf_Ls_DataFx : System.Web.UI.Page
{
private DataTable tableOld, tableNew;
private int yearOld, yearNew;
protected System.Web.UI.WebControls.Label lb_title;
private ChartArea chartArea;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
int year = DateTime.Today.Year;
ArrayList listYear = new ArrayList();
for (int k = 2001; k < year+1; k++)
{
listYear.Add(k);
}
ddl_year.DataSource = listYear;
ddl_year.SelectedValue = year.ToString();
ddl_year.DataBind();
Dll_Xq_DataBin(); //绑定小区信息
Dd_Chages_Item(); //绑定小区收费项目
//处理连接
string strDate = ddl_year.SelectedValue.Trim(); //分析年份
string strDwbm = ddl_unit.SelectedValue.Trim(); //分析小区
if (strDate != null && strDwbm != null)
{
ddl_unit.SelectedValue = strDwbm;
ddl_year.SelectedValue =strDate.ToString();
}
yearNew = int.Parse(ddl_year.SelectedValue);
yearOld = yearNew - 1;
try
{
tableOld = Inhabitant_CopyWatch_Chart.WysfLn_GetByXQYear(int.Parse(ddl_unit.SelectedValue.Trim()), yearOld.ToString(), int.Parse(Dd_Item.SelectedValue.Trim()), "yearOld");
tableNew = Inhabitant_CopyWatch_Chart.WysfLn_GetByXQYear(int.Parse(ddl_unit.SelectedValue.Trim()), yearNew.ToString(), int.Parse(Dd_Item.SelectedValue.Trim()), "yearNew");
if (tableNew == null && tableOld == null || tableNew.Rows.Count == 0 && tableOld.Rows.Count == 0)
{
lbl_err.Text = "你所选择的小区在该时间段无数据!";
lbl_err.Visible = true;
}
else
{
lbl_err.Visible = false;
CreateChart(tableOld, tableNew);
}
}
catch (Exception ex)
{
lbl_err.Text = "访问数据库失败!";
lbl_err.Visible = true;
}
}
}
private void CreateChart(DataTable tableOld, DataTable tableNew)
{
string unitName = ddl_unit.SelectedItem.Text;
//lb_title.Text = unitName + "收费月分析图";
Chart1.TitleFont = new Font("宋体", 10, FontStyle.Bold);
Chart1.Legend.Font = new Font("宋体", 9);
Chart1.Legend.LegendStyle = LegendStyle.Row;
Chart1.Legend.Docking = LegendDocking.Top;
chartArea = Chart1.ChartAreas[0];
chartArea.AxisX.Title = "日期(月)";
chartArea.AxisY.Title = "金额:(元)";
chartArea.AxisX.TitleFont = chartArea.AxisY.TitleFont = chartArea.AxisX.LabelStyle.Font = chartArea.AxisY.LabelStyle.Font = new Font("宋体", 9);
AddColumn(tableOld);
AddColumn(tableNew);
Chart1.Series.Clear();
if (tableOld.Rows.Count >= tableNew.Rows.Count)
{
AddChartSeries(Chart1, yearOld.ToString(), tableOld.DefaultView, "rq", tableOld.DefaultView, "total", Color.CornflowerBlue, Color.SkyBlue);
AddChartSeries(Chart1, yearNew.ToString(), tableNew.DefaultView, "rq", tableNew.DefaultView, "totalNew", Color.Red, Color.Tomato);
AddHrefToPoint(Chart1.Series[0], tableOld.DefaultView);
AddHrefToPoint(Chart1.Series[1], tableNew.DefaultView);
}
else
{
AddChartSeries(Chart1, yearOld.ToString(), tableOld.DefaultView, "rq", tableOld.DefaultView, "total", Color.CornflowerBlue, Color.SkyBlue);
AddChartSeries(Chart1, yearNew.ToString(), tableNew.DefaultView, "rq", tableNew.DefaultView, "totalNew", Color.Red, Color.Tomato);
AddHrefToPoint(Chart1.Series[0], tableOld.DefaultView);
AddHrefToPoint(Chart1.Series[1], tableNew.DefaultView);
}
}
private Series AddChartSeries(Chart chart, string seriesName, IEnumerable enuX, string xValue, IEnumerable enuY, string yValue, Color color, Color bgColor)
{
Series s = chart.Series.Add(seriesName);
s.Points.DataBindXY(enuX, xValue, enuY, yValue);
s.Type = SeriesChartType.Column;
s.Color = color;
s.BackGradientEndColor = bgColor;
s.BackGradientType = GradientType.DiagonalLeft;
s.CustomAttributes = "DrawingStyle=Cylinder, LabelStyle=Bottom";
s.BorderColor = Color.Black;
s.BorderStyle = ChartDashStyle.Solid;
s.BorderWidth = 1;
s.ShadowOffset = 1;
s.EmptyPointStyle.BorderWidth = 0;
s.EmptyPointStyle.MarkerStyle = MarkerStyle.None;
s.Font = new Font("宋体", 8);
return s;
}
private void AddHrefToPoint(Series series, DataView dataView)
{
for (int k = 0; k < dataView.Table.Rows.Count; k++)
{
series.Points[k].MapAreaAttributes = String.Format("href=\"javascript:void(0)\" onclick=\"\" onmouseover=\"dispPie('{1}')\" onmouseout=\"cancelPie()\"", "date=" + dataView.Table.Rows[k]["rq"].ToString(), dataView.Table.Rows[k]["toolTip"].ToString());
}
}
private void AddColumn(DataTable table)
{
if (table != null)
{
table.Columns.Add("toolTip", Type.GetType("System.String"));
foreach (DataRow dr in table.Rows)
{
dr["toolTip"] = "月份:" + dr["rq"].ToString() + "月<br>" + "金额:" + dr["sfje"].ToString();
}
}
}
private void addColumn(DataTable targetTable, string TFieldName, DataTable souTable, string SFieldName)
{
targetTable.Columns.Add(TFieldName, Type.GetType("System.Double"));
if (targetTable.Rows.Count >= souTable.Rows.Count)
{
for (int i = 0; i < souTable.Rows.Count; i++)
{
targetTable.Rows[i][TFieldName] = souTable.Rows[i][SFieldName];
}
}
else
{
for (int j = 0; j < targetTable.Rows.Count; j++)
{
targetTable.Rows[j][TFieldName] = souTable.Rows[j][SFieldName];
}
}
}
/// <summary>
/// 绑定小区信息列表
/// </summary>
private void Dll_Xq_DataBin()
{
string SQL = "SELECT [XQ_ID], [xq_mc] FROM [Small_area_data]";
DataTable Dt = SqlHelper.GetQueryResult(SQL);
if (Dt.Rows.Count > 0)
{
ddl_unit.DataSource = Dt;
ddl_unit.DataBind();
}
}
/// <summary>
/// 取得收费项目
/// </summary>
public void Dd_Chages_Item()
{
string SQl = "select [sfmc],[Charge_item_ID] from [Charge_item]";
DataTable Dt = SqlHelper.GetQueryResult(SQl);
if(Dt.Rows.Count>0)
{
Dd_Item.DataSource = Dt;
Dd_Item.DataBind();
}
}
protected void but_ok_Click(object sender, EventArgs e)
{
yearNew = Convert.ToInt32(ddl_year.SelectedValue);
yearOld = yearNew - 1;
lbl_err.Visible = false;
try
{
tableOld = Inhabitant_CopyWatch_Chart.WysfLn_GetByXQYear(int.Parse(ddl_unit.SelectedValue.Trim()), yearOld.ToString(), int.Parse(Dd_Item.SelectedValue.Trim()), "yearOld");
tableNew = Inhabitant_CopyWatch_Chart.WysfLn_GetByXQYear(int.Parse(ddl_unit.SelectedValue.Trim()), yearNew.ToString(), int.Parse(Dd_Item.SelectedValue.Trim()), "yearNew");
if (tableNew == null && tableOld == null || tableNew.Rows.Count == 0 && tableOld.Rows.Count == 0)
{
lbl_err.Text = "你所选择的小区在该时间段无数据!";
lbl_err.Visible = true;
}
else
{
lbl_err.Visible = false;
CreateChart(tableOld, tableNew);
}
}
catch (Exception ex)
{
lbl_err.Text = "访问数据库失败!";
lbl_err.Visible = true;
}
}
protected void Chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
if (sender is ChartPicture)
{
if (Chart1.Series.Count > 0)
{
int cellWidth;
int firstCellWidth;
if (Chart1.Series[0].Points.Count > 1)
{
// Calculate data cell width
cellWidth = (int)Chart1.ChartAreas["Default"].AxisX.GetPosition(Chart1.Series[0].Points[1].XValue);
cellWidth -= (int)Chart1.ChartAreas["Default"].AxisX.GetPosition(Chart1.Series[0].Points[0].XValue);
cellWidth = (int)e.ChartGraphics.GetAbsolutePoint(new PointF(cellWidth, 0)).X;
// Calculate first column width
firstCellWidth = (int)Chart1.ChartAreas["Default"].AxisX.GetPosition(Chart1.Series[0].Points[0].XValue - 0.5);
firstCellWidth = (int)e.ChartGraphics.GetAbsolutePoint(new PointF(firstCellWidth, 0)).X;
}
else
{
cellWidth = 60;
firstCellWidth = 150;
}
ChangeColumnName(tableOld, tableNew);
//InitializeTable(ValuesTable, tableOld, firstCellWidth + 6, cellWidth + 2, yearOld.ToString());
// InitializeTable(ValuesTable, tableNew, firstCellWidth + 6, cellWidth + 2, yearNew.ToString());
}
}
}
private void ChangeColumnName(DataTable tableOld, DataTable tableNew)
{
if (tableOld != null)
{
tableOld.Columns.Remove("xqbh");
tableOld.Columns.Remove("rq");
tableOld.Columns.Remove("toolTip");
tableOld.Columns[0].ColumnName = "月均日产";
tableOld.Columns[1].ColumnName = "月开井数";
tableOld.Columns[2].ColumnName = "月均单井日产";
}
if (tableNew != null)
{
tableNew.Columns.Remove("xqbh");
tableNew.Columns.Remove("rq");
tableNew.Columns.Remove("toolTip");
tableNew.Columns[0].ColumnName = "月均日产";
tableNew.Columns[1].ColumnName = "月开井数";
tableNew.Columns[2].ColumnName = "月均单井日产";
}
if (tableOld != null && tableNew != null)
{
if (tableOld.Rows.Count >= tableNew.Rows.Count)
{
//tableOld.Columns.Remove("total");
}
else
{
//tableNew.Columns.Remove("totalNew");
}
}
}
public void InitializeTable(Table table, DataTable sTable, int firstColumnWidth, int otherColumnWidth, string nian)
{
foreach (DataColumn column in sTable.Columns)
{
// Create new table row for each column
TableRow row = new TableRow();
// Add title cell
TableCell cell = new TableCell();
cell.Controls.Add(new LiteralControl(nian + "年" + column.ColumnName));
cell.Width = new Unit(firstColumnWidth, UnitType.Pixel);
cell.HorizontalAlign = HorizontalAlign.Right;
row.Cells.Add(cell);
// Add data cells
int pointIndex = 0;
foreach (DataRow dataRow in sTable.Rows)
{
TableCell dataCell = new TableCell();
dataCell.Controls.Add(new LiteralControl(dataRow[column].ToString()));
dataCell.Width = new Unit(otherColumnWidth, UnitType.Pixel);
dataCell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(dataCell);
++pointIndex;
}
// Add row into the table
table.Rows.Add(row);
}
}
}
}
效果:
有需要的朋友留言,发给他。