ASP.NET画直方图

ASP.NET画直方图最近做直方图,找到了这篇文章,以此为例画一般的直方图可以实现了。usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Web;usingSystem.Web.SessionState;

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
 最近做直方图,找到了这篇文章,以此为例画一般的直方图可以实现了。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace commonu
{
/** <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{

/**//*自写编码*/
int ImgWidth=600; //1.[总宽度] ***图宽度 
int ImgHeight=300; //2.[总高度] ***图高度 
int ItemNum=1; //3.[项目数量] ***图表划分的块 
int ChildNum=6; //4.[块数] ***大块中划分的子项的数量
float ChildRate=0.6f; //5.[各块总占空间比率]
//int ChildSpace=15; //6.[各块间的间距]
int ChartLeft=80; //7.[图表左边距] ***图表距图的左边距离
int ChartRight=50; //8.[图表右边距] ***图表距图的右边距离
int ChartTop=50; //9.[图表顶边距] ***图表距图顶边距离
int ChartBottom=50; //10.[图表底边距] ***图表距图底边距离
int YMaxValue=5000; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
int YItemNum=10; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
int YTop=15; //13.[距纵轴顶端间隔]
int YStrStart=35; //14.[纵坐标标尺文字起始X坐标]
int XRight=15; //15.[距横轴右端间隔]
int XStrStart=20; //16.[横坐标标尺文字起始Y坐标]

//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
int chartwidth=ImgWidth-ChartLeft-ChartRight-XRight;
//[项目宽度]=[图表总宽度]/[项目数量]
int itemwidth=chartwidth/ItemNum;
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
int factwidth=Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
int rectanglewidth=factwidth/ChildNum;
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
int childspace=Convert.ToInt32(Math.Floor((itemwidth-factwidth)/(ChildNum+1)));

Graphics objGps;//建立画板对象
Bitmap objBitMap = new Bitmap(ImgWidth,ImgHeight);//建立位图对象
objGps = Graphics.FromImage(objBitMap);//根据位图对象建立画板对象
objGps.Clear(Color.White);//设置画板对象的背景色

int[] arrValues={0,0,0,0,0,0};//数据数组
//arrValues[0]=500;
arrValues[0]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*500/YMaxValue)); //处理显示数据,进行图表数值对应
arrValues[1]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*700/YMaxValue));
arrValues[2]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*900/YMaxValue));
arrValues[3]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*1000/YMaxValue));
arrValues[4]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*1400/YMaxValue));
arrValues[5]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*2200/YMaxValue));
string[] arrValueNames={"0","0","0","0","0","0","0","0","0","0"};//月份

arrValueNames[0] = "一月";
arrValueNames[1]="二月";
arrValueNames[2] = "三月";
arrValueNames[3]="四月";
arrValueNames[4]="五月";
arrValueNames[5]="六月";
arrValueNames[6] = "七月";
arrValueNames[7]="八月";
arrValueNames[8]="九月";
arrValueNames[9]="十月";

//得出矩形宽度,和画图X轴位置

//[项目宽度]=[总宽度]/[项目数量]
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2 

System.Drawing.Point[] pi=new Point[arrValues.Length]; //定义折线点的对象数组
System.Drawing.Point[] pit=new Point[3]; //定义坐标三角点的对象数组
System.Drawing.Pen pe=new Pen(new SolidBrush(GetColor(7)),1f); //定义画直线的对象
//画纵轴
objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ChartLeft,ChartTop));
//画纵轴终点箭头
pit[0].X=ImgWidth-ChartRight; //确定三角形三点的位置
pit[0].Y=ImgHeight-ChartBottom-4;
pit[1].X=ImgWidth-ChartRight;
pit[1].Y=ImgHeight-ChartBottom+4;
pit[2].X=ImgWidth-ChartRight+10;
pit[2].Y=ImgHeight-ChartBottom;
objGps.FillPolygon(new SolidBrush(GetColor(7)),pit);
//画纵轴标尺和标尺描述
for(int i=1;i<=YItemNum;i++)
{
//画标尺
objGps.DrawLine(pe,new PointF(ChartLeft,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i),new PointF(ChartLeft-5,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i));
//画描述
objGps.DrawString(arrValueNames[i-1].ToString(),new Font("宋体",10),Brushes.Black,new Point(YStrStart,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i-5));
}
//画横轴
objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ImgWidth-ChartRight,ImgHeight-ChartBottom));
//画横轴终点箭头
pit[0].X=ChartLeft-4; //确定三角形三点的位置
pit[0].Y=ChartTop;
pit[1].X=ChartLeft+4;
pit[1].Y=ChartTop;
pit[2].X=ChartLeft;
pit[2].Y=ChartTop-10;
objGps.FillPolygon(new SolidBrush(GetColor(7)),pit);
//画横轴标尺和标尺描述
for(int i=1;i<=ItemNum;i++)
{
objGps.DrawLine(pe,new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom),new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom+5));
objGps.DrawString(arrValueNames[i-1].ToString(),new Font("宋体",10),Brushes.Black,new Point(ChartLeft+childspace+itemwidth*(i-1),ImgHeight-ChartBottom+XStrStart));
}

for(int j = 0;j<arrValues.Length;j++)//画矩形图和折线图
{
objGps.FillRectangle(new SolidBrush(GetColor(j)),(j*(childspace+rectanglewidth))+childspace+ChartLeft,ImgHeight-ChartBottom-arrValues[j],rectanglewidth,arrValues[j]);
objGps.DrawRectangle(Pens.Black,(j*(childspace+rectanglewidth))+childspace+ChartLeft,ImgHeight-ChartBottom-arrValues[j],rectanglewidth,arrValues[j]);
pi[j].X=(j*(childspace+rectanglewidth))+childspace+ChartLeft;
pi[j].Y=ImgHeight-ChartBottom-arrValues[j];
pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,System.Drawing.Drawing2D.LineCap.RoundAnchor,System.Drawing.Drawing2D.DashCap.Round);
if(j>0)
{
objGps.DrawLine(pe,pi[j-1],pi[j]);
}
}

objBitMap.Save(Response.OutputStream,ImageFormat.Gif);//该位图对象以"GIF"格式输出
}
/** <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
/// <returns></returns>
public static Color GetColor(int itemIndex)
{
Color objColor = new Color();
switch(itemIndex)
{
case 0:
objColor = Color.Blue;
break;
case 1:
objColor = Color.Yellow;
break;
case 2:
objColor = Color.Red;
break;
case 3:
objColor = Color.Orange;
break;
case 4:
objColor = Color.Purple;
break;
case 5:
objColor = Color.Brown;
break;
case 6:
objColor = Color.Pink;
break;
default:
objColor = Color.Black;
break;
}
return objColor;
}
/** <param name="red">自定义颜色红色分量值,有效值0到255</param>
/// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
/// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
/// <returns></returns>
public static Color GetColor(int red,int green,int blue)
{
Color objColor = new Color();
objColor = Color.FromArgb(red,green,blue);
return objColor;
}

你还可能感兴趣:
使用AspNetPager控件分页

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157696.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月27日 下午3:16
下一篇 2022年7月27日 下午3:16


相关推荐

  • JAVAWEB酒店管理系统[通俗易懂]

    JAVAWEB酒店管理系统[通俗易懂]酒店管理系统分为前台和后台两个部分,其中后台供管理员管理系统之用,包括客房类型设置模块、客房设置模块以及操作员设置三个子模块,具体的功能模块如下。客房类型设置模块:该模块用来管理酒店的所有客房类型,包括新增客房类型、编辑已有客房类型、删除客房类型等功能。客房设置模块:该模块用来管理酒店的所有客房信息,包括新增客房、编辑已有客房、删除客房等功能。操作员设置模块:该模块用来管理酒店的操作员信息,包括新增操作员、编辑已有操作员信息、删除操作信息等功能。系统前台供酒店所有工作人员使用,包括入住登记模块、结账

    2026年2月20日
    4
  • nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)

    nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。需要优化的两个目标为特征数和精度。nsga2是一个多目标优化算法。具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:MATLABfunction…

    2022年5月19日
    42
  • MAC安装JDK及环境变量配置「建议收藏」

    MAC安装JDK及环境变量配置「建议收藏」访问Oracle官网http://www.oracle.com,浏览到首页的底部菜单,然后按下图提示操作:  2.点击“JDKDOWNLOAD”按钮:  3.选择“AcceptLisenceAgreement”同意协议:  4.点击MacOSXx64后面的下载链接: 

    2022年7月17日
    17
  • R及RStudio下载安装教程(超详细)

    R及RStudio下载安装教程(超详细)R语言是为数学研究工作者设计的一种数学编程语言,主要用于统计分析、绘图、数据挖掘。如果你是一个计算机程序的初学者并且急切地想了解计算机的通用编程,R语言不是一个很理想的选择,可以选择Python、C或Java。R语言与C语言都是贝尔实验室的研究成果,但两者有不同的侧重领域,R语言是一种解释型的面向数学理论研究工作者的语言,而C语言是为计算机软件工程师设计的。R语言是解释运行的语言(与C语言的编译运行不同),它的执行速度比C语言慢得多,不利于优化。但它在语法层面提供…

    2022年6月15日
    51
  • 【STM32】UART和USART的区别[通俗易懂]

    【STM32】UART和USART的区别[通俗易懂]1、UART:universalasynchronousreceiverandtransmitter通用异步收发器;2、USART:universalsynchronousasynchronousreceiverandtransmitter通用同步异步收发器。1、通用异步收发器:通用异步收发器是一种通用串行数据总线,用于异步通信。该总线双向通信,可以实现全双工传…

    2022年5月19日
    42
  • byte数组截取[通俗易懂]

    byte数组截取[通俗易懂]//原始数组byte[]bytes=ImageUtils.toByteArray(fromPaths[0]);//新数组byte[]b1=newbyte[bytes.length-80];//从原始数组80位置开始截取后面所有System.arraycopy(bytes,80,b1,0,bytes.length-80);BufferData2DbufferDat…

    2022年6月11日
    220

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号