原始碼推薦:一個使用C#繪製圖形引擎的Framework (轉)
Batman
下面將推薦一個可以很方便的生成多種圖形格式的,使用了+技術的圖形
引擎程式碼。它的主要描述如下:
1.可以生成的圖形格式支援BMP
EMF,GIF,Icon,JPEG,PNG,TIFF,WMF
2.在檔案ChartEngine.cs中為這個圖形引擎的大量關鍵程式碼
StockPicker.檔案是一個使用該引擎繪圖的例子
3.要使用這個圖形引擎,需要把所有的程式碼檔案複製到你的的某個
運用目錄(注:不是普通目錄)中,然後在該目錄下建立一個bin目錄,
然後執行mk.bat
4.ImageGenerator_VB.asp檔案的頭使用了 %>來說明生成的是某種格式的圖形檔案
5.為了考慮,還可以在ImageGenerator_VB.asp中增加以下程式碼
,該程式碼可以將生成的圖形檔案快取起來
這樣如果網站訪問量大時,可以大大減輕伺服器的負擔
好了,廢話少說,大家可以自己研究研究程式碼:
1.ChartEngine.cs檔案
using System.s;
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 Litle ;
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;
this.backColor = backColor;
this.foreColor = foreColor;
this.baseFont = baseFont;
this.legendFont = new Font(baseFont.FontFamily,
(baseFont.Size * 2/3), baseFont.Style | 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 = DateTime.ToString(DateTime.Now) ;
SizeF textsize = g.MeasureString(timeString,baseFont);
g.DrawString(timeString, baseFont, new Solirush(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);
}
}
}
}
2.ImageGenerator_Vb.aspx檔案
3.StockPicker.aspx檔案
tt's Stock Picker
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987493/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 推薦12個最好的 JavaScript 圖形繪製庫JavaScript
- 使用css繪製圖形CSS
- 使用CreateJS繪製圖形JS
- iOS 繪圖 - 如何繪製一個多邊形能力分佈圖iOS繪圖
- java:繪製圖形Java
- HTML5使用canvas繪製圖形HTMLCanvas
- VB原始碼推薦:一個操作Ini檔案的類 (轉)原始碼
- 推薦一款漂亮的 Java 圖形驗證碼Java
- MATLAB圖形繪製練習(一)Matlab
- css繪製特殊圖形CSS
- 使用Matplotlib繪製3D圖形3D
- 如何使用 Arduino 製作一個繪圖儀UI繪圖
- 利用 Matplotlib 繪製資料圖形(一)
- 如何繪製一個類甘特圖 (附原始碼)原始碼
- 分段函式圖形繪製函式
- javascript繪製心形圖案程式碼例項JavaScript
- 13個JavaScript圖表(JS圖表)圖形繪製外掛JavaScriptJS
- 推薦一個製作「ASCII 流程圖」工具——Graph EasyASCII流程圖
- Excalidraw:繪製圖形的新利器
- Python基本圖形繪製--模組1:turtle庫的使用Python
- Python例項:僅繪製圖例而不繪製實際的圖形Python
- Shader 繪製基礎圖形
- 如何使用 css 繪製心形CSS
- VC++儲存繪製的圖形-1C++
- CSS3繪製圖形圖案效果CSSS3
- 從零開始做一個SLG遊戲(三):用unity繪製圖形遊戲Unity
- 史上最強大的40多個純CSS繪製的圖形CSS
- 開始使用SmartDraw繪製開發中的各種圖形
- 轉:rrdtool強大的繪圖的引擎繪圖
- 使用joinjs繪製流程圖(一)JS流程圖
- RecyclerView 原始碼分析(一) —— 繪製流程解析View原始碼
- css3繪製心形圖案程式碼例項CSSS3
- canvas核心技術-如何繪製圖形Canvas
- BitmapShader繪製圓形圖片
- OpenGL ES繪製3D圖形3D
- 使用 Flutter 繪製圖表(一)柱狀圖?Flutter
- 使用joinjs繪製流程圖(五)-流程圖繪製JS流程圖
- 如何使用Python和Plotly繪製3D圖形Python3D