c#图形报表开发(Winform)

tzj19810812 2010-10-12 02:48:33
c#图形报表开发(Winform)
...全文
793 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
deyygywxf 2010-10-30
  • 打赏
  • 举报
回复
楼主的问题太笼统了,给你个代码示例自己参考下:
C# code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Owc11;
using System.Data;

namespace Common
{
public class OWCChart
{
#region 私有变量和公有属性

private string _picPath;
/// <summary>
/// 图片保存路径
/// </summary>
public string PicPath
{
get { return _picPath; }
set { _picPath = value; }
}

private string _title;
/// <summary>
/// 标题名
/// </summary>
public string Title
{
get { return _title; }
set { _title = value; }
}

private string _seriesname;
/// <summary>
/// 图例
/// </summary>
public string Seriesname
{
get { return _seriesname; }
set { _seriesname = value; }
}

private int _picwidth;
/// <summary>
/// 图形宽度
/// </summary>
public int Picwidth
{
get { return _picwidth; }
set { _picwidth = value; }
}

private int _picheght;
/// <summary>
/// 图形高度
/// </summary>
public int Picheght
{
get { return _picheght; }
set { _picheght = value; }
}

private DataTable _datasource;
/// <summary>
/// 数据源
/// </summary>
public DataTable Datasource
{
get { return _datasource; }
set
{
_datasource = value;
strCategory = GetColumnsStr(_datasource);
strValue = GetValueStr(_datasource);
}
}

private ChartChartTypeEnum _chartType;
/// <summary>
/// 图表样式
/// </summary>
public ChartChartTypeEnum ChartType
{
get { return _chartType; }
set { _chartType = value; }
}

private string _color;
/// <summary>
/// 色彩
/// </summary>
public string Color
{
get { return _color; }
set { _color = value; }
}

private string strCategory;
private string strValue;

private string _x;
/// <summary>
/// X轴的说明
/// </summary>
public string X
{
get { return _x; }
set { _x = value; }
}

private string _y;
/// <summary>
/// Y轴的说明
/// </summary>
public string Y
{
get { return _y; }
set { _y = value; }
}

#endregion

#region 方法

private string GetColumnsStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[1] + "" + '\t');
}
return strList.ToString();
}

private string GetValueStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[0] + "" + '\t');
}
return strList.ToString();
}

/// <summary>
/// 创建报表
/// </summary>
public void CreateChart(System.Windows.Forms.PictureBox pic)
{
//创建一个图形容器对象
ChartSpace objCSpace = new ChartSpaceClass();
//在图形容器中增加一个图形对象
ChChart objChart = objCSpace.Charts.Add(0);
//图表样式
objChart.Type = _chartType;

//显示标题
objChart.HasTitle = true;
//设置标题的各种属性
objChart.Title.Caption = _title;
objChart.Title.Font.Color = _color;
objChart.Title.Font.Bold = true;
objChart.Title.Font.Size = 15;
objChart.Title.Font.Name = "Verdana";

//显示图例
objChart.HasLegend = true;
//样式设置
ChSeries chSeries = objChart.SeriesCollection.Add(0);


chSeries.Border.Color = "#536e92";

objChart.PlotArea.Interior.Color = "#DCEAFC";
objChart.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#d5e0f1", "#fdfefe");
objChart.Border.Color = "#1a3b69";
objChart.PlotArea.Border.Color = "#b1b9c6";

if (_chartType == ChartChartTypeEnum.chChartTypeColumnClustered3D)
{
objChart.Inclination = 10; //表示指定三维图表的视图斜率。有效范围为 -90 到 90
objChart.Rotation = 5; //表示指定三维图表的旋转角度

chSeries.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#5699f0", "#a2c7f7");

objChart.Axes[0].HasTitle = true;
objChart.Axes[0].Title.Caption = _x;
objChart.Axes[1].HasTitle = true;
objChart.Axes[1].Title.Caption = _y;
objChart.Axes[0].Title.Font.Color = "#1a3b69";
objChart.Axes[1].Title.Font.Color = "#1a3b69";

for (int i = 0; i < objChart.Axes.Count; i++)
{
objChart.Axes[i].HasMajorGridlines = true;
objChart.Axes[i].HasMinorGridlines = false;
objChart.Axes[i].Line.Color = "#b1b9c6";
objChart.Axes[i].MajorGridlines.Line.Color = "#b1b9c6";
objChart.Axes[i].Font.Name = "Verdana";
objChart.Axes[i].Font.Size = 8;
objChart.Axes[i].Font.Color = "#1a3b69";

}
}
//给定系列的名字
chSeries.SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), _seriesname);
//给定分类
chSeries.SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
//给定值
chSeries.SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);

ChDataLabels chDataLab = objChart.SeriesCollection[0].DataLabelsCollection.Add();
chDataLab.HasValue = true;
//chDataLab.HasPercentage = true;

string fileName = "chartPic.gif";
string strAbsolutePath = _picPath + "\\" + fileName;
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _picheght);
pic.ImageLocation = strAbsolutePath;
}
#endregion
}
}


漠北兄弟 2010-10-30
  • 打赏
  • 举报
回复
这几天正在找这方面资料,每个大虾指导做起来真费劲。。。
bourbon1795 2010-10-30
  • 打赏
  • 举报
回复
水晶报表不行吗?
suners 2010-10-30
  • 打赏
  • 举报
回复
都在找
hhwydwfg 2010-10-30
  • 打赏
  • 举报
回复
水晶报表
rdlc
fastreport


CraxyMouse 2010-10-30
  • 打赏
  • 举报
回复
http://blog.csdn.net/dunao/archive/2009/02/05/3865053.aspx

deyygywxf 2010-10-30
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Owc11;
using System.Data;

namespace Common
{
public class OWCChart
{
#region 私有变量和公有属性

private string _picPath;
/// <summary>
/// 图片保存路径
/// </summary>
public string PicPath
{
get { return _picPath; }
set { _picPath = value; }
}

private string _title;
/// <summary>
/// 标题名
/// </summary>
public string Title
{
get { return _title; }
set { _title = value; }
}

private string _seriesname;
/// <summary>
/// 图例
/// </summary>
public string Seriesname
{
get { return _seriesname; }
set { _seriesname = value; }
}

private int _picwidth;
/// <summary>
/// 图形宽度
/// </summary>
public int Picwidth
{
get { return _picwidth; }
set { _picwidth = value; }
}

private int _picheght;
/// <summary>
/// 图形高度
/// </summary>
public int Picheght
{
get { return _picheght; }
set { _picheght = value; }
}

private DataTable _datasource;
/// <summary>
/// 数据源
/// </summary>
public DataTable Datasource
{
get { return _datasource; }
set
{
_datasource = value;
strCategory = GetColumnsStr(_datasource);
strValue = GetValueStr(_datasource);
}
}

private ChartChartTypeEnum _chartType;
/// <summary>
/// 图表样式
/// </summary>
public ChartChartTypeEnum ChartType
{
get { return _chartType; }
set { _chartType = value; }
}

private string _color;
/// <summary>
/// 色彩
/// </summary>
public string Color
{
get { return _color; }
set { _color = value; }
}

private string strCategory;
private string strValue;

private string _x;
/// <summary>
/// X轴的说明
/// </summary>
public string X
{
get { return _x; }
set { _x = value; }
}

private string _y;
/// <summary>
/// Y轴的说明
/// </summary>
public string Y
{
get { return _y; }
set { _y = value; }
}

#endregion

#region 方法

private string GetColumnsStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[1] + "" + '\t');
}
return strList.ToString();
}

private string GetValueStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[0] + "" + '\t');
}
return strList.ToString();
}

/// <summary>
/// 创建报表
/// </summary>
public void CreateChart(System.Windows.Forms.PictureBox pic)
{
//创建一个图形容器对象
ChartSpace objCSpace = new ChartSpaceClass();
//在图形容器中增加一个图形对象
ChChart objChart = objCSpace.Charts.Add(0);
//图表样式
objChart.Type = _chartType;

//显示标题
objChart.HasTitle = true;
//设置标题的各种属性
objChart.Title.Caption = _title;
objChart.Title.Font.Color = _color;
objChart.Title.Font.Bold = true;
objChart.Title.Font.Size = 15;
objChart.Title.Font.Name = "Verdana";

//显示图例
objChart.HasLegend = true;
//样式设置
ChSeries chSeries = objChart.SeriesCollection.Add(0);


chSeries.Border.Color = "#536e92";

objChart.PlotArea.Interior.Color = "#DCEAFC";
objChart.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#d5e0f1", "#fdfefe");
objChart.Border.Color = "#1a3b69";
objChart.PlotArea.Border.Color = "#b1b9c6";

if (_chartType == ChartChartTypeEnum.chChartTypeColumnClustered3D)
{
objChart.Inclination = 10; //表示指定三维图表的视图斜率。有效范围为 -90 到 90
objChart.Rotation = 5; //表示指定三维图表的旋转角度

chSeries.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#5699f0", "#a2c7f7");

objChart.Axes[0].HasTitle = true;
objChart.Axes[0].Title.Caption = _x;
objChart.Axes[1].HasTitle = true;
objChart.Axes[1].Title.Caption = _y;
objChart.Axes[0].Title.Font.Color = "#1a3b69";
objChart.Axes[1].Title.Font.Color = "#1a3b69";

for (int i = 0; i < objChart.Axes.Count; i++)
{
objChart.Axes[i].HasMajorGridlines = true;
objChart.Axes[i].HasMinorGridlines = false;
objChart.Axes[i].Line.Color = "#b1b9c6";
objChart.Axes[i].MajorGridlines.Line.Color = "#b1b9c6";
objChart.Axes[i].Font.Name = "Verdana";
objChart.Axes[i].Font.Size = 8;
objChart.Axes[i].Font.Color = "#1a3b69";

}
}
//给定系列的名字
chSeries.SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), _seriesname);
//给定分类
chSeries.SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
//给定值
chSeries.SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);

ChDataLabels chDataLab = objChart.SeriesCollection[0].DataLabelsCollection.Add();
chDataLab.HasValue = true;
//chDataLab.HasPercentage = true;

string fileName = "chartPic.gif";
string strAbsolutePath = _picPath + "\\" + fileName;
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _picheght);
pic.ImageLocation = strAbsolutePath;
}
#endregion
}
}


unto_lg 2010-10-29
  • 打赏
  • 举报
回复
zhidaole
showjancn 2010-10-12
  • 打赏
  • 举报
回复
可以试试DevExpress控件库,感觉还是不错。
wuyq11 2010-10-12
  • 打赏
  • 举报
回复
水晶报表
rdlc
fastreport
浪子-无悔 2010-10-12
  • 打赏
  • 举报
回复
楼主的问题太笼统了,给你个代码示例自己参考下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Owc11;
using System.Data;

namespace Common
{
public class OWCChart
{
#region 私有变量和公有属性

private string _picPath;
/// <summary>
/// 图片保存路径
/// </summary>
public string PicPath
{
get { return _picPath; }
set { _picPath = value; }
}

private string _title;
/// <summary>
/// 标题名
/// </summary>
public string Title
{
get { return _title; }
set { _title = value; }
}

private string _seriesname;
/// <summary>
/// 图例
/// </summary>
public string Seriesname
{
get { return _seriesname; }
set { _seriesname = value; }
}

private int _picwidth;
/// <summary>
/// 图形宽度
/// </summary>
public int Picwidth
{
get { return _picwidth; }
set { _picwidth = value; }
}

private int _picheght;
/// <summary>
/// 图形高度
/// </summary>
public int Picheght
{
get { return _picheght; }
set { _picheght = value; }
}

private DataTable _datasource;
/// <summary>
/// 数据源
/// </summary>
public DataTable Datasource
{
get { return _datasource; }
set
{
_datasource = value;
strCategory = GetColumnsStr(_datasource);
strValue = GetValueStr(_datasource);
}
}

private ChartChartTypeEnum _chartType;
/// <summary>
/// 图表样式
/// </summary>
public ChartChartTypeEnum ChartType
{
get { return _chartType; }
set { _chartType = value; }
}

private string _color;
/// <summary>
/// 色彩
/// </summary>
public string Color
{
get { return _color; }
set { _color = value; }
}

private string strCategory;
private string strValue;

private string _x;
/// <summary>
/// X轴的说明
/// </summary>
public string X
{
get { return _x; }
set { _x = value; }
}

private string _y;
/// <summary>
/// Y轴的说明
/// </summary>
public string Y
{
get { return _y; }
set { _y = value; }
}

#endregion

#region 方法

private string GetColumnsStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[1] + "" + '\t');
}
return strList.ToString();
}

private string GetValueStr(DataTable dt)
{
StringBuilder strList = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
strList.Append(dr[0] + "" + '\t');
}
return strList.ToString();
}

/// <summary>
/// 创建报表
/// </summary>
public void CreateChart(System.Windows.Forms.PictureBox pic)
{
//创建一个图形容器对象
ChartSpace objCSpace = new ChartSpaceClass();
//在图形容器中增加一个图形对象
ChChart objChart = objCSpace.Charts.Add(0);
//图表样式
objChart.Type = _chartType;

//显示标题
objChart.HasTitle = true;
//设置标题的各种属性
objChart.Title.Caption = _title;
objChart.Title.Font.Color = _color;
objChart.Title.Font.Bold = true;
objChart.Title.Font.Size = 15;
objChart.Title.Font.Name = "Verdana";

//显示图例
objChart.HasLegend = true;
//样式设置
ChSeries chSeries = objChart.SeriesCollection.Add(0);


chSeries.Border.Color = "#536e92";

objChart.PlotArea.Interior.Color = "#DCEAFC";
objChart.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#d5e0f1", "#fdfefe");
objChart.Border.Color = "#1a3b69";
objChart.PlotArea.Border.Color = "#b1b9c6";

if (_chartType == ChartChartTypeEnum.chChartTypeColumnClustered3D)
{
objChart.Inclination = 10; //表示指定三维图表的视图斜率。有效范围为 -90 到 90
objChart.Rotation = 5; //表示指定三维图表的旋转角度

chSeries.Interior.SetTwoColorGradient(ChartGradientStyleEnum.chGradientFromCorner, ChartGradientVariantEnum.chGradientVariantStart, "#5699f0", "#a2c7f7");

objChart.Axes[0].HasTitle = true;
objChart.Axes[0].Title.Caption = _x;
objChart.Axes[1].HasTitle = true;
objChart.Axes[1].Title.Caption = _y;
objChart.Axes[0].Title.Font.Color = "#1a3b69";
objChart.Axes[1].Title.Font.Color = "#1a3b69";

for (int i = 0; i < objChart.Axes.Count; i++)
{
objChart.Axes[i].HasMajorGridlines = true;
objChart.Axes[i].HasMinorGridlines = false;
objChart.Axes[i].Line.Color = "#b1b9c6";
objChart.Axes[i].MajorGridlines.Line.Color = "#b1b9c6";
objChart.Axes[i].Font.Name = "Verdana";
objChart.Axes[i].Font.Size = 8;
objChart.Axes[i].Font.Color = "#1a3b69";

}
}
//给定系列的名字
chSeries.SetData(ChartDimensionsEnum.chDimSeriesNames, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), _seriesname);
//给定分类
chSeries.SetData(ChartDimensionsEnum.chDimCategories, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
//给定值
chSeries.SetData(ChartDimensionsEnum.chDimValues, ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);

ChDataLabels chDataLab = objChart.SeriesCollection[0].DataLabelsCollection.Add();
chDataLab.HasValue = true;
//chDataLab.HasPercentage = true;

string fileName = "chartPic.gif";
string strAbsolutePath = _picPath + "\\" + fileName;
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _picheght);
pic.ImageLocation = strAbsolutePath;
}
#endregion
}
}

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧