分享
 
 
 

C#绘图(可以处理负值)

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

using System;

using System.IO;//用于文件存取

using System.Data;//用于数据访问

using System.Drawing;//提供画GDI+图形的基本功能

using System.Drawing.Text;//提供画GDI+图形的高级功能

using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能

using System.Drawing.Imaging;//提供画GDI+图形的高级功能

namespace Drawpicture

{

/// <summary>

/// BarChart 的摘要说明。

/// </summary>

public class BarChart

{

private int Pic_Width = 410;

private int Pic_Height = 320;

public static bool hasNeg = false;

private const string FontWord = "Arial";

private const int FontSize = 9;

public int SIDE_WIDTH = 400;

public int SIDE_HEIGHT = 400;

private const int CHART_TOP = 60;

private int CHART_HEIGHT = 300;

private const int CHART_LEFT = 60;

private const int CHART_WIDTH = 300;

public void Render(string[] word,int[] data, out string fileName)

{

for(int i=0;i<data.Length;i++)

{

if(data[i]<0)

{

Pic_Height *=2;

hasNeg = true;

SIDE_HEIGHT = (int)(SIDE_HEIGHT*1.5);

CHART_HEIGHT =(int)(CHART_HEIGHT*0.3);

break;

}

}

ChartUtil cu = new ChartUtil();

PointF[] zuoBiaoStart;

PointF[] zuoBiaoEnd;

Graphics g;

Bitmap bm;

createCanvas(out bm, out g, out zuoBiaoEnd, out zuoBiaoStart,cu);

//画坐标轴

int chiDu = drawAxes(data, zuoBiaoStart, g, zuoBiaoEnd,cu);

//画条形图

float barWidth = CHART_WIDTH / (2 * 2);

PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);

float barHeight = 2;

for(int i=0;i<2;i++)

{

barHeight = ((float)data[i]/chiDu) *(CHART_HEIGHT/5) ;

if(barHeight<0)

{

barOrigin.Y = CHART_TOP + CHART_HEIGHT;

}

else

{

barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;

}

g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,Math.Abs(barHeight));

barOrigin.X = barOrigin.X + (barWidth * 2);

}

//画右上角的说明图形

drawInstruction(CHART_LEFT, CHART_HEIGHT, CHART_TOP, g, word, data);

//输出图形

fileName = cu.PicPath + Guid.NewGuid().ToString() + ".gif";

bm.Save(fileName, ImageFormat.Gif);

//资源回收

bm.Dispose();

g.Dispose();

}

private int drawAxes(int[] data, PointF[] zuoBiaoStart, Graphics g, PointF[] zuoBiaoEnd,ChartUtil cu)

{

int chiDu =1;

int[] t = cu.getZuoBiaoValue(data,out chiDu);

for(int i=0;i<zuoBiaoStart.Length;i++)

{

PointF txtPos = new PointF();

txtPos.X = zuoBiaoStart[i].X -50;

txtPos.Y = zuoBiaoStart[i].Y - 5;

g.DrawString(t[i].ToString(),new Font(FontWord,8),Brushes.Black,txtPos);

g.DrawLine(Pens.Black,zuoBiaoStart[i],zuoBiaoEnd[i]);

}

return chiDu;

}

private void createCanvas(out Bitmap bm, out Graphics g, out PointF[] zuoBiaoEnd, out PointF[] zuoBiaoStart,ChartUtil cu)

{

//建立一个Graphics对象实例

bm = new Bitmap(Pic_Width,Pic_Height);

g = Graphics.FromImage(bm);

//设置条图图形和文字属性

g.ScaleTransform((Convert.ToSingle(Pic_Width))/SIDE_WIDTH,(Convert.ToSingle(Pic_Height))/SIDE_HEIGHT);

g.SmoothingMode = SmoothingMode.Default;

g.TextRenderingHint = TextRenderingHint.AntiAlias;

//设定画布和边

g.Clear(Color.White);

g.DrawRectangle(Pens.Black,0,0,SIDE_WIDTH-1,SIDE_HEIGHT-1);

//设置条形图的边

g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP,CHART_WIDTH, CHART_HEIGHT);

if(hasNeg)

{

g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP+CHART_HEIGHT,CHART_WIDTH, CHART_HEIGHT);

}

zuoBiaoEnd = null;

zuoBiaoStart = cu.getZuoBiaoPoint(new PointF(CHART_TOP,CHART_LEFT),CHART_HEIGHT,CHART_WIDTH,out zuoBiaoEnd,hasNeg);

}

private void drawInstruction(int CHART_LEFT, int CHART_HEIGHT, int CHART_TOP, Graphics g, string[] word, int[] data)

{

PointF colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT + CHART_TOP + 2);

if(hasNeg)

{

colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT*2 + CHART_TOP + 2);

}

PointF boxOrigin = new PointF(Pic_Width*5/6-30,CHART_TOP*1/3);

PointF textOrigin = new PointF(Pic_Width*4/5+8,CHART_TOP*1/3 -3 );

for(int i=0;i<2;i++)

{

g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);

//g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);

g.DrawString(word[i],new Font(FontWord,FontSize),Brushes.DarkBlue,colsNamePoint);

g.DrawString(data[i].ToString(),new Font(FontWord,FontSize),Brushes.Black,textOrigin);

colsNamePoint.X += 150;

boxOrigin.Y += 15;

textOrigin.Y += 15;

}

}

}

public class ChartUtil

{

public readonly string PicPath = System.AppDomain.CurrentDomain.BaseDirectory + "Temp/";

public PointF[] getZuoBiaoPoint(PointF orign,int height,int width,out PointF[] endPoint,bool hasNeg)

{

int num = 6;

if(hasNeg) num = 11;

PointF[] startPoint = new PointF[num];

endPoint = new PointF[num];

for(int i =0;i<num;i++)

{

PointF pfStart = new PointF();

PointF pfEnd = new PointF();

pfStart.X = orign.X;

pfEnd.X = orign.X + width;

pfStart.Y = orign.Y + i*height/5;

pfEnd.Y = pfStart.Y;

startPoint[i] = pfStart;

endPoint[i] = pfEnd;

}

return startPoint;

}

public int[] getZuoBiaoValue(int[] data,out int chidu)

{

int[] result = new Int32[11];

int Max = data[0];

for(int i=0;i<data.Length;i++)

{

if(Max < data[i])

Max = data[i];

}

string strMax = Max + "";

chidu =(int)((Convert.ToInt32(strMax.Substring(0,1)) +1)* Math.Pow(10,strMax.Length-1))/5;

for(int i=0;i<result.Length;i++)

{

result[i] = chidu* (5-i) ;

}

return result;

}

#region get color

public static Color GetChartItemColor(int itemIndex)

{

Color selectedColor;

switch(itemIndex)

{

case 0:

selectedColor = Color.Blue;

break;

case 1:

selectedColor = Color.Red;

break;

case 2:

selectedColor = Color.Yellow;

break;

case 3:

selectedColor = Color.Purple;

break;

default:

selectedColor = Color.Green;

break;

}

return selectedColor;

}

#endregion

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有