Skip to main content
RSS feed Subscribe to feed

 

Skip Navigation LinksHome  Extending the Spotfire Platform  Creating Extensions to the Spotfire Platform  Creating a Fitting Model  Fitting Model: Exporting the Curve Fit Result to Microsoft® Excel

©Spotfire 2011

Fitting Model: Exporting the Curve Fit Result to Microsoft® Excel

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);
    }

Refer to Creating a Tool for details.

Implementation Steps

  1. Add a new table containing the curve fit results to the analysis.
  2. Write the contents of the table to a file on disk.
  3. Clean up the resources.
  4. 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);
    }
}