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. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  2. Shiro身份验证及授权(二)

    一.Shiro 身份验证 身份验证的步骤: 收集用户身份 / 凭证,即如用户名 / 密码: 调用 Subject.login 进行登录,如果失败将得到相应的 AuthenticationExcepti ...

  3. java容器(二) Map类框架图解

  4. 马尔科夫随机场(MRF)及其在图像降噪中的matlab实现

    (Markov Random Field)马尔科夫随机场,本质上是一种概率无向图模型 下面从概率图模型说起,主要参考PR&ML 第八章 Graphical Model (图模型) 定义:A g ...

  5. C++快读模板

    C++的快速读入模板 inline int read() { ; char ch = getchar(); ') { if (ch == '-') flag = true; ch = getchar( ...

  6. Exception:Request processing failed; nested exception is org.apache.ibatis.binding.BindingException

    异常 在测试Spring MVC+Mybatis整合时,运行 Maven build -> tomcat7:Run 遇到如下异常 从异常信息上看,是找不到mapper对应的xml文件,于是我到t ...

  7. 实验14:VLAN间的路由

    实验11-1: 单臂路由实现VLAN 间路由 Ø    实验目的通过本实验,读者可以掌握如下技能:(1) 路由器以太网接口上的子接口(2) 单臂路由实现VLAN 间路由的配置Ø    实验拓扑 实验步 ...

  8. 分析Ajax爬取今日头条街拍美图-崔庆才思路

    站点分析 源码及遇到的问题 代码结构 方法定义 需要的常量 关于在代码中遇到的问题 01. 数据库连接 02.今日头条的反爬虫机制 03. json解码遇到的问题 04. 关于response.tex ...

  9. selenium 操作下拉处理

    操作下拉框处理 在网页中,有时候会遇到下拉框处理,这时候使用Webdriver提供的select类来处理. ##操作下拉框处理 #coding = utf-8 from selenium import ...

  10. iomanip、cstring、string、sstream

    #include<iomanip>          控   制   符                             作           用                 ...