Click here to Skip to main content
15,884,298 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 52.6K   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.Drawing;
<font color= "blue">using</font> System.Collections;
<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">    // BarGraph Class</font>
<font color= "green">    //</font>
<font color= "green">    // This class uses GDI+ to render Bar Chart.</font>
<font color= "green">    //</font>
<font color= "green">    //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">    public class</font> BarGraph : Chart
<font color= "blue">    </font>{
<font color= "blue">        private const </font>float _graphLegendSpacer = 15F;
<font color= "blue">        private const </font>int    _labelFontSize = 7;
<font color= "blue">        private const </font>int    _legendFontSize = 9;
<font color= "blue">        private const </font>float _legendRectangleSize = 10F;
<font color= "blue">        private const </font>float _spacer = 5F;
<font color= "blue"></font>
<font color= "green">        // Overall related members</font>
<font color= "blue">        private </font>Color    _backColor;
<font color= "blue">        private </font>string    _fontFamily;
<font color= "blue">        private </font>string    _longestTickValue = string.Empty;    // Used to calculate max value width
<font color= "blue">        private </font>float    _maxTickValueWidth;                    // Used to calculate left offset of bar graph
<font color= "blue">        private </font>float    _totalHeight;
<font color= "blue">        private </font>float    _totalWidth;
<font color= "blue">        </font>
<font color= "green">        // Graph related members</font>
<font color= "blue">        private </font>float    _barWidth;
<font color= "blue">        private </font>float    _bottomBuffer;    // Space from bottom to x axis
<font color= "blue">        private </font>bool    _displayBarData;    
<font color= "blue">        private </font>Color    _fontColor;
<font color= "blue">        private </font>float    _graphHeight;        
<font color= "blue">        private </font>float    _graphWidth;
<font color= "blue">        private </font>float    _maxValue = 0.0f;    // = final tick value * tick count
<font color= "blue">        private </font>float    _scaleFactor;        // = _maxValue / _graphHeight
<font color= "blue">        private </font>float    _spaceBtwBars;    // For now same as _barWidth
<font color= "blue">        private </font>float    _topBuffer;        // Space from top to the top of y axis
<font color= "blue">        private </font>float    _xOrigin;            // x position where graph starts drawing
<font color= "blue">        private </font>float    _yOrigin;            // y position where graph starts drawing
<font color= "blue">        private </font>string    _yLabel;
<font color= "blue">        private </font>int        _yTickCount;
<font color= "blue">        private </font>float    _yTickValue;        // Value for each tick = _maxValue/_yTickCount
<font color= "blue"></font>
<font color= "green">        // Legend related members</font>
<font color= "blue">        private </font>bool    _displayLegend;
<font color= "blue">        private </font>float    _legendWidth;
<font color= "blue">        private </font>string    _longestLabel = string.Empty;    // Used to calculate legend width
<font color= "blue">        private </font>float    _maxLabelWidth = 0.0f;
<font color= "blue"></font>
<font color= "blue">        public </font>string FontFamily {
<font color= "blue">            </font>get{ return _fontFamily; }
<font color= "blue">            </font>set{ _fontFamily = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>Color BackgroundColor {
<font color= "blue">            </font>set{ _backColor = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>int BottomBuffer {
<font color= "blue">            set</font> { _bottomBuffer = Convert.ToSingle(value); }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>Color FontColor {
<font color= "blue">            </font>set{ _fontColor = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>int Height {
<font color= "blue">            </font>get{ return Convert.ToInt32(_totalHeight); }
<font color= "blue">            </font>set{ _totalHeight = Convert.ToSingle(value); }  
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>int Width {
<font color= "blue">            </font>get{ return Convert.ToInt32(_totalWidth); }
<font color= "blue">            </font>set{ _totalWidth = Convert.ToSingle(value); }    
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>bool ShowLegend {
<font color= "blue">            </font>get{ return _displayLegend; }
<font color= "blue">            </font>set{ _displayLegend = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>bool ShowData {
<font color= "blue">            </font>get{ return _displayBarData; }
<font color= "blue">            </font>set{ _displayBarData = value; }
<font color= "blue">        </font>}
<font color= "blue">        public </font>int TopBuffer {
<font color= "blue">            set</font> { _topBuffer = Convert.ToSingle(value); }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>string VerticalLabel {
<font color= "blue">            </font>get{ return _yLabel; }
<font color= "blue">            </font>set{ _yLabel = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>int VerticalTickCount {
<font color= "blue">            </font>get{ return _yTickCount; }
<font color= "blue">            </font>set{ _yTickCount = value; }
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>BarGraph()
<font color= "blue">        </font>{
<font color= "blue">            </font>AssignDefaultSettings();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        public </font>BarGraph(Color bgColor)
<font color= "blue">        </font>{
<font color= "blue">            </font>AssignDefaultSettings();
<font color= "blue">            </font>BackgroundColor = bgColor;
<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 bar graph.  It is the method called before invoking the Draw() method.</font>
<font color= "green">        // labels is the x values.</font>
<font color= "green">        // values is the y values.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        public void</font> CollectDataPoints(string[] labels, string[] values)
<font color= "blue">        </font>{
<font color= "blue"></font><font color= "blue">            if </font>(labels.Length == values.Length) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>for(int i=0; i<labels.Length; i++)
<font color= "blue">                </font>{
<font color= "blue">                    </font>float temp = Convert.ToSingle(values[i]);
<font color= "blue">                    </font>string shortLbl = MakeShortLabel(labels[i]);
<font color= "blue"></font>
<font color= "green">                    // For now put 0.0 for start position and sweep size</font>
<font color= "blue">                    </font>DataPoints.Add(new ChartItem(shortLbl, labels[i], temp, 0.0f, 0.0f, GetColor(i)));
<font color= "blue">                </font>
<font color= "green">                    // Find max value from data; this is only temporary _maxValue</font>
<font color= "blue"></font><font color= "blue">                    if </font>(_maxValue < temp) _maxValue = temp;<font color= "blue"></font>
<font color= "blue"></font>
<font color= "green">                    // Find the longest description</font>
<font color= "blue"></font><font color= "blue">                    if </font>(_displayLegend) <font color= "blue"></font>
<font color= "blue">                    </font>{
<font color= "blue">                        </font>string currentLbl = labels[i] + " (" + shortLbl + ")";
<font color= "blue">                        </font>float currentWidth = CalculateImgFontWidth(currentLbl, _legendFontSize, FontFamily);
<font color= "blue"></font><font color= "blue">                        if</font>(_maxLabelWidth < currentWidth)<font color= "blue"></font>
<font color= "blue">                        </font>{
<font color= "blue">                            </font>_longestLabel = currentLbl;
<font color= "blue">                            </font>_maxLabelWidth = currentWidth;
<font color= "blue">                        </font>}
<font color= "blue">                    </font>}
<font color= "blue">                </font>}
<font color= "blue"></font>
<font color= "blue">                </font>CalculateTickAndMax();
<font color= "blue">                </font>CalculateGraphDimension();
<font color= "blue">                </font>CalculateBarWidth(DataPoints.Count, _graphWidth);
<font color= "blue">                </font>CalculateSweepValues();
<font color= "blue">            </font>}
<font color= "blue"></font><font color= "blue">            else</font><font color= "blue"></font>
<font color= "blue">                </font>throw new Exception("X data count is different from Y data count");
<font color= "blue">        </font>}
<font color= "blue">        </font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // Same as above; called when user doesn't care about the x values</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        public void</font> CollectDataPoints(string[] values)
<font color= "blue">        </font>{
<font color= "blue">            </font>string[] labels = values;
<font color= "blue">            </font>CollectDataPoints(labels, values);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method returns a bar graph bitmap to the calling function.  It is called after </font>
<font color= "green">        // all dimensions and data points are calculated.</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 height = Convert.ToInt32(_totalHeight);
<font color= "blue">            </font>int width = Convert.ToInt32(_totalWidth);
<font color= "blue"></font>
<font color= "blue">            </font>Bitmap bmp = new Bitmap(width, height);
<font color= "blue">            </font>Graphics graph = Graphics.FromImage(bmp);
<font color= "blue">            </font>graph.CompositingQuality = CompositingQuality.HighQuality;
<font color= "blue">            </font>graph.SmoothingMode = SmoothingMode.AntiAlias;
<font color= "blue"></font>
<font color= "green">            // Set the background: need to draw one pixel larger than the bitmap to cover all area</font>
<font color= "blue">            </font>graph.FillRectangle(new SolidBrush(_backColor), -1, -1, bmp.Width+1, bmp.Height+1);
<font color= "blue"></font>
<font color= "blue">            </font>DrawVerticalLabelArea(graph);
<font color= "blue">            </font>DrawBars(graph);
<font color= "blue">            </font>DrawXLabelArea(graph);
<font color= "blue"></font><font color= "blue">            if </font>(_displayLegend) DrawLegend(graph);<font color= "blue"></font>
<font color= "blue"></font>
<font color= "blue">            </font>graph.Dispose();
<font color= "blue"></font>
<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 draws all the bars for the graph.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> DrawBars(Graphics graph)
<font color= "blue">        </font>{
<font color= "blue">            </font>SolidBrush brsFont = new SolidBrush(_fontColor);
<font color= "blue">            </font>Font valFont = new Font(_fontFamily, _labelFontSize);
<font color= "blue">            </font>StringFormat sfFormat = new StringFormat();
<font color= "blue">            </font>sfFormat.Alignment = StringAlignment.Center;
<font color= "blue">            </font>int i = 0;
<font color= "blue"></font>
<font color= "green">            // Draw bars and the value above each bar</font>
<font color= "blue">            </font>foreach(ChartItem item in DataPoints)
<font color= "blue">            </font>{
<font color= "blue">                </font>SolidBrush barBrush = new SolidBrush(item.ItemColor);
<font color= "blue">                </font>float itemY = _yOrigin + _graphHeight - item.SweepSize;
<font color= "blue"></font>
<font color= "green">                // When drawing, all position is relative to (_xOrigin, _yOrigin)</font>
<font color= "blue">                </font>graph.FillRectangle(barBrush, _xOrigin + item.StartPos, itemY, _barWidth, item.SweepSize);
<font color= "blue"></font>
<font color= "green">                // Draw data value</font>
<font color= "blue"></font><font color= "blue">                if </font>(_displayBarData)<font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    </font>float startX = _xOrigin + (i * (_barWidth + _spaceBtwBars));  // This draws the value on center of the bar
<font color= "blue">                    </font>float startY = itemY - 2f - valFont.Height;                      // Positioned on top of each bar by 2 pixels
<font color= "blue">                    </font>RectangleF recVal = new RectangleF(startX, startY, _barWidth + _spaceBtwBars, valFont.Height);
<font color= "blue">                    </font>graph.DrawString(item.Value.ToString("#,###.##"), valFont, brsFont, recVal, sfFormat);    
<font color= "blue">                </font>}
<font color= "blue">                </font>i++;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method draws the y label, tick marks, tick values, and the y axis.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> DrawVerticalLabelArea(Graphics graph)
<font color= "blue">        </font>{
<font color= "blue">            </font>Font lblFont = new Font(_fontFamily, _labelFontSize);
<font color= "blue">            </font>SolidBrush brs = new SolidBrush(_fontColor);
<font color= "blue">            </font>StringFormat lblFormat = new StringFormat();
<font color= "blue">            </font>lblFormat.Alignment = StringAlignment.Near;
<font color= "blue">            </font>Pen pen = new Pen(_fontColor);
<font color= "blue"></font>
<font color= "green">            // Draw vertical label at the top of y-axis and place it in the middle top of y-axis</font>
<font color= "blue">            </font>RectangleF recVLabel = new RectangleF(0f, _yOrigin-2*_spacer-lblFont.Height, _xOrigin*2, lblFont.Height);
<font color= "blue">            </font>StringFormat sfVLabel = new StringFormat();
<font color= "blue">            </font>sfVLabel.Alignment = StringAlignment.Center;
<font color= "blue">            </font>graph.DrawString(_yLabel, lblFont, brs, recVLabel, sfVLabel);
<font color= "blue"></font>
<font color= "green">            // Draw all tick values and tick marks</font>
<font color= "blue">            for</font> (int i=0; i<_yTickCount; i++)
<font color= "blue">            </font>{
<font color= "blue">                </font>float currentY = _topBuffer + (i * _yTickValue/_scaleFactor);    // Position for tick mark
<font color= "blue">                </font>float labelY = currentY-lblFont.Height/2;                        // Place label in the middle of tick
<font color= "blue">                </font>RectangleF lblRec = new RectangleF(_spacer, labelY, _maxTickValueWidth, lblFont.Height);
<font color= "blue">                </font>float currentTick = _maxValue - i*_yTickValue;                    // Calculate tick value from top to bottom
<font color= "blue"></font>
<font color= "blue">                </font>graph.DrawString(currentTick.ToString("#,###.##"), lblFont, brs, lblRec, lblFormat);    // Draw tick value  
<font color= "blue">                </font>graph.DrawLine(pen, _xOrigin, currentY, _xOrigin - 4.0f, currentY);                        // Draw tick mark
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // Draw y axis</font>
<font color= "blue">            </font>graph.DrawLine(pen, _xOrigin, _yOrigin, _xOrigin, _yOrigin + _graphHeight);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method draws x axis and all x labels</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> DrawXLabelArea(Graphics graph)
<font color= "blue">        </font>{
<font color= "blue">            </font>Font lblFont = new Font(_fontFamily, _labelFontSize);
<font color= "blue">            </font>SolidBrush brs = new SolidBrush(_fontColor);
<font color= "blue">            </font>StringFormat lblFormat = new StringFormat();
<font color= "blue">            </font>lblFormat.Alignment = StringAlignment.Center;
<font color= "blue">            </font>Pen pen = new Pen(_fontColor);
<font color= "blue"></font>
<font color= "green">            // Draw x axis</font>
<font color= "blue">            </font>graph.DrawLine(pen, _xOrigin, _yOrigin + _graphHeight, _xOrigin + _graphWidth, _yOrigin + _graphHeight);
<font color= "blue"></font>
<font color= "blue">            </font>float currentX;
<font color= "blue">            </font>float currentY = _yOrigin + _graphHeight + 2.0f;    // All x labels are drawn 2 pixels below x-axis
<font color= "blue">            </font>float labelWidth = _barWidth + _spaceBtwBars;        // Fits exactly below the bar
<font color= "blue">            </font>int i = 0;
<font color= "blue"></font>
<font color= "green">            // Draw x labels</font>
<font color= "blue">            </font>foreach(ChartItem item in DataPoints)
<font color= "blue">            </font>{
<font color= "blue">                </font>currentX = _xOrigin + (i * labelWidth);
<font color= "blue">                </font>RectangleF recLbl = new RectangleF(currentX, currentY, labelWidth, lblFont.Height);
<font color= "blue">                </font>string lblString = _displayLegend ? item.Label : item.Description;    // Decide what to show: short or long
<font color= "blue"></font>
<font color= "blue">                </font>graph.DrawString(lblString, lblFont, brs, recLbl, lblFormat);
<font color= "blue">                </font>i++;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method determines where to place the legend box.</font>
<font color= "green">        // It draws the legend border, legend description, and legend color code.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> DrawLegend(Graphics graph)
<font color= "blue">        </font>{
<font color= "blue">            </font>Font lblFont = new Font(_fontFamily, _legendFontSize);
<font color= "blue">            </font>SolidBrush brs = new SolidBrush(_fontColor);
<font color= "blue">            </font>StringFormat lblFormat = new StringFormat();
<font color= "blue">            </font>lblFormat.Alignment = StringAlignment.Near;
<font color= "blue">            </font>Pen pen = new Pen(_fontColor);
<font color= "blue"></font>
<font color= "green">            // Calculate Legend drawing start point</font>
<font color= "blue">            </font>float startX = _xOrigin + _graphWidth + _graphLegendSpacer;
<font color= "blue">            </font>float startY = _yOrigin;
<font color= "blue"></font>
<font color= "blue">            </font>float xColorCode = startX + _spacer;
<font color= "blue">            </font>float xLegendText = xColorCode + _legendRectangleSize + _spacer;
<font color= "blue">            </font>float legendHeight = 0.0f;
<font color= "blue">            </font>for(int i=0; i<DataPoints.Count; i++)
<font color= "blue">            </font>{
<font color= "blue">                </font>ChartItem point = DataPoints[i];
<font color= "blue">                </font>string text = point.Description + " (" + point.Label + ")";
<font color= "blue">                </font>float currentY = startY + _spacer + (i * (lblFont.Height + _spacer));
<font color= "blue">                </font>legendHeight += lblFont.Height + _spacer;
<font color= "blue"></font>
<font color= "green">                // Draw legend description</font>
<font color= "blue">                </font>graph.DrawString(text, lblFont, brs, xLegendText, currentY, lblFormat);
<font color= "blue"></font>
<font color= "green">                // Draw color code</font>
<font color= "blue">                </font>graph.FillRectangle(new SolidBrush(DataPoints[i].ItemColor), xColorCode, currentY + 3f, _legendRectangleSize, _legendRectangleSize);
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // Draw legend border</font>
<font color= "blue">            </font>graph.DrawRectangle(pen, startX, startY, _legendWidth, legendHeight + _spacer);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method calculates all measurement aspects of the bar graph from the given data points</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CalculateGraphDimension() 
<font color= "blue">        </font>{
<font color= "blue">            </font>FindLongestTickValue();
<font color= "blue">            </font>
<font color= "green">            // Need to add another character for spacing; this is not used for drawing, just for calculation</font>
<font color= "blue">            </font>_longestTickValue += "0";        
<font color= "blue">            </font>_maxTickValueWidth = CalculateImgFontWidth(_longestTickValue, _labelFontSize, FontFamily);
<font color= "blue">            </font>float leftOffset = _spacer + _maxTickValueWidth;
<font color= "blue">            </font>float rtOffset = 0.0f;
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue">            if </font>(_displayLegend) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>_legendWidth = _spacer + _legendRectangleSize + _spacer + _maxLabelWidth + _spacer;
<font color= "blue">                </font>rtOffset = _graphLegendSpacer + _legendWidth + _spacer;
<font color= "blue">            </font>}
<font color= "blue"></font><font color= "blue">            else</font><font color= "blue"></font>
<font color= "blue">                </font>rtOffset = _spacer;        // Make graph in the middle
<font color= "blue"></font>
<font color= "blue">            </font>_graphHeight = _totalHeight - _topBuffer - _bottomBuffer;    // Buffer spaces are used to print labels
<font color= "blue">            </font>_graphWidth = _totalWidth - leftOffset - rtOffset;
<font color= "blue">            </font>_xOrigin = leftOffset;
<font color= "blue">            </font>_yOrigin = _topBuffer;
<font color= "blue"></font>
<font color= "green">            // Once the correct _maxValue is determined, then calculate _scaleFactor</font>
<font color= "blue">            </font>_scaleFactor = _maxValue / _graphHeight;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method determines the longest tick value from the given data points.</font>
<font color= "green">        // The result is needed to calculate the correct graph dimension.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> FindLongestTickValue()
<font color= "blue">        </font>{
<font color= "blue">            </font>float currentTick;
<font color= "blue">            </font>string tickString;
<font color= "blue">            for</font> (int i=0; i<_yTickCount; i++)
<font color= "blue">            </font>{
<font color= "blue">                </font>currentTick = _maxValue - i*_yTickValue;    
<font color= "blue">                </font>tickString = currentTick.ToString("#,###.##");
<font color= "blue"></font><font color= "blue">                if </font>(_longestTickValue.Length < tickString.Length)<font color= "blue"></font>
<font color= "blue">                    </font>_longestTickValue = tickString;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method calculates the image width in pixel for a given text</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private </font>float CalculateImgFontWidth(string text, int size, string family)
<font color= "blue">        </font>{
<font color= "blue">            </font>Bitmap bmp = null;
<font color= "blue">            </font>Graphics graph =null;
<font color= "blue">            </font>Font font = new Font(family, size);
<font color= "blue"></font>
<font color= "green">            // Calculate the size of the string.</font>
<font color= "blue">            </font>bmp = new Bitmap(1,1,PixelFormat.Format32bppArgb);
<font color= "blue">            </font>graph = Graphics.FromImage(bmp);
<font color= "blue">            </font>SizeF oSize = graph.MeasureString(text, font);
<font color= "blue">            </font>
<font color= "blue">            </font>graph.Dispose();
<font color= "blue">            </font>bmp.Dispose();
<font color= "blue"></font>
<font color= "blue">            return</font> oSize.Width;
<font color= "blue">        </font>}
<font color= "blue">        </font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method creates abbreviation from long description; used for making legend</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private </font>string MakeShortLabel(string text)
<font color= "blue">        </font>{
<font color= "blue">            </font>string label = text;
<font color= "blue"></font><font color= "blue">            if </font>(text.Length > 2) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>int midPostition = Convert.ToInt32(Math.Floor(text.Length/2));
<font color= "blue">                </font>label = text.Substring(0,1) + text.Substring(midPostition, 1) + text.Substring(text.Length-1,1);
<font color= "blue">            </font>}
<font color= "blue">            return</font> label;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method calculates the max value and each tick mark value for the bar graph.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CalculateTickAndMax()
<font color= "blue">        </font>{
<font color= "blue">            </font>float tempMax = 0.0f;
<font color= "blue"></font>
<font color= "green">            // Give graph some head room first about 10% of current max</font>
<font color= "blue">            </font>_maxValue *= 1.1f;
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue">            if </font>(_maxValue != 0.0f)<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "green">                // Find a rounded value nearest to the current max value</font>
<font color= "green">                // Calculate this max first to give enough space to draw value on each bar</font>
<font color= "blue">                </font>double exp = Convert.ToDouble(Math.Floor(Math.Log10(_maxValue)));
<font color= "blue">                </font>tempMax = Convert.ToSingle(Math.Ceiling(_maxValue / Math.Pow(10, exp)) * Math.Pow(10, exp));
<font color= "blue">            </font>}
<font color= "blue"></font><font color= "blue">            else</font><font color= "blue"></font>
<font color= "blue">                </font>tempMax = 1.0f;
<font color= "blue"></font>
<font color= "green">            // Once max value is calculated, tick value can be determined; tick value should be a whole number</font>
<font color= "blue">            </font>_yTickValue = tempMax / _yTickCount;
<font color= "blue">            </font>double expTick = Convert.ToDouble(Math.Floor(Math.Log10(_yTickValue)));
<font color= "blue">            </font>_yTickValue = Convert.ToSingle(Math.Ceiling(_yTickValue / Math.Pow(10, expTick)) * Math.Pow(10, expTick));
<font color= "blue"></font>
<font color= "green">            // Re-calculate the max value with the new tick value</font>
<font color= "blue">            </font>_maxValue = _yTickValue * _yTickCount;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method calculates the height for each bar in the graph</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CalculateSweepValues()
<font color= "blue">        </font>{
<font color= "green">            // Called when all values and scale factor are known</font>
<font color= "green">            // All values calculated here are relative from (_xOrigin, _yOrigin)</font>
<font color= "blue">            </font>int i = 0;
<font color= "blue">            </font>foreach(ChartItem item in DataPoints)
<font color= "blue">            </font>{
<font color= "green">                // This implementation does not support negative value</font>
<font color= "blue"></font><font color= "blue">                if </font>(item.Value >= 0) item.SweepSize = item.Value/_scaleFactor;<font color= "blue"></font>
<font color= "blue">                </font>
<font color= "green">                // (_spaceBtwBars/2) makes half white space for the first bar</font>
<font color= "blue">                </font>item.StartPos = (_spaceBtwBars/2) + i * (_barWidth+_spaceBtwBars);
<font color= "blue">                </font>i++;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method calculates the width for each bar in the graph</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CalculateBarWidth(int dataCount, float barGraphWidth)
<font color= "blue">        </font>{
<font color= "green">            // White space between each bar is the same as bar width itself</font>
<font color= "blue">            </font>_barWidth = barGraphWidth / (dataCount * 2);  // Each bar has 1 white space 
<font color= "blue">            </font>_spaceBtwBars = _barWidth;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // This method assigns default value to the bar graph properties and is only </font>
<font color= "green">        // called from BarGraph constructors</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> AssignDefaultSettings()
<font color= "blue">        </font>{
<font color= "green">            // default values</font>
<font color= "blue">            </font>_totalWidth = 700f;
<font color= "blue">            </font>_totalHeight = 450f;
<font color= "blue">            </font>_fontFamily = "Verdana";
<font color= "blue">            </font>_backColor = Color.White;
<font color= "blue">            </font>_fontColor = Color.Black;
<font color= "blue">            </font>_topBuffer = 30f;
<font color= "blue">            </font>_bottomBuffer = 30f;
<font color= "blue">            </font>_yTickCount = 2;
<font color= "blue">            </font>_displayLegend = false;
<font color= "blue">            </font>_displayBarData = false;
<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