Advertisement
MarMar_IV

TempChart

Nov 5th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.08 KB | None | 0 0
  1.         private Bitmap createGraphFromValues(List<TempRecord> records, int minuteStep)
  2.         {
  3.             int padding = 30;
  4.             int chartPadding_T = 15;
  5.             int chartPadding_B = 20;
  6.             Bitmap bitmap = new Bitmap(1000,500);
  7.  
  8.             float minTemp = 0;
  9.             float maxTemp = 0;            
  10.             getTempBounds(records, out minTemp, out maxTemp);
  11.             float delta = (float) Math.Floor((double)(Math.Abs(minTemp) + Math.Abs(maxTemp)));
  12.  
  13.             using (Graphics g = Graphics.FromImage(bitmap)) {
  14.                 using (SolidBrush b = new SolidBrush(Color.White)) {
  15.                     using (Pen p = new Pen(Color.Black)) {
  16.                         b.Color = Color.White;
  17.                         g.FillRectangle(b, new RectangleF(new PointF(0, 0), bitmap.Size));
  18.  
  19.                         Rect drawingArea = new Rect(padding, padding, bitmap.Size.Width - 2 * padding, bitmap.Size.Height - 2 * padding - chartPadding_B);
  20.                         drawingArea.Width = (int) (Math.Ceiling((double)(drawingArea.Width / (float)records.Count)) * (double)records.Count);
  21.  
  22.  
  23.  
  24.                         int realHeight_ForChart = drawingArea.Height - chartPadding_T - chartPadding_B;
  25.                         int tempWidth = drawingArea.Width / records.Count;
  26.  
  27.                         List<Rect> tempDrawingAreas = new List<Rect>();
  28.                         List<Rect> tempAreas = new List<Rect>();
  29.                         for (int i = 0; i < records.Count; i++) {
  30.                             Rect r = new Rect((i*tempWidth) + padding , padding + chartPadding_T, tempWidth, realHeight_ForChart);
  31.                             Rect r2 = new Rect((i * tempWidth) + padding, padding, tempWidth, realHeight_ForChart + chartPadding_T + chartPadding_B);
  32.  
  33.                             tempAreas.Add(r);
  34.                             tempDrawingAreas.Add(r2);
  35.                         }
  36.  
  37.  
  38.                         int idx = 0;
  39.                         List<Point> tempPoints = new List<Point>();
  40.                         foreach (TempRecord tr in records) {
  41.                             float absTemp = tr.Temp + Math.Abs(minTemp);
  42.  
  43.                             float percentHeight = 50.0f;
  44.                             if (delta != 0.0f) {
  45.                                 percentHeight = ((float)100.0 / (float)delta) * absTemp;
  46.                             }
  47.  
  48.                             Rect tempArea = tempAreas[idx];
  49.                             float tempHei = ((float)tempArea.Height / (float)100.0) * percentHeight;                            
  50.  
  51.                             Point tempPoint = new Point();
  52.                             tempPoint.X = tempArea.getEdgeCenterCoords(Edge.eTop).X;
  53.                             tempPoint.Y = tempArea.getEdgeCenterCoords(Edge.eBottom).Y - (int)tempHei;
  54.  
  55.                             if (tempPoint.Y > tempArea.Bottom) {
  56.                                 tempPoint.Y = tempArea.Bottom;
  57.                             }
  58.                             if (tempPoint.Y < tempArea.Top)
  59.                             {
  60.                                 tempPoint.Y = tempArea.Top;
  61.                             }
  62.  
  63.                             tempPoints.Add(tempPoint);
  64.                             idx++;
  65.                         }
  66.  
  67.                         int arrLen = tempAreas.Count + 2 + 2 + 1;
  68.                         Point[] points = new Point[arrLen];
  69.                         byte[] types = new byte[arrLen];
  70.  
  71.                         idx = 0;
  72.                         points[idx] = drawingArea.getCornerCoords(Corner.cBottomLeft);                        
  73.                         types[idx] = (byte) PathPointType.Start;
  74.                         idx++;
  75.  
  76.                         points[idx] = tempAreas[0].getCornerCoords(Corner.cTopLeft);
  77.                         points[idx].Y = tempPoints[0].Y;
  78.                         types[idx] = (byte)PathPointType.Line;
  79.                         idx++;
  80.                         for (int i = 0; i < tempPoints.Count; i++) {
  81.                             points[idx] = tempPoints[i];
  82.                             types[idx] = (byte)PathPointType.Bezier;
  83.                             idx++;
  84.                         }
  85.                         points[idx] = tempAreas[tempAreas.Count-1].getCornerCoords(Corner.cTopRight);
  86.                         points[idx].Y = tempPoints[tempAreas.Count - 1].Y;
  87.                         types[idx] = (byte)PathPointType.Bezier;
  88.                         idx++;
  89.  
  90.                         points[idx] = drawingArea.getCornerCoords(Corner.cBottomRight);
  91.                         types[idx] = (byte)PathPointType.Line;
  92.                         idx++;
  93.  
  94.                         points[idx] = points[0];
  95.                         types[idx] = (byte)PathPointType.Line;
  96.                         idx++;
  97.  
  98.                         for (int i = 0; i < points.Length; i++) {
  99.                             points[i].X -= padding;
  100.                             points[i].Y -= padding;
  101.                         }
  102.  
  103.  
  104.  
  105.  
  106.                         GraphicsPath path = new GraphicsPath(points, types);
  107.                         Bitmap bezRegioBitmap = new Bitmap(drawingArea.Width, drawingArea.Height);
  108.                         using (Graphics g2 = Graphics.FromImage(bezRegioBitmap)) {
  109.                             g2.FillClosedCurve(Brushes.Black, points);
  110.  
  111.                             idx = 0;
  112.                             foreach (Rect re in tempDrawingAreas) {
  113.                                 Bitmap input_plus = null;
  114.                                 Bitmap input_minus = null;
  115.                                 Bitmap masked_plus = null;
  116.                                 Bitmap masked_minus = null;
  117.                                 Bitmap mask = null;
  118.  
  119.  
  120.  
  121.                                 mask = new Bitmap(re.Width, bezRegioBitmap.Height);
  122.                                 using (Graphics g3 = Graphics.FromImage(mask))
  123.                                 {
  124.                                     g3.DrawImage(bezRegioBitmap, 0, 0, new Rectangle(idx * mask.Width, 0, mask.Width, mask.Height), GraphicsUnit.Pixel);
  125.                                 }
  126.  
  127.                                 input_plus = new Bitmap(mask.Width, mask.Height);
  128.                                 input_minus = new Bitmap(mask.Width, mask.Height);
  129.  
  130.                                 LinearGradientBrush lb1 = new LinearGradientBrush(new Point(0, 0), new Point(0, mask.Height), Color.FromArgb(255, 255, 175, 75), Color.FromArgb(255, 255, 146, 10));
  131.                                 LinearGradientBrush lb2 = new LinearGradientBrush(new Point(0, 0), new Point(0, mask.Height), Color.FromArgb(255, 135, 224, 253), Color.FromArgb(255, 5, 171, 224));
  132.  
  133.                                 Graphics.FromImage(input_plus).FillRectangle(lb1, new Rectangle(0,0,mask.Width,mask.Height));
  134.                                 Graphics.FromImage(input_minus).FillRectangle(lb2, new Rectangle(0, 0, mask.Width, mask.Height));
  135.  
  136.                                 masked_plus = ImageMask.getMaskedImage(input_plus, mask);
  137.                                 masked_minus = ImageMask.getMaskedImage(input_minus, mask);
  138.  
  139.                                 lb1.Dispose();
  140.                                 lb2.Dispose();
  141.                                 input_plus.Dispose();
  142.                                 input_minus.Dispose();
  143.  
  144.  
  145.  
  146.                                 TempRecord tr = records[idx];
  147.                                 Bitmap masked = null;
  148.                                 if (tr.Temp > 0)
  149.                                 {
  150.                                     masked = masked_plus;
  151.                                 }
  152.                                 else {
  153.                                     masked = masked_minus;
  154.                                 }
  155.  
  156.                                 g.DrawImage(masked, new Point(padding + idx * mask.Width, padding));
  157.  
  158.                                 idx++;
  159.                             }
  160.                         }
  161.                         path.Dispose();
  162.  
  163.                         for (int i = 0; i < points.Length; i++)
  164.                         {
  165.                             points[i].X += padding;
  166.                             points[i].Y += padding;
  167.                         }
  168.                        
  169.  
  170.  
  171.  
  172.  
  173.                         foreach (Rect r2 in tempDrawingAreas)
  174.                         {
  175.                             p.Color = Color.FromArgb(10, 50, 50, 50);
  176.                             g.DrawLine(p, new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Top), new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom + 5));
  177.  
  178.                             p.Color = Color.Black;
  179.                             g.DrawLine(p, new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom), new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom + 5));
  180.                         }
  181.  
  182.                         p.Color = Color.FromArgb(10, 50, 50, 50);
  183.                         int vertCNT = (int)((double)tempDrawingAreas.Count * (double)1.5);
  184.                         int vertSpace = (int) Math.Ceiling((float)drawingArea.Height / (float)vertCNT);
  185.                         for (int i = 1; i < vertCNT; i++) {
  186.                             int y = drawingArea.Bottom - i*vertSpace;
  187.                             g.DrawLine(p, new Point(drawingArea.Left, y), new Point(drawingArea.Right, y));
  188.                         }
  189.  
  190.                         StringFormat format = new StringFormat();
  191.                         format.LineAlignment = StringAlignment.Center;
  192.                         format.Alignment = StringAlignment.Center;
  193.                         Font drawFontBold = new Font("Arial", 9, FontStyle.Bold);
  194.                         Font drawFontItalic = new Font("Arial", 9, FontStyle.Italic);
  195.  
  196.                         idx = 0;
  197.                         foreach (TempRecord tr in records) {
  198.                             Rect r = tempDrawingAreas[idx];
  199.  
  200.                             g.DrawString(tr.Temp.ToString(), drawFontBold, new SolidBrush(Color.Black), new RectangleF(r.Left, r.Bottom + 2, r.Width, 25), format);
  201.  
  202.                             idx++;
  203.                         }
  204.                         g.DrawString("[°C]", drawFontItalic, new SolidBrush(Color.Black), drawingArea.Right + 15, drawingArea.Bottom + 12, format);
  205.  
  206.                         format.LineAlignment = StringAlignment.Near;
  207.                         format.Alignment = StringAlignment.Near;
  208.                         g.DrawString(String.Format("{0:MM/dd/yy H:mm:ss}", records[0].Date), drawFontBold, new SolidBrush(Color.Black), new RectangleF(drawingArea.Left, drawingArea.Bottom + 30, 200, 30), format);
  209.  
  210.                         format.LineAlignment = StringAlignment.Near;
  211.                         format.Alignment = StringAlignment.Far;
  212.                         g.DrawString(String.Format("{0:MM/dd/yy H:mm:ss}", records[records.Count-1].Date), drawFontBold, new SolidBrush(Color.Black), new RectangleF(drawingArea.Right - 200, drawingArea.Bottom + 30, 200, 30), format);
  213.  
  214.  
  215.                         format.LineAlignment = StringAlignment.Near;
  216.                         format.Alignment = StringAlignment.Near;
  217.                         g.DrawString(String.Format("Krok: {0} minut", minuteStep), drawFontBold, new SolidBrush(Color.Black), new RectangleF(drawingArea.Left, drawingArea.Top - 20, 200, 30), format);
  218.  
  219.  
  220.                         p.Color = Color.Black;
  221.                         g.DrawLine(p, drawingArea.getCornerCoords(Corner.cTopLeft), drawingArea.getCornerCoords(Corner.cBottomLeft));
  222.                         g.DrawLine(p, drawingArea.getCornerCoords(Corner.cBottomLeft), drawingArea.getCornerCoords(Corner.cBottomRight));
  223.                     }
  224.                 }
  225.             }
  226.  
  227.             System.GC.Collect();
  228.  
  229.             return bitmap;
  230.         }
  231.  
  232.         private void getTempBounds(List<TempRecord> records, out float min, out float max) {
  233.             min = records[0].Temp;
  234.             max = min;
  235.  
  236.             foreach (TempRecord tr in records) {
  237.                 if (tr.Temp > max) {
  238.                     max = tr.Temp;
  239.                 }
  240.                 if (tr.Temp < min)
  241.                 {
  242.                     min = tr.Temp;
  243.                 }
  244.             }
  245.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement