需求:webAPI接口中需要后台生成统计图的图片,嵌入生成的pdf中
官网地址:https://www.dotnetcharting.com
官网demo地址:https://www.dotnetcharting.com/demo.aspx
说明:本人使用的dll包是7.0破解版本(Web版),可满足最基本的功能需求
准备工作:
- 创建一个web项目
- 项目添加引用下载的dotnetCHARTING.dll包
- 创建一个类文件,添加引用 using dotnetCHARTING;
简单的柱状以及饼状统计图的实现代码
#region 绘制统计图
/// <summary>
/// 创建柱状统计图
/// </summary>
/// <param name="dtSource">数据源(两列,列名称分别为Name和YValue,其中Name对应于X轴个尺度的名称,YValue为Y轴的数值</param>
/// <param name="title">图片标题</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <param name="xTitle">x轴文字标识</param>
/// <param name="yTitle">y轴文字标识</param>
/// <param name="seriesName">图例文本说明</param>
/// <param name="filePath">图片保存路径</param>
/// <returns></returns>
public static bool CreateColumn(DataTable dtSource,string title,int width,int height,string xTitle,string yTitle,string seriesName,out string filePath)
{
try
{
Chart chart = new Chart();
chart.Title = title;
chart.Width = width;
chart.Height = height;
chart.XAxis.Label.Text = xTitle;
chart.YAxis.Label.Text = yTitle;
//chart.XAxis.Label.Font = new Font("宋体", 8, FontStyle.Bold);
//chart.YAxis.Label.Font = new Font("宋体", 8, FontStyle.Bold);
chart.Type = ChartType.Combo;
chart.Series.Type = SeriesType.Cylinder;
chart.Series.Name = seriesName;
chart.Series.Data = dtSource;
chart.SeriesCollection.Add();
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect = true;
chart.Use3D = false;
chart.Series.DefaultElement.ShowValue = true;
chart.Series.ShowOther = false;
chart.LegendBox.Position = LegendBoxPosition.None;//不显示图例
//文件路径
string rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\", @"/");
string uploadFilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
if (string.IsNullOrWhiteSpace(uploadFilePath))
{
uploadFilePath = "/UploadFiles";
}
string fileFullPath = "";
fileFullPath = rootPath.Substring(0, rootPath.LastIndexOf("/")) + uploadFilePath + "/ImageFiles/";
fileFullPath = fileFullPath + DateTime.Now.ToString("yyyyMMdd") + "/";
CreatePath(fileFullPath);
filePath = fileFullPath + Guid.NewGuid().ToString() + ".jpg";
Image img = chart.GetChartBitmap();
img.Save(filePath);
return true;
}
catch
{
filePath = "";
return false;
}
}
/// <summary>
/// 创建饼状统计图
/// </summary>
/// <param name="dtSource">数据源</param>
/// <param name="title">标题</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="seriesName">图例文本说明</param>
/// <param name="filePath">图片保存路径</param>
/// <returns></returns>
public static bool CreatePie(DataTable dtSource, string title, int width, int height, string seriesName, out string filePath)
{
try
{
Chart chart = new Chart();
chart.Title = title;
chart.Width = width;
chart.Height = height;
chart.Type = ChartType.Pie;
chart.Series.Type = SeriesType.AreaLine;
chart.Series.Name = seriesName;
chart.ShadingEffect = true;
chart.Use3D = false;
chart.DefaultSeries.DefaultElement.Transparency = 20;
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.PieLabelMode = PieLabelMode.Outside;
//数据处理
SeriesCollection seriesCollection = new SeriesCollection();
foreach(DataRow dr in dtSource.Rows)
{
Series series = new Series();
series.Name = dr["Name"].ToString();
Element element = new Element();
element.Name = dr["Name"].ToString(); // 每元素的名称
element.YValue = Convert.ToInt32(dr["YValue"].ToString());// 每元素的大小数值
series.Elements.Add(element);
seriesCollection.Add(series);
}
chart.SeriesCollection.Add(seriesCollection);
chart.Series.DefaultElement.ShowValue = true;
//文件路径
string rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\", @"/");
string uploadFilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
if (string.IsNullOrWhiteSpace(uploadFilePath))
{
uploadFilePath = "/UploadFiles";
}
string fileFullPath = "";
fileFullPath = rootPath.Substring(0, rootPath.LastIndexOf("/")) + uploadFilePath + "/ImageFiles/";
fileFullPath = fileFullPath + DateTime.Now.ToString("yyyyMMdd") + "/";
CreatePath(fileFullPath);
filePath = fileFullPath + Guid.NewGuid().ToString() + ".jpg";
Image img = chart.GetChartBitmap();
img.Save(filePath);
return true;
}
catch
{
filePath = "";
return false;
}
}
#endregion