使用dotnetCharting生成统计图的图片

需求:webAPI接口中需要后台生成统计图的图片,嵌入生成的pdf中

官网地址:https://www.dotnetcharting.com

官网demo地址:https://www.dotnetcharting.com/demo.aspx

说明:本人使用的dll包是7.0破解版本(Web版),可满足最基本的功能需求

准备工作:

  1. 创建一个web项目
  2. 项目添加引用下载的dotnetCHARTING.dll包
  3. 创建一个类文件,添加引用 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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值