盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

用ASP.NET动态生成图像(转2)

时间:2024/11/4作者:未知来源:盾怪网教程人气:

[摘要]FontStyle.Bold);g.SmoothingMode = SmoothingMode.AntiAlias;CalculateChartDimensions(); DrawBackgroun...
FontStyle.Bold);
g.SmoothingMode = SmoothingMode.AntiAlias;
CalculateChartDimensions();
DrawBackground();
InternalDrawChart() ;
}

private void CalculateChartDimensions() {
right = r.Width - 5;
top = 5 * baseFont.Size ;
bottom = r.Height - baseFont.Size * 2;
tickCount = chartData.YMin ;
yCount = (chartData.YMax-chartData.YMin) / chartData.YTickSize ;
hspacing = (bottom-top) / yCount;
vspacing = (right) / chartData.XAxisTitles.Length;
//Left depends on width of text - for simplicities sake assume that largest yvalue is the biggest
//Take into account the first X Axis title
float maxYTextSize = g.MeasureString(chartData.YMax.ToString(), baseFont).Width;
float firstXTitle = g.MeasureString(chartData.XAxisTitles[0], baseFont).Width;
left = (maxYTextSize > firstXTitle) ? maxYTextSize : firstXTitle ;
left = r.X + left + 5 ;
//Calculate size of legend box
float maxLegendWidth = 0 ;
float maxLegendHeight = 0 ;
//Work out size of biggest legend
foreach (ChartLine cl in chartData.Lines) {
float currentWidth = g.MeasureString(cl.Title, legendFont).Width;
float currentHeight = g.MeasureString(cl.Title, legendFont).Height;
maxLegendWidth = (maxLegendWidth > currentWidth) ? maxLegendWidth : currentWidth ;
maxLegendHeight = (maxLegendHeight > currentHeight) ? maxLegendHeight : currentHeight ;
}
legendRect = new RectangleF(r.X+2, r.Y+2, maxLegendWidth + 25 + 5, ((maxLegendHeight+2)*chartData.Lines.Count) + 3) ;
}
private void DrawBackground() {
LinearGradientBrush b = new LinearGradientBrush(r, Color.SteelBlue, backColor,LinearGradientMode.Horizontal);
g.FillRectangle(b, r);
b.Dispose();
}
private void InternalDrawChart() {
DrawGrid() ;
foreach (ChartLine cl in chartData.Lines) {
DrawLine(cl);
}
DrawLegend() ;
//Draw time on chart
string timeString = "Generated:" + DateTime.Now.ToLongTimeString() ;
SizeF textsize = g.MeasureString(timeString,baseFont);
g.DrawString(timeString, baseFont, new SolidBrush(foreColor), r.Width - textsize.Width - 5, textsize.Height * 2 / 3) ;
}
private void DrawGrid() {
Pen gridPen = new Pen(chartData.GridColor) ;

//Vertical - include tick desc's
if (chartData.ShowVGridLines) {
for (int i = 0 ; (vspacing * i) < right; i++) {
float x = left + (vspacing *i);
string desc = chartData.XAxisTitles[i];
g.DrawLine(gridPen, x,top,x,bottom +(baseFont.Size*1/3));
SizeF textsize = g.MeasureString(desc,baseFont);
g.DrawString(desc, baseFont, new SolidBrush(chartData.GridColor), x-(textsize.Width/2), bottom + (baseFont.Size*2/3)) ;
}
}
//Horizontal
if (chartData.ShowHGridLines) {
for (float i = bottom ; i > top; i-=hspacing) {
string desc = tickCount.ToString();
tickCount+=chartData.YTickSize;
g.DrawLine(gridPen, right, i, left-3, i);
SizeF textsize = g.MeasureString(desc,baseFont);
g.DrawString(desc, baseFont, new SolidBrush(chartData.GridColor), left-textsize.Width - 3, i - (textsize.Height/2)) ;
}
}
}
private void DrawLine(ChartLine chartLine) {
Pen linePen = new Pen(chartLine.Color);
linePen.StartCap = LineCap.Round;
linePen.EndCap = LineCap.Round;
linePen.Width = chartLine.Width ;
linePen.DashStyle = chartLine.LineStyle;
PointF[] Values = new PointF[chartLine.Values.Length];
float scale = hspacing / chartData.YTickSize ;
for (int i = 0 ; i < chartLine.Values.Length; i++) {
float x = left + vspacing * i;
Values[i] = new PointF(x, bottom-chartLine.Values[i]*scale);
}
g.DrawLines(linePen, Values);
}
private void DrawLegend() {
//Draw Legend Box
ControlPaint.DrawBorder(g, (Rectangle)legendRect, SystemColors.WindowFrame, ButtonBorderStyle.Solid);
LinearGradientBrush b = new LinearGradientBrush(legendRect, backColor, Color.SteelBlue, LinearGradientMode.Horizontal);
r.Inflate(-1, -1);
g.FillRectangle(b, legendRect);
b.Dispose();
float startY = 5;
foreach (ChartLine cl in chartData.Lines) {
Pen p = new Pen(cl.Color) ;
p.Width = p.Width*4;
SizeF textsize = g.MeasureString(cl.Title, legendFont);
float lineY = startY + textsize.Height / 2 ;
g.DrawLine(p, r.X + 7, lineY, r.X + 25, lineY);
g.DrawString(cl.Title, legendFont, new SolidBrush(foreColor), r.X + 30, startY);
startY += (textsize.Height+2);
}
}
}
}




关键词:用ASP.NET动态生成图像(转2)




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版