/// <summary> /// This is the basic constructor that makes a image at defualt settings. /// It calls several Private methods for the actual drawing. /// </summary> /// <param name="h"></param> /// <param name="w"></param> /// <param name="l"></param> /// <returns></returns> public static Bitmap draw(Histogram h, int w, int l, DisplayOptions opt) { //Create a bitmap and make it editable Bitmap bitmap = new Bitmap(w,l); Graphics g = Graphics.FromImage(bitmap); setTheme(Gui.theme); double ticknum; pushover = 0; pushoverY = 0; //set the margin for the left side checkDecimals(h, opt); //set the bottom margin. if (opt.hor_Label_Decimals > 1 && h.numBars > 14) pushoverY=8; //draw the background drawBackgrounds(g, w, l,opt); //Draw the Horizontal Axis drawXAxis(g, l, w, h, opt); //Draw the bars drawBars(g, l, w, h, opt); //Draw the Vertical Axis //drawYAxis( g,l,Convert.ToInt32(getMax(h.frequency)),h); if (opt.number_of_ticksB == true) { drawYAxis(g, l, opt.number_of_ticks, h, opt); } if (opt.number_of_ticksB == false) { drawYAxis(g, l, 6, h, opt); } //Return the image return bitmap; }
private static void checkDecimals(Histogram h, DisplayOptions opt) { float tickNumber = 6; float labelMin = 0; int x = 0; string[] words; float yCurrent = (float)getMax(h.frequency); if (opt.label_startB == true) { labelMin = (float)opt.label_start; } if (opt.label_stopB == true) { labelMin = (float)opt.label_stop; } if (opt.number_of_ticksB == true) { tickNumber = (float)opt.number_of_ticks; } float yIncrementer = (yCurrent - (float)labelMin) / tickNumber; for (int i = 0; i < tickNumber; i++) { words = yCurrent.ToString().Split('.'); if (words.Length == 2) { if (words[1].Length > x) x = words[1].Length; } yCurrent -= yIncrementer; } if (x < opt.vert_Label_Decimals) { pushover = x * 5; } else pushover = opt.vert_Label_Decimals * 5; }
private static void drawYAxisLabels(Graphics g, int l, int ticknum, Histogram h, DisplayOptions opt) { double labelMin = 0; if (opt.label_startB == true) { labelMin = Convert.ToDouble(opt.label_start); } int u = l - 34-10; int spacing = u / ticknum; int margin = 0; float yLabel = getMax(h.frequency); float yIncrementer=(yLabel-(float)labelMin)/ticknum; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //draw the labels, starting from higest to lowest for (int i = 0; i < ticknum; i++) { double y=0; string label=""; string[] words; string dec; if(opt.vert_Label_DecimalsB==false) { y = Math.Round(yLabel); label = y.ToString(); } if (opt.vert_Label_DecimalsB == true) { label = yLabel.ToString(); words = label.Split('.'); if (opt.vert_Label_Decimals == 0) label = words[0]; else { if (words.Length == 2) { dec = ""; for (int j = 0; j < opt.vert_Label_Decimals; j++) { if (j < words[1].Length) { dec = dec + words[1][j].ToString(); margin += 4; } } label = words[0] + "." + dec; dec = ""; } } } g.DrawString(label, new Font("Arial", 7), Brushes.Black, new Point(0+10-margin+pushover, 17 + (i * spacing))); yLabel-=yIncrementer; margin = 0; } //Draw Zero g.DrawString(Convert.ToString(labelMin), new Font("Arial", 7), Brushes.Black, new Point(0+10-margin+pushover, 8 + (u)-pushoverY)); }
/// <summary> /// This method draws X axis, based on nmumber of bars /// </summary> /// <param name="g"></param> /// <param name="l"></param> /// <param name="w"></param> /// <param name="h"></param> private static void drawXAxis(Graphics g,int l, int w, Histogram h, DisplayOptions opt) { int xl = w - 42-10-5 - pushover; int spacing = xl / (h.X_Axis.Count-1); string label = ""; string dec = ""; int margin=0; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; for (int i = 0; i < h.X_Axis.Count; i++) { label = Convert.ToString(h.X_Axis[i]); string[] words = label.Split('.'); if (opt.hor_Label_DecimalsB == true) { if (opt.hor_Label_Decimals == 0) label = words[0]; else { if (words.Length == 2) { dec = ""; for (int j = 0; j < opt.hor_Label_Decimals; j++) { if(j<words[1].Length) dec = dec + words[1][j].ToString(); } label = words[0] + "." + dec; dec = ""; } } } if (opt.hor_Label_DecimalsB == false) { dec = ""; if (words.Length == 2) { for (int j = 0; j < h.decimalPlaces; j++) { dec = "."; if (j < words[1].Length) dec = dec + words[1][j].ToString(); } label = words[0] + dec; dec = ""; } } //Draw the ticks g.FillRectangle(blackBrush, 22+10 + 5 + pushover + (i * spacing), l - 20-10-pushoverY, 2, 6); //Draw the label int hgt = 0; if (opt.hor_Label_Decimals > 1 && i % 2 == 0 && h.numBars>14) hgt = 8; margin = label.Length*3; g.DrawString(label, new Font("Arial", 7), Brushes.Black, new Point(22 +10-margin+5 +pushover+ (i * spacing), l - 15-10+hgt-pushoverY)); label = ""; } }
/// <summary> /// This method draws the Y axis, with a given number of ticks /// </summary> /// <param name="g"></param> /// <param name="l"></param> /// <param name="ticknum"></param> private static void drawYAxis(Graphics g,int l,int ticknum, Histogram h, DisplayOptions opt) { int u = l - 34-10; int spacing = u / ticknum; int margin = 0; for (int i = 0; i < ticknum; i++) { g.FillRectangle(blackBrush, 12+12+margin+pushover,(22+(i*spacing)) , 4,2 ); } drawYAxisLabels(g, l, ticknum, h, opt); }
/// <summary> /// This method takes a histogram and sets the image box to /// the histogram image, drawn using the image class. Whether or not user /// parameters have been set determines which method from the image class /// it calls. /// </summary> /// <param name="a"></param> private void DrawHistoGram(Histogram a) { HistoGram_pictureBox.BackgroundImage = Image.draw(a, HistoGram_pictureBox.Width, HistoGram_pictureBox.Height, opt); }
/// <summary> /// This method contains the loop that draws the bars and their outlines. /// </summary> /// <param name="g"></param> /// <param name="l"></param> /// <param name="w"></param> /// <param name="h"></param> private static void drawBars(Graphics g, int l, int w, Histogram h, DisplayOptions opt) { int xl = w - 42-10-5-pushover; int spacing = xl / (h.X_Axis.Count-1); //Create new bar hieghts based on Label Start and stop if (opt.label_startB == true) { for (int i = 0; i < h.frequency.Count; i++) { if (Convert.ToDouble(h.frequency[i]) <= opt.label_start) h.frequency[i] = 0; } } if (opt.label_stopB == true) { for (int i = 0; i < h.frequency.Count; i++) { if (Convert.ToDouble(h.frequency[i]) >= opt.label_stop) h.frequency[i] = opt.label_stop; } } //Set the ceiling and floor for drawing the historgram float floor = l - 21 - 10; float max_freq = getMax(h.frequency); float max_ceiling = 22; //A loop to draw the bars for (int i = 0; i < h.numBars ; i++) { int hght = Convert.ToInt32(h.frequency[i]); //Get the hieght of the bar. float freq_percent = 1 - (float)hght / max_freq;//Cacluate the percent the frequency repesents. float pixel_percent = (floor - max_ceiling) * freq_percent; //Calcualte how many pixels that is. float ceiling = max_ceiling + pixel_percent;//Find the hieght to drawthetop of the bar at. //Draw the black rectangle for the outline. g.FillRectangle(blackBrush, 22+10+5+pushover + (i * spacing), Convert.ToInt32(ceiling), spacing, Convert.ToInt32(floor) - Convert.ToInt32(ceiling) + 1-pushoverY); //Draw a smaller blue rectangle g.FillRectangle(blueBrush, (24+10+5+pushover + (i * spacing)), Convert.ToInt32(ceiling) + 2, spacing - 5, (Convert.ToInt32(floor) - Convert.ToInt32(ceiling) + 1) - 4-pushoverY); } }
private void Number_of_Bins_Scroll(object sender, ScrollEventArgs e) { histogamOptions_isChanged = true; ho.num_binsB = true; ho.num_bins = Number_of_Bins.Value; barlabel.Text = ho.num_bins.ToString(); if (d != null) { h = new Histogram(d, ho); DrawHistoGram(h); } else if(d==null) { MessageBox.Show("You must Press Input Data first", "Ok", MessageBoxButtons.OKCancel); } }
private void Number_of_Ticks_Scroll(object sender, ScrollEventArgs e) { displayOptions_isChanged = true; opt.number_of_ticksB = true; // opt.vert_Label_DecimalsB = false; opt.number_of_ticks = Number_of_Ticks.Value; ticklabe.Text = Number_of_Ticks.Value.ToString(); if (d != null) { h = new Histogram(d, ho); DrawHistoGram(h); } else if (d == null) { MessageBox.Show("You must Press Input Data first", "Ok", MessageBoxButtons.OKCancel); } }
/// <summary> /// This method Creates a histogram with a data object as a parameter /// It then displays some information so the user can check to make sure /// everything is kosher. Whether or not user parameters have been set /// determines which method from the Histogram class /// it calls. /// </summary> /// <param name="d"></param> private void Historgram(DataReader d) { h = new Histogram(d, ho); report = "Historgram Report"; report = report + "\r\nFrequencyValues"; foreach (int a in h.frequency) { report = report + a.ToString() + " "; } report = report + "\r\nXAxis"; foreach (float a in h.X_Axis) { report = report + a.ToString() + " "; } report = report + "\r\n Width " + (h.width).ToString(); report = report + "\r\n Max " + (h.max).ToString(); report = report + "\r\n Min " + (h.min).ToString(); report = report + "\r\n Number of Bars " + (h.numBars).ToString(); report = report + "\r\n Number of decimals " + (h.decimalPlaces).ToString(); }
/// <summary> /// This method takes a histogram and sets the image box to /// the histogram image, drawn using the image class. Whether or not user /// parameters have been set determines which method from the image class /// it calls. /// </summary> /// <param name="a"></param> private void DrawHistoGram(Histogram a) { HistoGram_pictureBox.BackgroundImage = Image.draw(a, HistoGram_pictureBox.Width, HistoGram_pictureBox.Height, opt); }
/// <summary> /// Initializes a new instance of the HistogramImage class. /// Constructor accepts two integers, a width and a height. /// Initializes pixel values, fonts, colors, and etc. needed to structure a histogram bitmap image. /// </summary> /// <param name="width"></param> /// <param name="height"></param> public HistogramImage(int width, int height) { this.pixWidth = width; this.pixHeight = height; histogram = new Histogram(); histogramBitmap = new Bitmap(width, height); histogramGraphic = Graphics.FromImage(histogramBitmap); maxLabelPixSize = histogramGraphic.MeasureString("100", fontRegular); bgBorderPixWidth = 1; bgPixTM = 20; bgPixBM = 5; bgPixLM = 5; bgPixRM = 20; frameBorderPixWidth = 1; framePixTM = 5; framePixLM = 5; framePixRM = 5; vertTickPixWidth = 6; vertTickPixHeight = 1; horizTickPixWidth = 1; horizTickPixHeight = 6; fullBGPixLM = Convert.ToInt32(maxLabelPixSize.Width) + bgPixLM + vertTickPixWidth + 2; fullBGPixBM = Convert.ToInt32(maxLabelPixSize.Height) + bgPixBM + horizTickPixHeight + 2; framePixWidth = pixWidth - fullBGPixLM - bgPixRM; framePixHeight = pixHeight - fullBGPixBM - bgPixTM; firstBarXAxis = fullBGPixLM + framePixLM; barsMaxPixWidth = framePixWidth - framePixLM - framePixRM; barsMaxPixHeight = height - bgPixTM - framePixTM - fullBGPixBM; barsMaxHeightYAxis = bgPixTM + framePixTM; }
private static void drawYAxisLabels(Graphics g, int l, int ticknum, Histogram h, DisplayOptions opt) { double labelMin = 0; if (opt.label_startB == true) { labelMin = Convert.ToDouble(opt.label_start); } int u = l - 34 - 10; int spacing = u / ticknum; int margin = 0; float yLabel = getMax(h.frequency); float yIncrementer = (yLabel - (float)labelMin) / ticknum; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //draw the labels, starting from higest to lowest for (int i = 0; i < ticknum; i++) { double y = 0; string label = ""; string[] words; string dec; if (opt.vert_Label_DecimalsB == false) { y = Math.Round(yLabel); label = y.ToString(); } if (opt.vert_Label_DecimalsB == true) { label = yLabel.ToString(); words = label.Split('.'); if (opt.vert_Label_Decimals == 0) { label = words[0]; } else { if (words.Length == 2) { dec = ""; for (int j = 0; j < opt.vert_Label_Decimals; j++) { if (j < words[1].Length) { dec = dec + words[1][j].ToString(); margin += 4; } } label = words[0] + "." + dec; dec = ""; } } } g.DrawString(label, new Font("Arial", 7), Brushes.Black, new Point(0 + 10 - margin + pushover, 17 + (i * spacing))); yLabel -= yIncrementer; margin = 0; } //Draw Zero g.DrawString(Convert.ToString(labelMin), new Font("Arial", 7), Brushes.Black, new Point(0 + 10 - margin + pushover, 8 + (u) - pushoverY)); }
/// <summary> /// This method draws X axis, based on nmumber of bars /// </summary> /// <param name="g"></param> /// <param name="l"></param> /// <param name="w"></param> /// <param name="h"></param> private static void drawXAxis(Graphics g, int l, int w, Histogram h, DisplayOptions opt) { int xl = w - 42 - 10 - 5 - pushover; int spacing = xl / (h.X_Axis.Count - 1); string label = ""; string dec = ""; int margin = 0; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; for (int i = 0; i < h.X_Axis.Count; i++) { label = Convert.ToString(h.X_Axis[i]); string[] words = label.Split('.'); if (opt.hor_Label_DecimalsB == true) { if (opt.hor_Label_Decimals == 0) { label = words[0]; } else { if (words.Length == 2) { dec = ""; for (int j = 0; j < opt.hor_Label_Decimals; j++) { if (j < words[1].Length) { dec = dec + words[1][j].ToString(); } } label = words[0] + "." + dec; dec = ""; } } } if (opt.hor_Label_DecimalsB == false) { dec = ""; if (words.Length == 2) { for (int j = 0; j < h.decimalPlaces; j++) { dec = "."; if (j < words[1].Length) { dec = dec + words[1][j].ToString(); } } label = words[0] + dec; dec = ""; } } //Draw the ticks g.FillRectangle(blackBrush, 22 + 10 + 5 + pushover + (i * spacing), l - 20 - 10 - pushoverY, 2, 6); //Draw the label int hgt = 0; if (opt.hor_Label_Decimals > 1 && i % 2 == 0 && h.numBars > 14) { hgt = 8; } margin = label.Length * 3; g.DrawString(label, new Font("Arial", 7), Brushes.Black, new Point(22 + 10 - margin + 5 + pushover + (i * spacing), l - 15 - 10 + hgt - pushoverY)); label = ""; } }