public Translator(Rectangle plotRectangle, RangeD xRange, RangeD yRange) { PlotRectangle = plotRectangle; XRange = xRange; YRange = yRange; KX = plotRectangle.Width / (xRange.End - xRange.Begin); KY = plotRectangle.Height / (yRange.End - yRange.Begin); }
public static void Plot( Graphics g, Rectangle plotRectangle, float fontSize, Color lineColor, Color textColor, RangeD xRange, RangeD yRange, Tuple <double, string>[] xTics, Tuple <double, string>[] yTics, bool enableDots, Tuple <Color, PointD[]>[] data, PointD?marker) { var font = new Font("Consolas", fontSize, GraphicsUnit.Pixel); var linePen = new Pen(lineColor); var lineBrush = new SolidBrush(lineColor); var textBrush = new SolidBrush(textColor); g.DrawRectangles(linePen, new[] { plotRectangle }); var ticLength = 3; var t = new Translator(plotRectangle, xRange, yRange); foreach (var tic in xTics) { var x = t.Translate(new PointD(tic.Item1, 0.0)).X; g.DrawString(tic.Item2, font, textBrush, new PointF(x, plotRectangle.Bottom + XLabelOffset), StringFormatTop); g.DrawLine(linePen, x, plotRectangle.Top, x, plotRectangle.Top + ticLength); g.DrawLine(linePen, x, plotRectangle.Bottom - ticLength, x, plotRectangle.Bottom); } foreach (var tic in yTics) { var y = t.Translate(new PointD(0, tic.Item1)).Y; g.DrawString(tic.Item2, font, textBrush, new PointF(plotRectangle.Left - YLabelOffset, y), StringFormatRight); g.DrawLine(linePen, plotRectangle.Left, y, plotRectangle.Left + ticLength, y); g.DrawLine(linePen, plotRectangle.Right - ticLength, y, plotRectangle.Right, y); } for (int i = 0; i < data.Length; i++) { var curve = Array.ConvertAll(data[i].Item2, t.Translate); Color color = data[i].Item1; using (var pen = new Pen(color)) using (var brush = new SolidBrush(color)) { for (int j = 0; j < curve.Length; j++) { if (j != 0 && Point_IsValid(curve[j - 1]) && Point_IsValid(curve[j])) { g.DrawLine(pen, curve[j - 1], curve[j]); } if (enableDots && Point_IsValid(curve[j])) { g.FillRectangle(brush, new Rectangle(curve[j].X - DotSize / 2, curve[j].Y - DotSize / 2, DotSize, DotSize)); } } } } if (marker.HasValue) { var p = t.Translate(marker.Value); if (Point_IsValid(p)) { g.DrawRectangles(Pens.White, new[] { new RectangleF(p.X - MarkerSize / 2, p.Y - MarkerSize / 2, MarkerSize - 1, MarkerSize - 1) }); } } font.Dispose(); linePen.Dispose(); lineBrush.Dispose(); textBrush.Dispose(); }