Click here to Skip to main content
15,894,017 members
Articles / Web Development / ASP.NET

Race to Linux - Race 3: Reports Starter Kit using Mono SqlServer/Firebird

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
30 Sep 20052 min read 53.1K   328   15  
Reports Starter Kit port to Linux using Mono
<html><head><link rel=stylesheet href=style.css></head><body><div class=SourcePanel style='font-size:12'><pre style='background-color:white'>
<font color= "blue">using</font> System;
<font color= "blue">using</font> System.Collections;
<font color= "blue">using</font> System.Drawing;
<font color= "blue">using</font> System.Drawing.Imaging;
<font color= "blue">using</font> System.Drawing.Drawing2D;
<font color= "blue"></font>
<font color= "blue">namespace</font> ASPNET.StarterKit.Chart
<font color= "blue"></font>{
<font color= "green">    //*********************************************************************</font>
<font color= "green">    //</font>
<font color= "green">    // PieChart Class</font>
<font color= "green">    //</font>
<font color= "green">    // This class uses GDI+ to render Pie Chart.</font>
<font color= "green">    //</font>
<font color= "green">    //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">    public class</font> PieChart : Chart
<font color= "blue">    </font>{
<font color= "blue">        private const </font>int    _bufferSpace = 125;
<font color= "blue">        private </font>ArrayList    _chartItems;
<font color= "blue">        private </font>int            _perimeter;
<font color= "blue">        private </font>Color        _backgroundColor;
<font color= "blue">        private </font>Color        _borderColor;
<font color= "blue">        private </font>float        _total;
<font color= "blue">        private </font>int            _legendWidth;
<font color= "blue">        private </font>int            _legendHeight;
<font color= "blue">        private </font>int            _legendFontHeight;
<font color= "blue">        private </font>string        _legendFontStyle;
<font color= "blue">        private </font>float        _legendFontSize;
<font color= "blue"></font>
<font color= "blue">        public </font>PieChart()
<font color= "blue">        </font>{
<font color= "blue">            </font>_chartItems = new ArrayList();
<font color= "blue">            </font>_perimeter = 250;
<font color= "blue">            </font>_backgroundColor = Color.White;
<font color= "blue">            </font>_borderColor = Color.FromArgb(63,63,63);
<font color= "blue">            </font>_legendFontSize = 8;
<font color= "blue">            </font>_legendFontStyle = "Verdana";
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>PieChart(Color bgColor)
<font color= "blue">        </font>{
<font color= "blue">            </font>_chartItems = new ArrayList();
<font color= "blue">            </font>_perimeter = 250;
<font color= "blue">            </font>_backgroundColor = bgColor;
<font color= "blue">            </font>_borderColor = Color.FromArgb(63,63,63);
<font color= "blue">            </font>_legendFontSize = 8;
<font color= "blue">            </font>_legendFontStyle = "Verdana";
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method collects all data points and calculate all the necessary dimensions </font>
<font color= "green">        // to draw the chart.  It is the first method called before invoking the Draw() method.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        public void</font> CollectDataPoints(string[] xValues, string[] yValues)
<font color= "blue">        </font>{
<font color= "blue">            </font>_total = 0.0f;
<font color= "blue">            </font>
<font color= "blue">            for</font> (int i = 0;i < xValues.Length;i++)
<font color= "blue">            </font>{
<font color= "blue">                </font>float ftemp = Convert.ToSingle(yValues[i]);
<font color= "blue">                </font>_chartItems.Add(new ChartItem(xValues[i], xValues.ToString(), ftemp, 0, 0, Color.AliceBlue));
<font color= "blue">                </font>_total += ftemp;
<font color= "blue">            </font>}
<font color= "blue">            </font>
<font color= "blue">            </font>float nextStartPos = 0.0f;
<font color= "blue">            </font>int counter = 0;
<font color= "blue">            </font>foreach (ChartItem item in _chartItems)
<font color= "blue">            </font>{
<font color= "blue">                </font>item.StartPos = nextStartPos;
<font color= "blue">                </font>item.SweepSize = item.Value / _total * 360;
<font color= "blue">                </font>nextStartPos = item.StartPos + item.SweepSize;
<font color= "blue">                </font>item.ItemColor = GetColor(counter++);
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>CalculateLegendWidthHeight();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method returns a bitmap to the calling function.  This is the method</font>
<font color= "green">        // that actually draws the pie chart and the legend with it.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        public </font>override Bitmap Draw()
<font color= "blue">        </font>{
<font color= "blue">            </font>int perimeter = _perimeter;
<font color= "blue">            </font>Rectangle pieRect = new Rectangle(0, 0, perimeter, perimeter-1);
<font color= "blue">            </font>Bitmap bmp = new Bitmap(perimeter + _legendWidth, perimeter);
<font color= "blue">            </font>Graphics grp = Graphics.FromImage(bmp);
<font color= "blue">        </font>
<font color= "green">            //Paint Back ground</font>
<font color= "blue">            </font>grp.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, perimeter + _legendWidth, perimeter);
<font color= "blue"></font>
<font color= "green">            //Align text to the right</font>
<font color= "blue">            </font>StringFormat sf = new StringFormat();
<font color= "blue">            </font>sf.Alignment = StringAlignment.Far; 
<font color= "blue">            </font>
<font color= "green">            //Draw all wedges and legends</font>
<font color= "blue">            </font>for(int i=0; i<_chartItems.Count; i++)
<font color= "blue">            </font>{
<font color= "blue">                </font>ChartItem item = (ChartItem) _chartItems[i];
<font color= "blue">                </font>SolidBrush brs = new SolidBrush(item.ItemColor);
<font color= "blue">                </font>grp.FillPie(brs, pieRect, item.StartPos, item.SweepSize);
<font color= "blue">                </font>grp.FillRectangle(brs, perimeter + _bufferSpace, i * _legendFontHeight + 15, 10, 10);
<font color= "blue">                </font>grp.DrawString(item.Label, new Font(_legendFontStyle, _legendFontSize), 
<font color= "blue">                    </font>new SolidBrush(Color.Black), perimeter + _bufferSpace + 20, i * _legendFontHeight + 13);
<font color= "blue"></font>
<font color= "blue">                </font>grp.DrawString(item.Value.ToString("C"), new Font(_legendFontStyle, _legendFontSize), 
<font color= "blue">                    </font>new SolidBrush(Color.Black), perimeter + _bufferSpace + 200, i * _legendFontHeight + 13,sf);
<font color= "blue">            </font>}
<font color= "blue">            </font>
<font color= "green">            //draws the border around Pie</font>
<font color= "blue">            </font>grp.DrawEllipse(new Pen(_borderColor, 2), pieRect);  
<font color= "blue"></font>
<font color= "green">            //draw border around legend</font>
<font color= "blue">            </font>grp.DrawRectangle(new Pen(_borderColor, 1), perimeter + _bufferSpace - 10, 10, 220, _chartItems.Count * _legendFontHeight + 25);
<font color= "blue"></font>
<font color= "green">            //Draw Total under legend</font>
<font color= "blue">            </font>grp.DrawString("Total", new Font(_legendFontStyle, _legendFontSize, FontStyle.Bold), 
<font color= "blue">                </font>new SolidBrush(Color.Black), perimeter + _bufferSpace + 30, (_chartItems.Count+1) * _legendFontHeight,sf);
<font color= "blue">            </font>grp.DrawString(_total.ToString("C"), new Font(_legendFontStyle, _legendFontSize, FontStyle.Bold), 
<font color= "blue">                </font>new SolidBrush(Color.Black), perimeter + _bufferSpace + 200, (_chartItems.Count+1) * _legendFontHeight,sf);
<font color= "blue">            </font>
<font color= "blue">            </font>grp.SmoothingMode = SmoothingMode.AntiAlias;
<font color= "blue">            </font>grp.Dispose();
<font color= "blue">            return</font> bmp;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        //    This method calculates the space required to draw the chart legend.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue">        </font>
<font color= "blue">        private void</font> CalculateLegendWidthHeight()
<font color= "blue">        </font>{
<font color= "blue">            </font>Font fontLegend = new Font(_legendFontStyle, _legendFontSize);
<font color= "blue">            </font>_legendFontHeight = fontLegend.Height+5;
<font color= "blue">            </font>_legendHeight = fontLegend.Height * (_chartItems.Count + 1);
<font color= "blue"></font><font color= "blue">            if </font>(_legendHeight > _perimeter) _perimeter = _legendHeight;<font color= "blue"></font>
<font color= "blue"></font>
<font color= "blue">            </font>_legendWidth = _perimeter + _bufferSpace;
<font color= "blue">        </font>}
<font color= "blue">    </font>}
<font color= "blue"></font>}
</pre>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Uruguay Uruguay
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions