public RenderData CreateRenderData(Graph graph, Graphics g, int width, int height) { RenderData data = new RenderData { Graph = graph, g = g, Width = width, Height = height, AxisPen = new Pen(Color.Black), AxisFont = new Font(FontFamily.GenericSansSerif, 10.0f), AxisBrush = new SolidBrush(Color.Black), AxisStringFormat = "#,##0.##", LegendFont = new Font(FontFamily.GenericSansSerif, 10.0f), LegendBrush = new SolidBrush(Color.Black), LargeTickLength = 10, SeriesPens = graph.Series.Select(s => new Pen(s.Color)).ToArray() }; data.LegendFontMetrics = g.MeasureString("Title of a Series", data.LegendFont); int legendWidth = (int)(data.LegendFontMetrics.Width + 20); data.LegendRect = new Rectangle(width - legendWidth, 0, legendWidth, height); SizeF axisFontMetrics = g.MeasureString(graph.ActualYMaximum.ToString(data.AxisStringFormat), data.AxisFont); data.AxisYRect = new Rectangle(0, 0, (int)(axisFontMetrics.Width + data.LargeTickLength), (int)(height - axisFontMetrics.Height - data.LargeTickLength)); data.AxisXRect = new Rectangle(data.AxisYRect.Right, data.AxisYRect.Bottom, width - data.AxisYRect.Left - data.LegendRect.Width, (int)(axisFontMetrics.Height + data.LargeTickLength)); data.GraphRect = new Rectangle(data.AxisYRect.Right, data.AxisYRect.Top, width - data.AxisYRect.Width - data.LegendRect.Width, height - data.AxisYRect.Top - data.AxisXRect.Height); data.XMin = data.Graph.ActualXMinimum; data.XMax = data.Graph.ActualXMaximum; data.XInterval = data.Graph.ActualXInterval; if (Math.Abs(data.XMax - data.XMin) < 0.0001) { data.XPixelInterval = width / 10.0; data.XPixelsPerValue = 0.0; } else { data.XPixelInterval = Math.Max(1.0, data.GraphRect.Width / ((data.XMax - data.XMin) / data.XInterval)); data.XPixelsPerValue = data.GraphRect.Width / (data.XMax - data.XMin); } data.YMin = data.Graph.ActualYMinimum; data.YMax = data.Graph.ActualYMaximum; data.YInterval = data.Graph.ActualYInterval; if (Math.Abs(data.YMax - data.YMin) < 0.0001) { data.YPixelInterval = height / 10.0; data.YPixelsPerValue = 0.0; } else { data.YPixelInterval = Math.Max(1.0, data.GraphRect.Height / ((data.YMax - data.YMin) / data.YInterval)); data.YPixelsPerValue = data.GraphRect.Height / (data.YMax - data.YMin); } return data; }
public void Render(Graph graph, Graphics g, int width, int height) { g.Clear(Color.White); using (RenderData data = CreateRenderData(graph, g, width, height)) { RenderYAxis(data); RenderXAxis(data); RenderGraphData(data); RenderLegend(data); _lastData = data; } }
public GraphControl() { InitializeComponent(); Graph = new Graph(); RecreateBitmap(); }