/// <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(); }
/// <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(); }
private static void CreateGraph(StreamWriter OutputFile, XmlNode N, int NextLevel, Model parentModel) { string InstanceName = XmlUtilities.Value(N.OwnerDocument.DocumentElement, "Name"); string GraphName; if (N.Name == "XYPairs") { GraphName = XmlUtilities.Value(N.ParentNode.ParentNode, "Name") + XmlUtilities.Value(N.ParentNode, "Name") + "Graph"; } else { GraphName = XmlUtilities.Value(N.ParentNode.ParentNode, "Name") + XmlUtilities.Value(N, "Name") + "Graph"; } OutputFile.WriteLine(Header(XmlUtilities.Value(N.ParentNode, "Name"), NextLevel, XmlUtilities.Value(N.ParentNode.ParentNode, "Name"))); WriteDescriptionForTypeName(OutputFile, N.ParentNode, parentModel); TryDocumentMemo(OutputFile, N.ParentNode, NextLevel); Directory.CreateDirectory(InstanceName + "Graphs"); string GifFileName = InstanceName + "Graphs\\" + GraphName + ".gif"; // work out x and y variable names. string XName = XmlUtilities.Value(N.ParentNode, "XProperty"); if (N.ParentNode.Name == "TemperatureFunction" || N.ParentNode.Name == "AirTemperatureFunction") { XName = "Temperature (oC)"; } string YName; if (N.Name == "XYPairs") { YName = XmlUtilities.Value(N.ParentNode, "Name"); } else { YName = XmlUtilities.Value(N, "Name"); } if (YName == "Function") { YName = XmlUtilities.Value(N.ParentNode.ParentNode, "Name"); } // Set up to write a table. OutputFile.WriteLine("<table border=\"0\">"); // OutputFile.WriteLine("<td></td><td></td>"); // OutputFile.WriteLine("<tr>"); // output xy table as a nested table. OutputFile.WriteLine("<td>"); OutputFile.WriteLine("<table width=\"250\">"); OutputFile.WriteLine("<td><b>" + XName + "</b></td><td><b>" + YName + "</b></td>"); double[] x = MathUtilities.StringsToDoubles(XmlUtilities.Values(N, "X/double")); double[] y = MathUtilities.StringsToDoubles(XmlUtilities.Values(N, "Y/double")); for (int i = 0; i < x.Length; i++) { OutputFile.WriteLine("<tr><td>" + x[i] + "</td><td>" + y[i] + "</td></tr>"); } OutputFile.WriteLine("</table>"); OutputFile.WriteLine("</td>"); // output chart as a column to the outer table. OutputFile.WriteLine("<td>"); OutputFile.WriteLine("<img src=\"" + GifFileName + "\">"); OutputFile.WriteLine("</td>"); OutputFile.WriteLine("</tr>"); OutputFile.WriteLine("</table>"); // Setup cleanish graph. GraphView graph = new GraphView(); graph.Clear(); // Create a line series. graph.DrawLineAndMarkers("", x, y, Models.Graph.Axis.AxisType.Bottom, Models.Graph.Axis.AxisType.Left, Color.Blue, Models.Graph.Series.LineType.Solid, Models.Graph.Series.MarkerType.FilledCircle, true); // Format the axes. graph.FormatAxis(Models.Graph.Axis.AxisType.Bottom, XName, false, double.NaN, double.NaN, double.NaN); graph.FormatAxis(Models.Graph.Axis.AxisType.Left, YName, false, double.NaN, double.NaN, double.NaN); // Format the title graph.BackColor = Color.White; graph.Refresh(); // Export graph to bitmap file. Bitmap image = new Bitmap(400, 400); graph.Export(image); image.Save(GifFileName, System.Drawing.Imaging.ImageFormat.Gif); }