Source code recommendation: A Framework using a C # draws graphics engine

zhaozj2021-02-08  254

Batman will recommend a graphics engine code that can be easily generated in a variety of graphics formats.

Its main description is as follows: 1. The graphical file format that can be generated supports BMP EMF, GIF, ICON, JPEG, PNG, TIFF, WMF2. In the file chartengine.cs, a large number of key code for this graphics engine is one. Examples of using this engine drawing 3. To use this graphics engine, you need to copy all code files to an application directory for your web (Note: Not a normal directory), then build a bin directory in this directory, then The header running the mk.bat4.imageGenerator_vb.asp file uses <% @ page contentType = "image / jpeg"%> to the generated graphics file 5. In order to consider server performance, you can also in ImageGenerator_vb.asp Increase the following code <% @ outputcache duration = "60"%>, the code can cache the generated graphic file so if the website is on the spot, you can greatly reduce the server's burden, nonsense, you can yourself Research Code: 1.ChartEngine.cs file 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 lineestyle {get {returnesteyle;} set {lineestyle = 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 sh owVGridLines = 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.com 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 transove (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; bottom private float;; private float left; private float right; private float top 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 = fo reColor; 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 = rX 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)? Maxleg endWidth: 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 VoidinAldrawchart () {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 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) 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 <

% @ Import namespace = "system.drawing"%> <% @ import namespace = "system.drawing.drawing2d"%> <% @ Import namespace = "system.drawing.imaging"%> <% @ import namespace = "chartgenerator "%>