This tool example creats a tool for scatter plots, exporting curve fit results to Excel via a file on disk.
Overview
This Export curve fit results tool is a custom tool with a VisualContent context:
- Register the name to be displayed in the context menu:
public ExportCurveFitTool() : base("Export curve fit results", null)
{
// Empty
}
- Restrict the to scatter plots only:
protected override bool IsEnabledCore(VisualContent context)
{
return (context is ScatterPlot) && base.IsEnabledCore(context);
}
Implementation Steps
- Add a new table containing the curve fit results to the analysis.
- Write the contents of the table to a file on disk.
- Clean up the resources.
- Open the file in Excel.
protected override void ExecuteCore(VisualContent context)
{
ScatterPlot scatterPlot = context as ScatterPlot;
if (scatterPlot.FittingModels.Count > 0)
{
DataManager data = scatterPlot.Context.GetService<DataManager>();
// take the first fitting model
FittingModel fm = scatterPlot.FittingModels[0];
// add a table with the curve fit results
DataSource ds = fm.GetResultsDataSource();
DataTable table = data.Tables.Add(fm.TypeId.Name, ds);
// create a data writer
DataWriter writer = data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter);
// create a temp file
string fileName = Path.GetTempFileName();
FileStream fs = File.OpenWrite(fileName);
writer.Write(fs, table, new IndexSet(table.RowCount, true), table.Columns.Names);
// clean up
fs.Close();
data.Tables.Remove(table);
// start excel
string xlsFileName = Path.ChangeExtension(fileName, "xls");
File.Move(fileName, xlsFileName);
Process.Start(xlsFileName);
}
}