Added a new "Undefined" control mode. Changed the GPU fan control to restore to default (auto) settings when the Open Hardware Monitor closes (unless the control remained in "Undefined" mode).
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="HtmlReportWriter.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Specifies the html element type to use when writing plots.
29 // --------------------------------------------------------------------------------------------------------------------
30 namespace OxyPlot.Reporting
32 using System.Collections.Generic;
37 /// Specifies the html element type to use when writing plots.
39 public enum HtmlPlotElementType
42 /// Use the embed tag and reference an external svg file.
47 /// Use the object tag and reference an external svg file.
52 /// Use the svg tag and include the plot inline.
58 /// HTML5 report writer.
60 public class HtmlReportWriter : XmlWriterBase, IReportWriter
63 /// The text measurer.
65 private readonly IRenderContext textMeasurer;
68 /// The figure counter.
70 private int figureCounter;
75 private ReportStyle style;
78 /// Initializes a new instance of the <see cref="HtmlReportWriter"/> class.
80 /// <param name="stream">
83 /// <param name="textMeasurer">
84 /// The text measurer.
86 public HtmlReportWriter(Stream stream, IRenderContext textMeasurer = null)
89 this.textMeasurer = textMeasurer;
90 this.WriteHtmlElement();
91 this.PlotElementType = HtmlPlotElementType.Svg;
95 /// Gets or sets the type of the plot element.
98 /// The type of the plot element.
100 public HtmlPlotElementType PlotElementType { get; set; }
103 /// Closes this instance.
105 public override void Close()
107 this.WriteEndElement();
108 this.WriteEndElement();
113 /// Writes the class ID.
115 /// <param name="className">
118 /// <param name="id">
121 public void WriteClassId(string className, string id = null)
123 if (className != null)
125 this.WriteAttributeString("class", className);
130 this.WriteAttributeString("id", id);
135 /// Writes the drawing.
140 public void WriteDrawing(DrawingFigure d)
142 this.WriteStartFigure();
143 this.WriteRaw(d.Content);
144 this.WriteEndFigure(d.FigureText);
148 /// Writes the equation.
150 /// <param name="equation">
153 public void WriteEquation(Equation equation)
159 /// Writes the header.
164 public void WriteHeader(Header h)
171 this.WriteStartElement("h" + h.Level);
172 this.WriteString(h.ToString());
173 this.WriteEndElement();
177 /// Writes the image.
182 public void WriteImage(Image i)
184 // this requires the image to be located in the same folder as the html
185 string localFileName = i.Source;
186 this.WriteStartFigure();
187 this.WriteStartElement("img");
188 this.WriteAttributeString("src", localFileName);
189 this.WriteAttributeString("alt", i.FigureText);
190 this.WriteEndElement();
191 this.WriteEndFigure(i.FigureText);
195 /// Writes the paragraph.
200 public void WriteParagraph(Paragraph p)
202 this.WriteElementString("p", p.Text);
208 /// <param name="plot">
211 public void WritePlot(PlotFigure plot)
213 this.WriteStartFigure();
214 switch (this.PlotElementType)
216 case HtmlPlotElementType.Embed:
217 case HtmlPlotElementType.Object:
218 // TODO: need a Func<string,Stream> to provide streams for the plot files?
220 //string source = string.Format(
221 // "{0}_Plot{1}.svg", Path.GetFileNameWithoutExtension(this.outputFile), plot.FigureNumber);
222 //plot.PlotModel.SaveSvg(this.GetFullFileName(source), plot.Width, plot.Height, this.textMeasurer);
223 //this.WriteStartElement(this.PlotElementType == HtmlPlotElementType.Embed ? "embed" : "object");
224 //this.WriteAttributeString("src", source);
225 //this.WriteAttributeString("type", "image/svg+xml");
226 //this.WriteEndElement();
228 case HtmlPlotElementType.Svg:
229 this.WriteRaw(plot.PlotModel.ToSvg(plot.Width, plot.Height, false, this.textMeasurer));
233 this.WriteEndFigure(plot.FigureText);
237 /// The write report.
239 /// <param name="report">
242 /// <param name="reportStyle">
245 public void WriteReport(Report report, ReportStyle reportStyle)
247 this.style = reportStyle;
248 this.WriteHtmlHeader(report.Title, null, CreateCss(reportStyle));
253 /// Writes the items.
258 public void WriteRows(Table t)
260 IList<TableColumn> columns = t.Columns;
262 foreach (var c in columns)
264 this.WriteStartElement("col");
265 this.WriteAttributeString("align", GetAlignmentString(c.Alignment));
266 if (double.IsNaN(c.Width))
268 this.WriteAttributeString("width", c.Width + "pt");
271 this.WriteEndElement();
274 foreach (var row in t.Rows)
278 this.WriteStartElement("thead");
281 this.WriteStartElement("tr");
283 foreach (var c in row.Cells)
285 bool isHeader = row.IsHeader || t.Columns[j++].IsHeader;
287 this.WriteStartElement("td");
290 this.WriteAttributeString("class", "header");
293 this.WriteString(c.Content);
294 this.WriteEndElement();
297 this.WriteEndElement(); // tr
300 this.WriteEndElement(); // thead
306 /// Writes the table.
311 public void WriteTable(Table t)
313 if (t.Rows == null || t.Columns == null)
318 this.WriteStartElement("table");
320 // WriteAttributeString("border", "1");
321 // WriteAttributeString("width", "60%");
322 if (t.Caption != null)
324 this.WriteStartElement("caption");
325 this.WriteString(t.GetFullCaption(this.style));
326 this.WriteEndElement();
331 this.WriteEndElement(); // table
335 /// Creates the css section.
337 /// <param name="style">
343 private static string CreateCss(ReportStyle style)
345 var css = new StringBuilder();
346 css.AppendLine("body { " + ParagraphStyleToCss(style.BodyTextStyle) + " }");
347 for (int i = 0; i < style.HeaderStyles.Length; i++)
349 css.AppendLine("h" + (i + 1) + " {" + ParagraphStyleToCss(style.HeaderStyles[i]) + " }");
352 css.AppendLine("table caption { " + ParagraphStyleToCss(style.TableCaptionStyle) + " }");
353 css.AppendLine("thead { " + ParagraphStyleToCss(style.TableHeaderStyle) + " }");
354 css.AppendLine("td { " + ParagraphStyleToCss(style.TableTextStyle) + " }");
355 css.AppendLine("td.header { " + ParagraphStyleToCss(style.TableHeaderStyle) + " }");
356 css.AppendLine("figuretext { " + ParagraphStyleToCss(style.FigureTextStyle) + " }");
359 @"body { margin:20pt; }
360 table { border: solid 1px black; margin: 8pt; border-collapse:collapse; }
361 td { padding: 0 2pt 0 2pt; border-left: solid 1px black; border-right: solid 1px black;}
362 thead { border:solid 1px black; }
363 .content, .content td { border: none; }
364 .figure { margin: 8pt;}
365 .table { margin: 8pt;}
366 .table caption { margin: 4pt;}
367 .table thead td { padding: 2pt;}");
368 return css.ToString();
372 /// Gets the alignment string.
375 /// The alignment type.
378 /// An alignment string.
380 private static string GetAlignmentString(Alignment a)
382 return a.ToString().ToLower();
386 /// Converts a paragraphes style to css.
394 private static string ParagraphStyleToCss(ParagraphStyle s)
396 var css = new StringBuilder();
397 if (s.FontFamily != null)
399 css.Append(string.Format("font-family:{0};", s.FontFamily));
402 css.Append(string.Format("font-size:{0}pt;", s.FontSize));
405 css.Append(string.Format("font-weight:bold;"));
408 return css.ToString();
412 /// Initializes this instance.
414 private void WriteHtmlElement()
416 this.WriteStartElement("html", "http://www.w3.org/1999/xhtml");
422 /// <param name="divstyle">
423 /// The style of the div.
425 /// <param name="content">
428 private void WriteDiv(string divstyle, string content)
430 this.WriteStartElement("div");
431 this.WriteAttributeString("class", divstyle);
432 this.WriteString(content);
433 this.WriteEndElement();
437 /// Writes the end figure.
439 /// <param name="text">
442 private void WriteEndFigure(string text)
444 this.WriteDiv("figuretext", string.Format("Fig {0}. {1}", this.figureCounter, text));
445 this.WriteEndElement();
449 /// Writes the HTML header.
451 /// <param name="title">
454 /// <param name="cssPath">
457 /// <param name="cssStyle">
460 private void WriteHtmlHeader(string title, string cssPath, string cssStyle)
462 this.WriteStartElement("head");
466 this.WriteElementString("title", title);
471 this.WriteStartElement("link");
472 this.WriteAttributeString("href", cssPath);
473 this.WriteAttributeString("rel", "stylesheet");
474 this.WriteAttributeString("type", "text/css");
475 this.WriteEndElement(); // link
478 if (cssStyle != null)
480 this.WriteStartElement("style");
481 this.WriteAttributeString("type", "text/css");
482 this.WriteRaw(cssStyle);
483 this.WriteEndElement();
486 this.WriteEndElement(); // head
487 this.WriteStartElement("body");
491 /// Writes the start figure element.
493 private void WriteStartFigure()
495 this.figureCounter++;
496 this.WriteStartElement("p");
497 this.WriteClassId("figure");