您的位置:首页 > 编程语言 > ASP

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

2008-05-01 03:05 316 查看
StockPicker.ASPx:
<script language="VB" runat=server>
Sub ChartBtn_Click(Sender as Object, E as EventArgs)
chart.ImageUrl = "ImageGenerator_Vb.ASPx?"
chart.Visible = true
For i=0 to Stocks.Items.Count-1
If (Stocks.Items(i).Selected = true) Then
chart.ImageUrl = chart.ImageUrl & "symbols=" & Stocks.Items(i).Value & "&"
End If
Next
End Sub
</script>
<html>
<body>
<form runat=server>
<h1>Scott's Stock Picker</h1>
<ASP:checkboxlist id="Stocks" runat=server>
<ASP:listitem>MSFT</ASP:listitem>
<ASP:listitem>SUN</ASP:listitem>
</ASP:checkboxlist>
<ASP:button text="Chart Your Selected Stocks" OnClick="ChartBtn_Click" runat=server/>
<hr>
<ASP:Image id="chart" ImageUrl="" Visible=false runat=server/>
</form>
</body>
</html>

ImageGenerator_VB.ASPx:
<%@ Page Language="VB" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="ChartGenerator" %>
<%@ OutputCache Duration="10" %>
<script language="VB" runat=server>
Function GetStockDetails(Symbol as String) as ChartLine
Dim myChartLine as new ChartLine
if (symbol = "msft") then
Dim StockValues() as Single = { 60, 110, 120, 180, 185, 190, 240, 290 }
myChartLine.Width = 5
myChartLine.Color = Color.Blue
myChartLine.LineStyle = DashStyle.Solid
myChartLine.Title = "Microsoft Corp. (MSFT)"
myChartLine.Symbol = "MSFT"
myChartLine.Values = StockValues
return myChartLine
elseif (symbol = "sun") then
Dim StockValues() as Single = { 180, 155, 125, 60, 25, 15, 10, 3 }
myChartLine.Width = 5
myChartLine.Color = Color.Red
myChartLine.LineStyle = DashStyle.Dot
myChartLine.Title = "Sun Corp. (Sun)"
myChartLine.Symbol = "Sun"
myChartLine.Values = StockValues
return myChartLine
end if
return nothing
End Function
Sub Page_Load(Sender as Object, E as EventArgs)
' Generate Chart Data For Image....
Dim XAxes() as String = { "9:00AM", "9:30AM", "10:00AM", "11:00AM", "12:00AM", "1:00PM", "1:30PM" }
Dim MyChartData as New ChartData
MyChartData.YTickSize = 20
MyChartData.YMax = 250
MyChartData.YMin = 0
MyChartData.XAxisTitles = XAxes
Dim Symbols() as String = Request.QueryString.GetValues("symbols")
if (Not Symbols = Nothing) then
for i=0 to Symbols.Length-1
Dim stockValue as ChartLine = GetStockDetails(symbols(i).ToLower)
If (stockValue <> nothing) then
myChartData.Lines.Add(stockValue)
End if
Next
end if
' Create In-Memory BitMap of JPEG
Dim MyChartEngine as New ChartEngine
Dim StockBitMap as BitMap = MyChartEngine.DrawChart(600, 400, myChartData)
' Render BitMap Stream Back To Client
StockBitMap.Save(Response.OutputStream, ImageFormat.JPEG)
End Sub
</script>

ChartEngine.cs:
using System.WinForms;
using System.Collections;
using System.Collections.Bases;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.ComponentModel;
using System;
using System.IO;
namespace ChartGenerator {
//Core Line Data structure
public struct LineData {
public float[] LineValues ;
public string LiNETitle ;
public string LineSymbol ;
}

//Line Data plus display style information
public class ChartLine {
private Color lineColor ;
private LineData lineData ;
private DashStyle lineStyle ;
private int lineWidth ;
//Constructors
public ChartLine() :base() {}
public ChartLine(LineData lineData) :base() {
this.lineData = lineData;
}
//Properties
public Color Color {
get { return lineColor ; }
set { lineColor = value ; }
}
public DashStyle LineStyle {
get { return lineStyle ; }
set { lineStyle = value ; }
}

public string Symbol {
get { return lineData.LineSymbol ; }
set { lineData.LineSymbol = value ; }
}
public string Title {
get { return lineData.LiNETitle ; }
set { lineData.LiNETitle = value ; }
}
public float[] Values {
get { return lineData.LineValues ; }
set { lineData.LineValues = value ; }
}
public int Width {
get { return lineWidth ; }
set { lineWidth = value ; }
}

//Methods
public void SetLineData(LineData lineData) {
this.lineData = lineData;
}
}

//Chart Data structure
public class ChartData {
private float yTickSize;
private float yMax;
private float yMin;
private string[] xAxisTitles ;
private ChartLineList lines = new ChartLineList();
private Color gridColor=Color.Blue;
private bool showHGridLines=true;
private bool showVGridLines=true;
//Properties
public float YTickSize {
get { return yTickSize ; }
set { yTickSize = value ; }
}
public float YMax {
get { return yMax ; }
set { yMax = value ; }
}

public float YMin {
get { return yMin ; }
set { yMin = value ; }
}
public string[] XAxisTitles {
get { return xAxisTitles ; }
set { xAxisTitles = value ; }
}
public ChartLineList Lines {
get { return lines ; }
set { lines = value ; }
}
public Color GridColor {
get { return gridColor ; }
set { gridColor = value ; }
}
public bool ShowHGridLines {
get { return showHGridLines ; }
set { showHGridLines = value ; }
}
public bool ShowVGridLines {
get { return showVGridLines ; }
set { showVGridLines = value ; }
}
//Collection of Chart Lines
public class ChartLineList : TypedCollectionBase {
public ChartLine this[int index] {
get {
return (ChartLine)(List[index]);
}
set {
List[index] = value;
}
}
public int Add(ChartLine value) {
return List.Add(value);
}
public void Insert(int index, ChartLine value) {
List.Insert(index, value);
}
public int IndexOf(ChartLine value) {
return List.IndexOf(value);
}
public bool Contains(ChartLine value) {
return List.Contains(value);
}
public void Remove(ChartLine value) {
List.Remove(value);
}
public void CopyTo(ChartLine[] array, int index) {
List.CopyTo(array, index);
}
}
}

//Charting Engine - draws a chart based on the given ChartData
public class ChartEngine {
private ChartData chartData ;
private float left;
private float right;
private float top;
private float bottom;
private float tickCount;
private float yCount;
private float hspacing;
private float vspacing;
private Graphics g;
private Rectangle r;
private Color backColor;
private Color foreColor;
private Font baseFont;
private Font legendFont;
private RectangleF legendRect;
public ChartEngine() {
}
public Bitmap DrawChart(int width, int height, ChartData chartData) {
Bitmap newBitmap = new Bitmap(width,height,PixelFormat.Format32bppARGB);
Graphics g = Graphics.FromImage(newBitmap);
Rectangle r = new Rectangle(0, 0, width, height);
Color myForeColor = Color.Black;
Color myBackColor = Color.White;
Font myFont = new Font("Arial", 10);
this.DrawChart(g, r, myBackColor, myForeColor, myFont, chartData);
return newBitmap;
}
public void DrawChart(Graphics g, Rectangle r, Color backColor, Color foreColor, Font baseFont, ChartData chartData) {
this.chartData = chartData;
this.g = g;
this.r = r;
共2页: 上一页 1 [2] 下一页
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: