c#画图之折线图
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#画图之折线图的更多相关文章
- 用python的matplotlib根据文件里面的数字画图像折线图
思路:用open打开文件,再用a=filename.readlines()提取每行的数据作为列表的值,然后传递列表给matplotlib并引入对应库画出图像 代码实现:import matplotli ...
- VS2010 使用TeeChart画图控件 - 之二 - 绘制图形(折线图,柱状图)
1.前期准备 详细可见VS2010 使用TeeChart画图控件 - 之中的一个 控件和类的导入 1. 1 加入TeeChart控件,给控件加入变量m_TeeChart 加入TeeChart控件,右击 ...
- 使用C#画图(饼图折线图)
public PlaceHolder PlaceHolder1; //显示图像的控件 各个图像的类别名称如下: PictureType 图形种类 5 chChartTypeBarCl ...
- highcharts高级画图柱状图和折线图
折线图一枚 $("#z_line").highcharts({ chart: { type: 'line' }, credits: { enabled: false // 禁用版权 ...
- python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...
- android 自定义折线图
看图: 比较简陋,主要是通过canvas画上去的: package com.example.democurvegraph.view; import java.util.ArrayList; impor ...
- 利用python进行折线图,直方图和饼图的绘制
我用10个国家某年的GDP来绘图,数据如下: labels = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', ...
- achartengine画出动态折线图
achartengine画出动态折线图的效果最近有个项目需要用到实时曲线图,我也上网搜索了一下,最后还是选择使用achartengine这个现成的东西,毕竟自己再canvas一下实在是太麻烦,而且项目 ...
- 安卓图表引擎AChartEngine(四) - 源码示例 嵌入Acitivity中的折线图
前面几篇博客中都是调用ChartFactory.get***Intent()方法,本节讲的内容调用ChartFactory.get***View()方法,这个方法调用的结果可以嵌入到任何一个Activ ...
随机推荐
- 数据结构与算法 --- js二分算法
var arr = [-34, 1, 3, 4, 5, 8, 34, 45, 65, 87]; //递归方式 function binarySearch(data,dest,start,end ){ ...
- 基于Flask框架搭建视频网站的学习日志(三)之原始web表单
基于Flask框架搭建视频网站的学习日志(三)1.原始Web 表单 本节主要用于体验一下前端后端直接数据的交互,样例不是太完善,下一节会加入Flash处理,稍微完善一下页面 (备注:建议先阅读廖雪峰老 ...
- Spring Boot 2.x基础教程:使用JdbcTemplate访问MySQL数据库
在第2章节中,我们介绍了如何通过Spring Boot来实现HTTP接口,以及围绕HTTP接口相关的单元测试.文档生成等实用技能.但是,这些内容还不足以帮助我们构建一个动态应用的服务端程序.不论我们是 ...
- 前端性能优化之利用 Chrome Dev Tools 进行页面性能分析
背景 我们经常使用 Chrome Dev Tools 来开发调试,但是很少知道怎么利用它来分析页面性能,这篇文章,我将详细说明怎样利用 Chrome Dev Tools 进行页面性能分析及性能报告数据 ...
- 基于 Istio 与 Kubernetes 对应用进行灰度发布与 Tracing
灰度发布,是指在黑与白之间,能够平滑过渡的一种发布方式.通俗来说,即让产品的迭代能够按照不同的灰度策略对新版本进行线上环境的测试,灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以对新版本进行测试 ...
- Spring配置 bean
在 Spring 的 IOC 容器里配置 Bean <bean id="helloWorld" class="com.xiya.spring.beans.Hello ...
- python函数2(返回值、传递列表...)
python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...
- Codeforces_842
A.枚举一个区间,判断是否有数符合. #include<bits/stdc++.h> using namespace std; long long l,r,x,y,k; int main( ...
- Go语言实现:【剑指offer】变态跳台阶
该题目来源于牛客网<剑指offer>专题. 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 找规律: 1阶:1种: 2阶:2 ...
- Go语言实现:【剑指offer】和为S的两个数字
该题目来源于牛客网<剑指offer>专题. 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 对应每个测试案 ...