Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Bitmap createGraphFromValues(List<TempRecord> records, int minuteStep)
- {
- int padding = 30;
- int chartPadding_T = 15;
- int chartPadding_B = 20;
- Bitmap bitmap = new Bitmap(1000,500);
- float minTemp = 0;
- float maxTemp = 0;
- getTempBounds(records, out minTemp, out maxTemp);
- float delta = (float) Math.Floor((double)(Math.Abs(minTemp) + Math.Abs(maxTemp)));
- using (Graphics g = Graphics.FromImage(bitmap)) {
- using (SolidBrush b = new SolidBrush(Color.White)) {
- using (Pen p = new Pen(Color.Black)) {
- b.Color = Color.White;
- g.FillRectangle(b, new RectangleF(new PointF(0, 0), bitmap.Size));
- Rect drawingArea = new Rect(padding, padding, bitmap.Size.Width - 2 * padding, bitmap.Size.Height - 2 * padding - chartPadding_B);
- drawingArea.Width = (int) (Math.Ceiling((double)(drawingArea.Width / (float)records.Count)) * (double)records.Count);
- int realHeight_ForChart = drawingArea.Height - chartPadding_T - chartPadding_B;
- int tempWidth = drawingArea.Width / records.Count;
- List<Rect> tempDrawingAreas = new List<Rect>();
- List<Rect> tempAreas = new List<Rect>();
- for (int i = 0; i < records.Count; i++) {
- Rect r = new Rect((i*tempWidth) + padding , padding + chartPadding_T, tempWidth, realHeight_ForChart);
- Rect r2 = new Rect((i * tempWidth) + padding, padding, tempWidth, realHeight_ForChart + chartPadding_T + chartPadding_B);
- tempAreas.Add(r);
- tempDrawingAreas.Add(r2);
- }
- int idx = 0;
- List<Point> tempPoints = new List<Point>();
- foreach (TempRecord tr in records) {
- float absTemp = tr.Temp + Math.Abs(minTemp);
- float percentHeight = 50.0f;
- if (delta != 0.0f) {
- percentHeight = ((float)100.0 / (float)delta) * absTemp;
- }
- Rect tempArea = tempAreas[idx];
- float tempHei = ((float)tempArea.Height / (float)100.0) * percentHeight;
- Point tempPoint = new Point();
- tempPoint.X = tempArea.getEdgeCenterCoords(Edge.eTop).X;
- tempPoint.Y = tempArea.getEdgeCenterCoords(Edge.eBottom).Y - (int)tempHei;
- if (tempPoint.Y > tempArea.Bottom) {
- tempPoint.Y = tempArea.Bottom;
- }
- if (tempPoint.Y < tempArea.Top)
- {
- tempPoint.Y = tempArea.Top;
- }
- tempPoints.Add(tempPoint);
- idx++;
- }
- int arrLen = tempAreas.Count + 2 + 2 + 1;
- Point[] points = new Point[arrLen];
- byte[] types = new byte[arrLen];
- idx = 0;
- points[idx] = drawingArea.getCornerCoords(Corner.cBottomLeft);
- types[idx] = (byte) PathPointType.Start;
- idx++;
- points[idx] = tempAreas[0].getCornerCoords(Corner.cTopLeft);
- points[idx].Y = tempPoints[0].Y;
- types[idx] = (byte)PathPointType.Line;
- idx++;
- for (int i = 0; i < tempPoints.Count; i++) {
- points[idx] = tempPoints[i];
- types[idx] = (byte)PathPointType.Bezier;
- idx++;
- }
- points[idx] = tempAreas[tempAreas.Count-1].getCornerCoords(Corner.cTopRight);
- points[idx].Y = tempPoints[tempAreas.Count - 1].Y;
- types[idx] = (byte)PathPointType.Bezier;
- idx++;
- points[idx] = drawingArea.getCornerCoords(Corner.cBottomRight);
- types[idx] = (byte)PathPointType.Line;
- idx++;
- points[idx] = points[0];
- types[idx] = (byte)PathPointType.Line;
- idx++;
- for (int i = 0; i < points.Length; i++) {
- points[i].X -= padding;
- points[i].Y -= padding;
- }
- GraphicsPath path = new GraphicsPath(points, types);
- Bitmap bezRegioBitmap = new Bitmap(drawingArea.Width, drawingArea.Height);
- using (Graphics g2 = Graphics.FromImage(bezRegioBitmap)) {
- g2.FillClosedCurve(Brushes.Black, points);
- idx = 0;
- foreach (Rect re in tempDrawingAreas) {
- Bitmap input_plus = null;
- Bitmap input_minus = null;
- Bitmap masked_plus = null;
- Bitmap masked_minus = null;
- Bitmap mask = null;
- mask = new Bitmap(re.Width, bezRegioBitmap.Height);
- using (Graphics g3 = Graphics.FromImage(mask))
- {
- g3.DrawImage(bezRegioBitmap, 0, 0, new Rectangle(idx * mask.Width, 0, mask.Width, mask.Height), GraphicsUnit.Pixel);
- }
- input_plus = new Bitmap(mask.Width, mask.Height);
- input_minus = new Bitmap(mask.Width, mask.Height);
- 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));
- 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));
- Graphics.FromImage(input_plus).FillRectangle(lb1, new Rectangle(0,0,mask.Width,mask.Height));
- Graphics.FromImage(input_minus).FillRectangle(lb2, new Rectangle(0, 0, mask.Width, mask.Height));
- masked_plus = ImageMask.getMaskedImage(input_plus, mask);
- masked_minus = ImageMask.getMaskedImage(input_minus, mask);
- lb1.Dispose();
- lb2.Dispose();
- input_plus.Dispose();
- input_minus.Dispose();
- TempRecord tr = records[idx];
- Bitmap masked = null;
- if (tr.Temp > 0)
- {
- masked = masked_plus;
- }
- else {
- masked = masked_minus;
- }
- g.DrawImage(masked, new Point(padding + idx * mask.Width, padding));
- idx++;
- }
- }
- path.Dispose();
- for (int i = 0; i < points.Length; i++)
- {
- points[i].X += padding;
- points[i].Y += padding;
- }
- foreach (Rect r2 in tempDrawingAreas)
- {
- p.Color = Color.FromArgb(10, 50, 50, 50);
- g.DrawLine(p, new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Top), new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom + 5));
- p.Color = Color.Black;
- g.DrawLine(p, new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom), new Point(r2.getEdgeCenterCoords(Edge.eTop).X, r2.Bottom + 5));
- }
- p.Color = Color.FromArgb(10, 50, 50, 50);
- int vertCNT = (int)((double)tempDrawingAreas.Count * (double)1.5);
- int vertSpace = (int) Math.Ceiling((float)drawingArea.Height / (float)vertCNT);
- for (int i = 1; i < vertCNT; i++) {
- int y = drawingArea.Bottom - i*vertSpace;
- g.DrawLine(p, new Point(drawingArea.Left, y), new Point(drawingArea.Right, y));
- }
- StringFormat format = new StringFormat();
- format.LineAlignment = StringAlignment.Center;
- format.Alignment = StringAlignment.Center;
- Font drawFontBold = new Font("Arial", 9, FontStyle.Bold);
- Font drawFontItalic = new Font("Arial", 9, FontStyle.Italic);
- idx = 0;
- foreach (TempRecord tr in records) {
- Rect r = tempDrawingAreas[idx];
- g.DrawString(tr.Temp.ToString(), drawFontBold, new SolidBrush(Color.Black), new RectangleF(r.Left, r.Bottom + 2, r.Width, 25), format);
- idx++;
- }
- g.DrawString("[°C]", drawFontItalic, new SolidBrush(Color.Black), drawingArea.Right + 15, drawingArea.Bottom + 12, format);
- format.LineAlignment = StringAlignment.Near;
- format.Alignment = StringAlignment.Near;
- 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);
- format.LineAlignment = StringAlignment.Near;
- format.Alignment = StringAlignment.Far;
- 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);
- format.LineAlignment = StringAlignment.Near;
- format.Alignment = StringAlignment.Near;
- g.DrawString(String.Format("Krok: {0} minut", minuteStep), drawFontBold, new SolidBrush(Color.Black), new RectangleF(drawingArea.Left, drawingArea.Top - 20, 200, 30), format);
- p.Color = Color.Black;
- g.DrawLine(p, drawingArea.getCornerCoords(Corner.cTopLeft), drawingArea.getCornerCoords(Corner.cBottomLeft));
- g.DrawLine(p, drawingArea.getCornerCoords(Corner.cBottomLeft), drawingArea.getCornerCoords(Corner.cBottomRight));
- }
- }
- }
- System.GC.Collect();
- return bitmap;
- }
- private void getTempBounds(List<TempRecord> records, out float min, out float max) {
- min = records[0].Temp;
- max = min;
- foreach (TempRecord tr in records) {
- if (tr.Temp > max) {
- max = tr.Temp;
- }
- if (tr.Temp < min)
- {
- min = tr.Temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement