Automating Plot Setup for JSViz Using a Custom Tool in TIBCO Spotfire®
Last updated:
9:19am Dec 12, 2018

Prerequisites

Introduction

Sometimes it is beneficial to automate the creation of plots, and sometimes entire pages or sections of an analysis. As the JavaScript Visualization Framework (JSViz) does not perform any default rendering when created, it might be even more beneficial to do so with a JSViz setup. 

Custom Tool Implementation for Plot Creation

This example is using an implementation of the CustomTool class and you may reference he JavaScript Visualization Framework Core project in Spotfire installation's Modules folder in the package builder and visual studio to be able to build the example tool.

When adding HTML and JavaScript resources to the project in Visual Studio, it is necessary to set all included files to "Build Action Embedded Resource".

The following example code does the following:

  1. Creates a plot and adds it to the Document
  2. Creates URLReference objects with embedded content to the Document-wide JavaScript repository collection, if they are not already there
  3. Adds the key (name)  of the JavaScript file to the plot

To avoid premature immediate update events to fire before the plot is completely configured, the creation and configuration steps are wrapped in a transaction.

C#

public class AddPlotTool : CustomTool<Document>
{

	public AddPlotTool() : base("Add Plot")
	{
	}

	protected override void ExecuteCore(Document context)
	{
		context.Transactions.ExecuteTransaction(delegate {
			var viz = context.ActivePageReference.Visuals.AddNew<JSVisualizationModel>();

			//Add the JavaScript files needed to create a doughnut chart to the plot
			AddJs(context, viz, "scripts.jQuery.js");
			AddJs(context, viz, "scripts.d3.v3.min.js");
			AddJs(context, viz, "scripts.JSViz.js");
			AddJs(context, viz, "scripts.DoughnutChart.js");

			viz.ApplyUserPreferences();
			viz.AutoConfigure();
		}
		);

	}

	private void AddJs(Document context, JSVisualizationModel viz, string name)
	{
		// Get the common JavaScript file  repository 
		var repo = context.CustomNodes.AddNewIfNeeded<ContentRepository>();
		
		// Add  the JS File to the common repository 
		if (!repo.ContainsKey(name))
		{
			// Read the content of the script folder 
			var content =
				System.Text.Encoding.UTF8.GetString(GetEmbeddedResource("SpotfirePS.AddPlotTool_70." + name));
			var urlReference = new UrlReference(name, null, content, ContentType.JS, true);
			repo[name] = urlReference;
		}
		// Add the file to be used in the plot (order sensitive)
		viz.UrlInclusions.Add(name);
	}

///
Helper methods here --- see zip file for details
///

}

 

Attachments

AttachmentSize
Package icon spotfireps.addplottool_70.zip191.97 KB