public JsonResult DrawBarChart()
{
#region 允许配置项 //定义宽高
int height = , width = ; //边缘位置留白
int margin_top = ;
int margin_right = ;
int margin_bottom = ;
int margin_left = ; //辅助线距离顶部的距离
int xsubline = ; //文字大小,单位:px
int fontsize = ; #endregion #region 数据 //最大数量/总数量
int maxCount = ; string[] bottomData = new string[] { "第一个", "第二个", "第三个", "第四个", "第五个" };
int[] barData = new int[] { , , , , }; maxCount = barData.Max();
maxCount = maxCount == ? : maxCount; #endregion //单位转换对象
Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor(); //生成图像对象
Bitmap image = new Bitmap(width + margin_left + margin_right, height + margin_top + margin_bottom); //创建画布
Graphics g = Graphics.FromImage(image);
//消除锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality; //黑色画笔--主轴颜色
Brush blackBrush = new SolidBrush(Color.FromArgb(, , , ));
Pen blackPen = new Pen(blackBrush, ); //灰色画笔--辅助线条颜色
Brush grayBrush = new SolidBrush(Color.FromArgb(, , , ));
Pen grayPen = new Pen(grayBrush, ); //填充区域内容
g.FillRectangle(Brushes.WhiteSmoke, , , width + margin_left + margin_right, height + margin_top + margin_bottom); //y轴
g.DrawLine(blackPen, margin_left, margin_top, margin_left, (height + margin_top)); //x轴
g.DrawLine(blackPen, margin_left, (height + margin_top), (width + margin_left), (height + margin_top)); Font font = new Font("宋体", unitCvtr.ConvertUnits(fontsize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point)); //x轴--辅助线 //画5条辅助线,不管数字大小..这里数字变化后,maxCount也继续变化,以适应后面的计算
int avgCount = Convert.ToInt32(Math.Ceiling(maxCount / 5.0)); maxCount = avgCount * ; int lineHeight = (height - xsubline) / ; //画辅助线与文字
for (int i = ; i <= ; i++)
{
//辅助线
if (i > )
{
g.DrawLine(grayPen, margin_left, (height + margin_top - lineHeight * i), (width + margin_left), (height + margin_top - lineHeight * i));
} //指向文字的线
g.DrawLine(blackPen, (margin_left - ), (height + margin_top - lineHeight * i), margin_left, (height + margin_top - lineHeight * i));
//文字
int text = avgCount * i; //if (i == 5)
//{
// if (maxCount - text > 0)
// {
// text = text + (maxCount - text);
// }
//} RectangleF rec = new RectangleF(, (height + margin_top - lineHeight * i - fontsize / ), margin_left - , );
//public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle);
//g.DrawString(text.ToString(), font, blackBrush, 10, (height + margin_top - lineHeight * i));
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
g.DrawString(text.ToString(), font, blackBrush, rec, format);
} //蓝色画笔--柱子的颜色
Brush blueBrush = new SolidBrush(Color.FromArgb(, , , ));
Pen bluePen = new Pen(blueBrush, ); int singleWidth = width / barData.Length; for (int i = ; i < barData.Length; i++)
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center; //居中 //计算柱子
int pillarHeight = Convert.ToInt32(barData[i] / Convert.ToDouble(maxCount) * (height - xsubline)); //这里是画柱子的代码
Rectangle rectangle = new Rectangle(margin_left + (i * singleWidth + singleWidth / ), height + margin_top - pillarHeight, singleWidth / , pillarHeight);
g.FillRectangle(blueBrush, rectangle); //柱子上的文字
RectangleF recText = new RectangleF(margin_left + (i * singleWidth), (height + margin_top - pillarHeight - ), singleWidth, );
g.DrawString(barData[i].ToString(), font, blackBrush, recText, format); //x轴下的文字
//指向线
g.DrawLine(blackPen, margin_left + (i * singleWidth + singleWidth / ), (height + margin_top), margin_left + (i * singleWidth + singleWidth / ), (height + margin_top + ));
//文字
RectangleF rec = new RectangleF(margin_left + (i * singleWidth), (height + margin_top + ), singleWidth, (margin_bottom - ));
g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);
} //将图片保存到指定的流中,适用于直接以流的方式输出图片
//System.IO.MemoryStream ms = new System.IO.MemoryStream();
//image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//Response.ClearContent();
//Response.ContentType = "image/Jpeg";
//Response.BinaryWrite(ms.ToArray()); string relativePath = @"\draw-image\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
string path = Server.MapPath(relativePath);
image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); //return relativePath;
return Json(relativePath, JsonRequestBehavior.AllowGet);
}

效果图

c#画图之柱形图的更多相关文章

  1. XCL-Charts图表库中柱形图的同源风格切换介绍

    柱形图是被使用最多的图之中的一个,在写XCL-Charts这个Android图表库时,为它花费的时间相当多,不是由于有多难绘制,而是要在设计时怎样才干保证图基类能适应各种情况,能灵活满足足够多的需求, ...

  2. Excel 2010高级应用-柱形图(一)

    今天,做项目低保真,是在excel中画图,这也是我第一次在excel中画图. 每次做过的东西或者学到的新东西,我必须要把他们记录下来,这样到时再次用到它们时可以很容易地找到. 下面介绍做柱形图的过程: ...

  3. echarts画图时tooltip.formatter参数params不会更新(转载)

    echarts画图时tooltip.formatter参数params不会更新 解决方案: setOption时默认是合并, 如果要全部重新加载 要写成 setOption({},true),这样就可 ...

  4. Matplotlib学习---用matplotlib画柱形图,堆积柱形图,横向柱形图(bar chart)

    这里利用Nathan Yau所著的<鲜活的数据:数据可视化指南>一书中的数据,学习画图. 数据地址:http://datasets.flowingdata.com/hot-dog-cont ...

  5. R语言-画柱形图

    barplot()函数 1.柱形图 > sales<-read.csv("citysales.csv",header=TRUE) #读取数据 > barplot( ...

  6. High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件

    原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还 ...

  7. ASP.NET 简单的柱形图实现(附带示例)

    对于一些内部系统的项目,各种图表是在所难免的,因为图表可以更加清晰的表达出想看到的数据. 因为之前从来没有做过关于图表的东西,唯一能想到的就是“验证码”,所以应该是一个思路,用GDI去搞. 数据懒着去 ...

  8. C# WinForm开发系列之c# 通过.net自带的chart控件绘制饼图,柱形图和折线图的基础使用和扩展

    一.需要实现的目标是: 1.将数据绑定到pie的后台数据中,自动生成饼图. 2.生成的饼图有详细文字的说明. 1.设置chart1的属性Legends中默认的Legend1的Enable为false: ...

  9. python使用matplotlib画图,jieba分词、词云、selenuium、图片、音频、视频、文字识别、人脸识别

    一.使用matplotlib画图 关注公众号"轻松学编程"了解更多. 使用matplotlib画柱形图 import matplotlib from matplotlib impo ...

随机推荐

  1. Linux 常用工具openssh之ssh-copy-id

    前言 ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上,ssh-copy-id命令也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ss ...

  2. Git详解之初次运行

    配置文件 一般在新的系统上,我们都需要先配置下自己的 Git 工作环境.配置工作只需一次,以后升级时还会沿用现在的配置.当然,如果需要,你随时可以用相同的命令修改已有的配置. Git 提供了一个叫做 ...

  3. java web 各个文件夹命名原因

    今天突然被同学问然后就发现,自己有很多的疑问: (1) 为什么servlet的配置文件,命名为 web.xml , 内部是如何读取的,原因就是他内度的工作原理 (2) webINF Src 文件为什么 ...

  4. Maven的scope属性作用域范围

    在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: 1. compile,缺省值,适用于所有 ...

  5. Distance Dependent Infinite Latent Feature Model 阅读笔记1

    阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...

  6. 最新2.7版本丨DataPipeline数据融合产品最新版本发布

    此次发布的2.7版本在进一步优化产品底层数据处理逻辑的同时更加注重提升用户在数据融合任务的日常管理.运行监控及资源分配等管理方面的功能增强与优化,力求帮助大家更为直观.便捷.稳定地管理数据融合任务,提 ...

  7. hdu 1159求最长公共子序列

    题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...

  8. constrainlayout布局

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/r ...

  9. Ops:jar包启动关闭脚本

    简介 公司开发架构为java语言的rpc dubbo架构,将功能分解为各个模块,模块较多,发布到环境上的应用为编译后的jar包和配置文件,以及启动关闭jar包的shell脚本.之前经常会出现进程启动不 ...

  10. postman之批量数据参数化(文件)

    相信小伙伴们在做接口测试时需要导入大量的数据进行测试,Jmeter进行接口测试时可以导入CSV数据文件进行参数化,那么postman又该如何导入数据文件进行测试呢?下面我给大家讲解一下. 第一:创建t ...