This article targets: TIBCO Spotfire 2.1 and forward Download Source Code
Introduction
Although TIBCO Spotfire is written in .NET 2.0, you can run it with any later version of the framework. As a matter of fact, most users probably run it on .NET 3.5 which is pushed out with Windows Update. So, if you (as a developer) know that all end-users will be running Spotfire in .NET 3.5, there is nothing stopping you from making use of the latest and greatest features in .NET. This article proves my point by illustrating how easy it is to implement a custom visualization using the Windows Presentation Foundation (WPF) which was introduced in .NET 3.0.
WPF-Windows Forms Interoperability
If you are familiar with WPF, you are aware that it is a totally different framework compared to its predecessor (Windows Forms) in which Spotfire runs. However, the .NET framework makes it really easy to display WPF controls in a Windows Forms application using a visual control named ElementHost. This control wraps the WPF visual in a container that can be rendered inside of any Windows Forms UserControl. There are also controls available that enable the reverse interoperability scenario (i.e. hosting a Windows Forms control in WPF).
Creating the Visualization Step-by-Step
The following steps describe how to practically create your WPF visualization. In this example we will create a WPF clone of the traffic light visualization that is part of the examples projects shipped with the Spotfire SDK.
- In Visual Studio, create the project that will house your visualization. Note that it is important that you create a Windows Forms Control Library project as opposed to the WPF User Control Library.
Like always it is also good practice to create a separate project for your model objects.
- Create the model class of your visual (together with its factory) and register it with the VisualRegistrar as you would normally do (see How to Create Spotfire Visualization). These classes are contained in the Visualization project as seen above.
public sealed class CustomAddIn : AddIn
{
protected override void RegisterVisuals(AddIn.VisualRegistrar registrar)
{
base.RegisterVisuals(registrar);
registrar.Register(new TrafficLightChartFactory());
}
}
- Create the WPF control with all the XAML that contains the view of your custom visualization.
- Create the UserControl that will contain you visualization and register it with the ViewRegistrar. Again, this is standard procedure when creating custom visualizations.
public sealed class CustomAddIn : AddIn
{
// Override methods in this class to register your extensions.
protected override void RegisterViews(AddIn.ViewRegistrar registrar)
{
base.RegisterViews(registrar);
registrar.Register(typeof(Control), typeof(TrafficLightChart), typeof(TrafficLightChartControl));
}
}
- When designing your UserControl in Visual Studio, drag and drop the ElementHost from the Toolbox panel to your control. You might consider docking the Dock property of the control to ‘Fill’.
- Set the content of the ElementHost control to your WPF implementation as seen in the screenshot below. Note that the designer now displays a thumbnail of your WPF control inside of a Windows Forms UserControl.
- Fire up TIBCO Spotfire and enjoy the magic of the new WPF visualization.
Conclusions
If you can use WPF visualizations, there are numerous reasons why you should prefer to do so:
- It is easy to create graphically superior applications using WPF. Also, the framework simplifies the process of adding graphical effects to your visualizations, such as animations, fade-ins, transformations, etc.
- With WPF, the process of creating visualizations can be separated into two. A designer generates the XAML file containing the graphics using a design tool such as Microsoft Expression Designer and the developer writes the code-behind (implementation and integration) of that visualization using Visual Studio. Each person can focus on what they know best to help achieve the best result possible.
- WPF provides highly advanced data-binding mechanisms that makes it easier to populate graphical elements with values from the Spotfire data engine.
- Finally, WPF is the primary graphical engine of Windows Vista and hence more future compliant than its predecessor Windows Forms.