internal void Draw(FivePointNine.Windows.Graphics.Graphics2 g, int width, int height, float xog, float yog, float xs, float ys, bool showHighlighted) { //minDispCached = float.PositiveInfinity; //maxDispCached = float.NegativeInfinity; if (!Enabled) { return; } try { minDispValueOvershoot = false; maxDispValueOvershoot = false; List <PointF> ps = new List <PointF>(); if (Values.Count > 2) { minDispYCached = Values[0]; maxDispYCached = Values[0]; float sumOfValues = 0; float sumOfTimes = 0; float summedValues = 0; int lastAddedX = (int)Math.Round(TimeStamps[0] * xs) - 1; for (int i = 0; i < Values.Count; i++) { var xToAdd = (int)Math.Round(TimeStamps[i] * xs); summedValues++; sumOfValues += Values[i]; sumOfTimes += TimeStamps[i]; if (xToAdd > lastAddedX) { float t = sumOfTimes / summedValues; float v = sumOfValues / summedValues; if (double.IsPositiveInfinity(v)) { continue; } float vG = height - (v * ys + yog); float tG = t * xs + xog; if (tG >= 0 && tG <= width) { if (vG < 0) { maxDispValueOvershoot = true; } if (vG > height) { minDispValueOvershoot = true; } if (v < minDispYCached) { minDispYCached = v; } if (v > maxDispYCached) { maxDispYCached = v; } ps.Add(new PointF(tG, vG)); } summedValues = 0; sumOfTimes = 0; sumOfValues = 0; lastAddedX = xToAdd; } } } Color c = Color.FromArgb((int)(LineOpacity * 255), LineColor); Pen PlotP = new Pen(c, LineThickness); if (ps.Count > 1) { if (showHighlighted) { g.DrawLines(new Pen(GraphicsUtils.GetContrast(PlotP.Color), PlotP.Width + 3), ps.ToArray()); g.DrawLines(new Pen(Color.FromArgb(255, PlotP.Color), LineThickness), ps.ToArray()); } else { g.DrawLines(PlotP, ps.ToArray()); } } if (LastPointHighlight && ps.Count > 0) { var p = ps[ps.Count - 1]; g.DrawRectangle(Pen, (int)(p.X - LineThickness * 2), (int)(p.Y - LineThickness * 2), LineThickness * 4, LineThickness * 4); } } catch (Exception ex) { } }
public static RectangleF drawXYAxis(FivePointNine.Windows.Graphics.Graphics2 g, float w, float h, float xs, float ys, float xog, float yog, bool grid, Color backColor, Font Font) { RectangleF drawingRect = new RectangleF(0, 0, w, h); // X Axis var axisP = new Pen(Color.DarkGray, 1.5F); var majLine = new Pen(Color.FromArgb(155, 155, 155), 1F); var minLine = new Pen(Color.FromArgb(200, 200, 200), 1F); float unitX = 1e-8F; float multF = 5; // determine scale first while (unitX * xs < Font.Height * 2.2F) { unitX *= multF; multF = multF == 2 ? 5 : 2; } if (unitX < 1e-8 || unitX > 1e8) { return(drawingRect); } float minX = 0, maxX = 0; while (minX * xs < -xog) { minX += unitX; } while (minX * xs > -xog) { minX -= unitX; } while (maxX * xs > w - xog) { maxX -= unitX; } while (maxX * xs < w - xog) { maxX += unitX; } Font f = new Font("ARIAL", Font.Height * 0.5F); int yaWid = f.Height * 4; int xaHei = (f.Height * 15 / 10); drawingRect = new RectangleF(0, 0, w - yaWid, h - xaHei); bool isMinLine = false; var xSigFiguresAfterD = 0; var totalFigs = (unitX / 2 - Math.Floor(unitX / 2)).ToString().Length - 2; while (Math.Round(unitX, xSigFiguresAfterD) == Math.Round(unitX / 2, xSigFiguresAfterD) && xSigFiguresAfterD <= totalFigs) { xSigFiguresAfterD++; } for (float i = minX; i <= maxX; i += unitX / 2) { PointF drawableMid = VtoG(new PointF(i, 0), xog / xs, xs, yog / ys, ys, h); drawableMid = new PointF(drawableMid.X, h); if (!isMinLine) { PointF drawable1 = new PointF(drawableMid.X, drawableMid.Y - 1.5F); PointF drawable2 = new PointF(drawableMid.X, drawableMid.Y + 1.5F); if (grid) { drawable1 = new PointF(drawable1.X, 0); } if (grid) { drawable2 = new PointF(drawable2.X, h - xaHei); } string s = roundedFrac(i, xSigFiguresAfterD); var xyo = g.MeasureString(s, f); PointF drawableStrPos = new PointF(drawableMid.X - xyo.Width / 2, drawableMid.Y - xyo.Height - 2); if (drawable1.X < w - yaWid && drawable1.X > 0) { g.DrawLine(majLine, drawable1, drawable2); g.DrawString(s, f, Color.Gray, drawableStrPos); } } else { //PointF drawable1 = new PointF(drawableMid.X, drawableMid.Y - 1); //PointF drawable2 = new PointF(drawableMid.X, drawableMid.Y + 1); //if (grid) drawable1 = new PointF(drawable1.X, 0); //if (grid) drawable2 = new PointF(drawable2.X, h - 15); //if (drawable1.X < w - 30 && drawable1.X > 0) // g.DrawLine(minLine, drawable1, drawable2); } isMinLine = !isMinLine; } if (xog < w - yaWid && xog > 0) { g.DrawLine(axisP, xog, 0, xog, h - 15); } // Y Axis float unitY = 1e-6F;; multF = 5; // determine scale first while (unitY * ys < Font.Height * 1.5F) { unitY *= multF; multF = multF == 2 ? 5 : 2; } if (unitY < 1e-7 || unitY > 1e7) { return(drawingRect); } float minY = 0, maxY = 0; while (minY * ys < -yog) { minY += unitY; } while (minY * ys > -yog) { minY -= unitY; } while (maxY * ys > h - yog) { maxY -= unitX; } while (maxY * ys < h - yog) { maxY += unitY; } isMinLine = false; var ySigFiguresAfterD = 0; totalFigs = (unitY / 2 - Math.Floor(unitY / 2)).ToString().Length - 2; while (Math.Round(unitY, ySigFiguresAfterD) == Math.Round(unitY / 2, ySigFiguresAfterD) && ySigFiguresAfterD <= totalFigs) { ySigFiguresAfterD++; } for (float i = minY; i <= maxY; i += unitY / 2) { PointF drawableMid = VtoG(new PointF(0, i), xog / xs, xs, yog / ys, ys, h); drawableMid = new PointF(w, drawableMid.Y); if (!isMinLine) { PointF drawable1 = new PointF(drawableMid.X - 1.5F, drawableMid.Y); PointF drawable2 = new PointF(drawableMid.X + 1.5F, drawableMid.Y); if (grid) { drawable1 = new PointF(0, drawable1.Y); } if (grid) { drawable2 = new PointF(w - yaWid, drawable2.Y); } string s = roundedFrac(i, ySigFiguresAfterD); var xyo = g.MeasureString(s, f); PointF drawableStrPos = new PointF(drawableMid.X - xyo.Width, drawableMid.Y - xyo.Height / 2 - 2); if (drawable2.Y < h - xaHei && drawable2.Y > 0) { g.DrawLine(majLine, drawable1, drawable2); g.DrawString(s, f, Color.Gray, drawableStrPos); } } else { //PointF drawable1 = new PointF(drawableMid.X - 1F, drawableMid.Y); //PointF drawable2 = new PointF(drawableMid.X + 1F, drawableMid.Y); //if (grid) drawable1 = new PointF(0, drawable1.Y); //if (grid) drawable2 = new PointF(w - 30, drawable2.Y); //if (drawable2.Y < h - 15 && drawable2.Y > 0) // g.DrawLine(minLine, drawable1, drawable2); } isMinLine = !isMinLine; } g.DrawLine(axisP, 0, h - yog, w - yaWid, h - yog); g.DrawRectangle(axisP, drawingRect); return(drawingRect); }