/// <summary>Creates the graph.</summary> /// <param name="writer">The writer.</param> /// <param name="graphAndTable">The graph and table to convert to html.</param> private void CreateGraphPDF(Section section, AutoDocumentation.GraphAndTable graphAndTable) { // Create a 2 column, 1 row table. Image in first cell, X/Y data in second cell. var table = section.AddTable(); table.Style = "GraphAndTable"; table.Rows.LeftIndent = graphAndTable.indent + "cm"; var column1 = table.AddColumn(); column1.Width = "8cm"; //column1.Format.Alignment = ParagraphAlignment.Right; var column2 = table.AddColumn(); column2.Width = "8cm"; //column2.Format.Alignment = ParagraphAlignment.Right; var row = table.AddRow(); // Ensure graphs directory exists. string graphDirectory = Path.Combine(WorkingDirectory, "Graphs"); Directory.CreateDirectory(graphDirectory); // Determine the name of the .png file to write. string pngFileName = Path.Combine(graphDirectory, graphAndTable.xyPairs.Parent.Parent.Name + graphAndTable.xyPairs.Parent.Name + ".png"); int count = 0; // If there are multiple graphs with the same name, they may overwrite each other. // Therefore, we attempt to generate a unique name. After 20 attempts, we give up. while (File.Exists(pngFileName) && count < 20) { count++; pngFileName = Path.Combine(graphDirectory, graphAndTable.xyPairs.Parent.Parent.Name + graphAndTable.xyPairs.Parent.Name + Guid.NewGuid() + ".png"); } // Setup graph. GraphView graph = new GraphView(); graph.Clear(); graph.Width = 400; graph.Height = 250; // Create a line series. graph.DrawLineAndMarkers("", graphAndTable.xyPairs.X, graphAndTable.xyPairs.Y, null, null, null, Models.Axis.AxisType.Bottom, Models.Axis.AxisType.Left, System.Drawing.Color.Blue, Models.LineType.Solid, Models.MarkerType.None, Models.LineThicknessType.Normal, Models.MarkerSizeType.Normal, 1, true); graph.ForegroundColour = OxyPlot.OxyColors.Black; graph.BackColor = OxyPlot.OxyColors.White; // Format the axes. graph.FormatAxis(Models.Axis.AxisType.Bottom, graphAndTable.xName, false, double.NaN, double.NaN, double.NaN, false); graph.FormatAxis(Models.Axis.AxisType.Left, graphAndTable.yName, false, double.NaN, double.NaN, double.NaN, false); graph.FontSize = 10; graph.Refresh(); // Export graph to bitmap file. Bitmap image = new Bitmap(graph.Width, graph.Height); using (Graphics gfx = Graphics.FromImage(image)) using (SolidBrush brush = new SolidBrush(System.Drawing.Color.White)) { gfx.FillRectangle(brush, 0, 0, image.Width, image.Height); } graph.Export(ref image, new Rectangle(0, 0, image.Width, image.Height), false); graph.MainWidget.Destroy(); image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png); MigraDoc.DocumentObjectModel.Shapes.Image sectionImage = row.Cells[0].AddImage(pngFileName); sectionImage.LockAspectRatio = true; sectionImage.Width = "8cm"; // Add x/y data. Paragraph xyParagraph = row.Cells[1].AddParagraph(); xyParagraph.Style = "xyStyle"; AddFixedWidthText(xyParagraph, "X", 10); AddFixedWidthText(xyParagraph, "Y", 10); xyParagraph.AddLineBreak(); for (int i = 0; i < graphAndTable.xyPairs.X.Length; i++) { AddFixedWidthText(xyParagraph, graphAndTable.xyPairs.X[i].ToString(), 10); AddFixedWidthText(xyParagraph, graphAndTable.xyPairs.Y[i].ToString(), 10); xyParagraph.AddLineBreak(); } // Add an empty paragraph for spacing. section.AddParagraph(); }
/// <summary>Creates the graph.</summary> /// <param name="writer">The writer.</param> /// <param name="graphAndTable">The graph and table to convert to html.</param> /// <param name="workingDirectory">The working directory.</param> private void CreateGraphPDF(Section section, AutoDocumentation.GraphAndTable graphAndTable, string workingDirectory) { // Create a 2 column, 1 row table. Image in first cell, X/Y data in second cell. Table table = section.AddTable(); table.Style = "GraphAndTable"; table.Rows.LeftIndent = graphAndTable.indent + "cm"; Column column1 = table.AddColumn(); column1.Width = "8cm"; //column1.Format.Alignment = ParagraphAlignment.Right; Column column2 = table.AddColumn(); column2.Width = "8cm"; //column2.Format.Alignment = ParagraphAlignment.Right; Row row = table.AddRow(); // Ensure graphs directory exists. string graphDirectory = Path.Combine(workingDirectory, "Graphs"); Directory.CreateDirectory(graphDirectory); // Determine the name of the .png file to write. string PNGFileName = Path.Combine(graphDirectory, graphAndTable.xyPairs.Parent.Parent.Name + graphAndTable.xyPairs.Parent.Name + ".png"); // Setup graph. GraphView graph = new GraphView(null); graph.Clear(); // Create a line series. graph.DrawLineAndMarkers("", graphAndTable.xyPairs.X, graphAndTable.xyPairs.Y, Models.Graph.Axis.AxisType.Bottom, Models.Graph.Axis.AxisType.Left, System.Drawing.Color.Blue, Models.Graph.LineType.Solid, Models.Graph.MarkerType.None, Models.Graph.LineThicknessType.Normal, Models.Graph.MarkerSizeType.Normal, true); // Format the axes. graph.FormatAxis(Models.Graph.Axis.AxisType.Bottom, graphAndTable.xName, false, double.NaN, double.NaN, double.NaN); graph.FormatAxis(Models.Graph.Axis.AxisType.Left, graphAndTable.yName, false, double.NaN, double.NaN, double.NaN); graph.BackColor = OxyPlot.OxyColors.White; graph.FontSize = 10; graph.Refresh(); // Export graph to bitmap file. Bitmap image = new Bitmap(400, 250); graph.Export(ref image, false); image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png); MigraDoc.DocumentObjectModel.Shapes.Image image1 = row.Cells[0].AddImage(PNGFileName); // Add x/y data. Paragraph xyParagraph = row.Cells[1].AddParagraph(); xyParagraph.Style = "xyStyle"; AddFixedWidthText(xyParagraph, "X", 10); AddFixedWidthText(xyParagraph, "Y", 10); xyParagraph.AddLineBreak(); for (int i = 0; i < graphAndTable.xyPairs.X.Length; i++) { AddFixedWidthText(xyParagraph, graphAndTable.xyPairs.X[i].ToString(), 10); AddFixedWidthText(xyParagraph, graphAndTable.xyPairs.Y[i].ToString(), 10); xyParagraph.AddLineBreak(); } // Add an empty paragraph for spacing. Paragraph spacing = section.AddParagraph(); }