Add Custom Buttons to SilverLight PivotView
ActiveAnalysis is all in one package when it comes to out-of-the box OLAP, data visualization and Business Intelligence features. Though its most appealing aspect is its UI; however like other GrapeCity powertools products, ActiveAnalysis allows users to customize it as per their requirements. The toolbar available for the SilverLight PivotView contains the features which are commonly used by the users. However a user may want to have some additional options readily available like the presence of a PDF export button or may be an Open/Save layout button. Let us see how we can modify the toolbar to extend its functionality. Here is the final output which we will get: The prerequisite for this is to have a silverlight project already created. You may want to check this link from our documentation which explains how we can create a silverlight viewer project. Once the the silverlight viewer is in place we can connect it to any datasource. Since we are concentrating on adding buttons to the toolbar, the first thing I would like to mention about the toolbar is that it is divided into different sections rather than individual items. In other words when you customize the ActiveAnalysis toolbar, you don't modify its items individually. Instead, the toolbar items are arranged by groups that you can show, hide, enumerate and expand by adding new buttons. These groups are:
- Layout: Clear layout, Open layout, Save layout buttons.
- Export view: Print preview, Copy, Export to buttons.
- UndoRedo: Undo, Redo buttons.
- Execution: Auto query, Execute query buttons.
- ViewSettings: SwapXY, Toggle schema, Toggle shelves, Cards, Grid settings, Format, Auto show legends buttons.
- Custom: Buttons to this group may be added for handling custom tasks.
In this post, I am going to demonstrate how we can add a Open/Save layout button to the Layout group and a PDF export button to the Custom group of the toolbar. So the code for adding the buttons looks something like this:
public MainPage()
{
InitializeComponent();
LocalCubeDataSource myData = new LocalCubeDataSource();
myData.CubeFile = @"C:\\Documents and Settings\\All Users\\Documents\\GrapeCity\\ActiveAnalysis\\DataSources\\NWind\\NwindLocalCube.ddacube";
pivotView1.Connection.AutoConnect = true;
pivotView1.DataSource = myData;
// To export to PDF
pivotView1.ToolbarSettings[ToolbarParts.Custom].AddButton( new ToolbarButton(null, "ExportPDF", GetImage("pdf"),
pv => new PdfExporter(pv).Save(true), pv => true, null, null));
// To loadlayout
pivotView1.ToolbarSettings[ToolbarParts.Layout].AddButton(
new ToolbarButton(null, "Open layout", GetImage("Open"),
pv => OpenLayoutAction(),
pv => true, null, null));
//To savelayout
pivotView1.ToolbarSettings[ToolbarParts.Layout].AddButton(
new ToolbarButton(null, "Save layout", GetImage("Save"),
pv => SaveLayoutAction(),
pv => pv.Connection != null && pv.Connection.IsConnected, null, null));
}
You must have noticed the GetImage method when adding the ToolbarButton. It actually picks the image from the image folder which we add to the project so that each added button has an image associated with it. Let's see how the GetImage method looks like:
private static BitmapImage GetImage(string key)
{
if (string.IsNullOrEmpty(key))
return null;
string assemblyName = typeof(App).Assembly.FullName;
assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(','));
Uri imageUri = new Uri(assemblyName + ";component/Images/" + key + ".png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(imageUri);
BitmapImage result = new BitmapImage();
result.SetSource(streamInfo.Stream);
return result;
}
Finally we need to add handlers which will handle the clicks on the Open/Save layout button. One thing to note about the PDF export button is that the only thing we need to make the export work is to add the "GrapeCity.ActiveAnalysis.Silverlight.Pdf.dll" reference to the project. Without this DLL the PDF export will not work. Let's also see how the handlers for Open/Save layout looks like:
private void OpenLayoutAction()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Analysis files (*.analysis)|*.analysis|All files|*.*";
dialog.Multiselect = false;
if (dialog.ShowDialog() != true)
return;
FileInfo file = dialog.File;
if (!file.Exists)
return;
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
settings.CloseInput = true;
using (Stream fs = file.OpenRead())
using (XmlReader reader = XmlReader.Create(fs, settings))
pivotView1.Read(reader, PersistSettings.Layout | PersistSettings.CardLayout);
}
private void SaveLayoutAction()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "analysis";
dialog.Filter = "Analysis files (*.analysis)|*.analysis|All files|*.*";
if (dialog.ShowDialog() != true)
return;
using (Stream fs = dialog.OpenFile())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\\t";
settings.Encoding = new UTF8Encoding(false);
settings.CloseOutput = true;
using (XmlWriter writer = XmlWriter.Create(fs, settings))
pivotView1.Write(writer, PersistSettings.Layout | PersistSettings.CardLayout);
}
}
So that is all that is required to add a button to the SilverLight PivotView's toolbar. A sample application can be downloaded using the link provided below. Please modify the Nwind data cube path in the sample according to the ActiveAnalysis installation path on your machine and set the web project as the startup project, so that the sample runs correctly. Download_C#_Sample