public JsonResult DrawLineChart()
{
// 预置颜色
List<Color> colors = new List<Color>()
{
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,),
Color.FromArgb(,,)
}; #region 允许配置项 //定义宽高
int height = , width = ; //边缘位置留白
int margin_top = ;
int margin_right = ;
int margin_bottom = ;
int margin_left = ; //辅助线距离顶部的距离
int xsubline = ; //文字大小,单位:px
int fontsize = ; // 折线名称预留的位置 颜色框20,与文字间隙5,文字80,距离折线图10,需要包含边缘留白
int lineNameWidth = - margin_right; #endregion #region 数据 //最大数量/总数量--生成y轴时,显示数字需要
int maxCount = ; //x轴底部显示的名称
string[] bottomData = new string[] { "第一个", "第二个", "第三个", "第四个", "第五个" }; //折线名称
string[] lineName = new string[] { "折线1", "折线2" }; //折线数据
List<List<int>> lineData = new List<List<int>> {
new List<int>(){ ,,,, },
new List<int>(){ ,,,, }
}; //maxCount = xCount.Max();
for (int i = ; i < lineData.Count; i++)
{
int tempMaxCount = lineData[i].Max(); if (tempMaxCount > maxCount)
{
maxCount = tempMaxCount;
}
} maxCount = maxCount == ? : maxCount; #endregion //单位转换对象
Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor(); //生成图像对象
Bitmap image = new Bitmap(width + margin_left + margin_right + lineNameWidth, 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 + lineNameWidth, 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条辅助线,不管数字大小
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; RectangleF rec = new RectangleF(, (height + margin_top - lineHeight * i - fontsize / ), margin_left - , );
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
g.DrawString(text.ToString(), font, blackBrush, rec, format);
} //底部文字
int singleWidth = width / (bottomData.Length - ); for (int i = ; i < bottomData.Length; i++)
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center; //居中 //x轴下的文字
//指向线
g.DrawLine(blackPen, margin_left + i * singleWidth, (height + margin_top), margin_left + i * singleWidth, (height + margin_top + ));
//文字
RectangleF rec = new RectangleF(margin_left + (i * singleWidth) - singleWidth / , (height + margin_top + ), singleWidth, (margin_bottom - ));
g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);
} //预定颜色
for (int i = ; i < lineName.Length; i++)
{
//随机颜色
Color tempColor = colors[i];//GetRandomColor(); //文字内容
StringFormat format = new StringFormat(StringFormatFlags.DirectionVertical);
//format.Alignment = StringAlignment.Center; //居中 //画笔
SolidBrush brush = new SolidBrush(tempColor);
Pen pen = new Pen(brush, ); // 折线名称处理
// 颜色块
Rectangle rectangle = new Rectangle(margin_left + width + , margin_top + i * , , );
g.FillRectangle(brush, rectangle); // 文字
RectangleF rec = new RectangleF(margin_left + width + + , margin_top + i * , , );
g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format); //这里要开始画折线了
Point[] points = new Point[lineData[i].Count]; for (int j = ; j < lineData[i].Count; j++)
{
int x = j * singleWidth + margin_left;
int y = height + margin_top - Convert.ToInt32(lineData[i][j] / Convert.ToDouble(maxCount) * (height - xsubline)); Point point = new Point(x,y); points[j] = point;
} g.DrawLines(pen, points);
} 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. 用python的matplotlib根据文件里面的数字画图像折线图

    思路:用open打开文件,再用a=filename.readlines()提取每行的数据作为列表的值,然后传递列表给matplotlib并引入对应库画出图像 代码实现:import matplotli ...

  2. VS2010 使用TeeChart画图控件 - 之二 - 绘制图形(折线图,柱状图)

    1.前期准备 详细可见VS2010 使用TeeChart画图控件 - 之中的一个 控件和类的导入 1. 1 加入TeeChart控件,给控件加入变量m_TeeChart 加入TeeChart控件,右击 ...

  3. 使用C#画图(饼图折线图)

    public PlaceHolder PlaceHolder1; //显示图像的控件 各个图像的类别名称如下: PictureType    图形种类    5    chChartTypeBarCl ...

  4. highcharts高级画图柱状图和折线图

    折线图一枚 $("#z_line").highcharts({ chart: { type: 'line' }, credits: { enabled: false // 禁用版权 ...

  5. python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

    最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...

  6. android 自定义折线图

    看图: 比较简陋,主要是通过canvas画上去的: package com.example.democurvegraph.view; import java.util.ArrayList; impor ...

  7. 利用python进行折线图,直方图和饼图的绘制

    我用10个国家某年的GDP来绘图,数据如下: labels   = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', ...

  8. achartengine画出动态折线图

    achartengine画出动态折线图的效果最近有个项目需要用到实时曲线图,我也上网搜索了一下,最后还是选择使用achartengine这个现成的东西,毕竟自己再canvas一下实在是太麻烦,而且项目 ...

  9. 安卓图表引擎AChartEngine(四) - 源码示例 嵌入Acitivity中的折线图

    前面几篇博客中都是调用ChartFactory.get***Intent()方法,本节讲的内容调用ChartFactory.get***View()方法,这个方法调用的结果可以嵌入到任何一个Activ ...

随机推荐

  1. CentOS7主机名的查看和修改

    CentOS7主机名的查看和修改 在CentOS7中,有三种定义的主机名: 静态的(Static hostname) "静态"主机名也称为内核主机名,是系统在启动时从/etc/ho ...

  2. Arduino系列之按键模块(一)

    今天我将简单介绍按键模块计数的原理: 我们常用的按键及按键模块有2脚和4脚的,其内部结构如图所示,当按下按键时就会接通按键两端,当放开时,两端自然断开.                         ...

  3. python学习Day04--列表

    [主要内容] 1.列表的索引: lst=[1,2,3,4,5,6] print(lst[0])      #获取第一个元素 lst[1]='你好'    #改变列表中的值 2.列表的切片: lst=[ ...

  4. SASS用法入门

    本文参考了 阮一峰 老师对 SASS 用法的讲解. 学过 CSS 的人都知道,它不是一种编程语言,在日常的开发中,经常要写大量的 css 代码,有很多的重复代码,效率很低.Sass 是一个 CSS 的 ...

  5. 【动手学pytorch】softmax回归

    一.什么是softmax? 有一个数组S,其元素为Si ,那么vi 的softmax值,就是该元素的指数与所有元素指数和的比值.具体公式表示为: softmax回归本质上也是一种对数据的估计 二.交叉 ...

  6. 批处理版MPlayer播放器(甲兵时代原创批处理)(下)

    注意,由于空间不支持显示退格键,需要自己手动补上,方法如上图: 接上篇: 批处理版音视频播放器上(甲兵时代原创批处理) :Bc cls COLOR 2F echo. call :colour &quo ...

  7. 自己封装的一个Ajax小框架

    在经历了Jsp实训的惨痛教训后,特意花了点时间学习Ajax,学完后自我感觉良好,于是写了如下一个小框架: /** * frameAjax * * 参数: * paramsObj: Json * req ...

  8. java jdk1.8 32/64位 官方绿色版下载附安装教程

    java jdk1.8 32/64位 官方绿色版下载附安装教程 地址:https://www.jb51.net/softs/691593.html

  9. 关于在osgearth 中 出现 arial.ttf : file not handled 的问题

    这是由于配置osg时 freetype 插件没有配置到位. 我个人的解决方法 打开CMAKE,点击advance,不勾选OSG_TEXT_USE_FONTCONFIG. 同时将freetype路径设置 ...

  10. html标签学习入门 随笔

    Html学习入门    随笔1: HTML 标题 HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 标题仅用于标题文本  不应该被使用在加粗字 ...