Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WaveformGraph To HTML Example

I'm not sure if anyone has a use for this.  It is a small portion of code that I used to generate and display an HTML report from within a WinForms application.  I left the example as simple as possible so that it could be modified easily.  Basically it just displays some graphs with random data in an HTML file.  All the methods are static so calling HTML.Report.Create(); should display a HTML page in the default browser.

 

Adding a couple of extra lines and using a Javascript image carousel like Awkward Showcase makes for a really nice interactive report.

 

Cheers,

Tony

 

I couldn't add an attachment so here is the code:

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using NationalInstruments.UI.WindowsForms;
using NationalInstruments.UI;
using NationalInstruments.Analysis;

namespace HTML
{
    public class Report
    {
	    public static void Create()
	    {	

			string HTMLPath = Path.Combine(Application.StartupPath, "HTML_Report");
			Random random = new Random();
			
			if(!Directory.Exists(HTMLPath))
			{
				Directory.CreateDirectory(HTMLPath);	
			}
			
			
			string HTMLFileName = Path.Combine(HTMLPath,  "NI_ChartReport.htm"); 
			
			FileStream fs = new FileStream(HTMLFileName, FileMode.Create);
			StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
			
			w.WriteLine("<html>");
			w.WriteLine("<body>");
			
			int numCharts = 10;
			for(int loop = 0; loop < numCharts; loop++)
			{
			
				int numPoints = 1000;
				double[] values = new double[numPoints];
				
				
				for(int i = 0; i < numPoints; i++)
				{
					values[i] = random.NextDouble() * loop +1;	
				}
						
				//Create a new chart
				double xAxisInc = 1.0;	
				WaveformGraph graph = AddChart("Chart Label", "XAxis Label", "YAxis Label");
				AddPlotToChart(graph, "Values" + loop.ToString(), "Units", values, graph.YAxes[0], 0.0, xAxisInc);
				
				//Create and use a mono spaced font for labels to ensure charts are all the same size
				FontConverter cvt = new FontConverter();
				Font f = cvt.ConvertFromString("Courier New, 8.25pt, style=Bold") as Font;
				graph.XAxes[0].MajorDivisions.LabelFont = f;
				graph.YAxes[0].MajorDivisions.LabelFont = f;
				//Force the format of labels so the charts are all the same size
				graph.YAxes[0].MajorDivisions.LabelFormat = new FormatString(FormatStringMode.Numeric, "6:0");
				
				//Create an image of the chart and insert into the HTML report
				string newPage = "" + (char) 12;
				Image pic = graph.ToImage(new Size(640, 240));
				string picFileName = Path.Combine(HTMLPath, "Values" + loop.ToString() + ".png");
				pic.Save(picFileName);
				w.WriteLine("<img src='" + picFileName + "' alt='" + picFileName + "'>");		
			}
				
	
			w.WriteLine("</body>");
			w.WriteLine("</html>");
			w.Flush();
			fs.Close();
		
			System.Diagnostics.Process.Start(HTMLFileName);
			
						
        }
		
		
		//This method creates a new chart with some default configuration settings
		public static WaveformGraph AddChart(string caption, string xAxisCaption, string yAxisCaption)
		{

			//Create the axes for the chart
			YAxis yAxis = CreateYAxis(yAxisCaption);
			yAxis.MajorDivisions.GridLineStyle = NationalInstruments.UI.LineStyle.Dot;
			//yAxis.MajorDivisions.GridVisible = true;
			XAxis xAxis = CreateXAxis(xAxisCaption);
			xAxis.MajorDivisions.GridLineStyle = NationalInstruments.UI.LineStyle.Dot;
			//xAxis.MajorDivisions.GridVisible = true;

			WaveformGraph newGraph = new WaveformGraph();
			newGraph.Border = Border.None;
			newGraph.Caption = caption;            
			newGraph.XAxes.Clear();
			newGraph.UseColorGenerator = true;
			newGraph.XAxes.Add(xAxis);
			newGraph.YAxes.Clear();
			newGraph.YAxes.Add(yAxis);
			newGraph.ZoomAnimation = false;           
			newGraph.Plots.Clear();

			return newGraph;

		}
		
		//This method adds a new plot to a chart and plots the supplied data
		public static WaveformPlot AddPlotToChart(WaveformGraph  graph, string channelName, string Units, double[] yValues, YAxis yAxis, double xStartValue, double xIncrement)
		{
			WaveformPlot newPlot = new WaveformPlot();
			newPlot.Tag = channelName;
			graph.Plots.Add(newPlot);
			newPlot.YAxis = yAxis;
			newPlot.PlotY(yValues, xStartValue, xIncrement);
 
			return newPlot;
		}
		
		public static YAxis CreateYAxis(string caption)
		{
			YAxis axis = new YAxis();
			axis.Range = new Range(-1, 1);
			axis.Mode = NationalInstruments.UI.AxisMode.AutoScaleVisibleLoose;
			axis.Caption = caption;
		
			return axis;
		}

		public static XAxis CreateXAxis(string caption)
		{
			XAxis axis = new XAxis();
			axis.Caption = caption;
		
			return axis;
		}
		
    }

}

 

Message 1 of 1
(5,049 Views)