Monday, March 14, 2011

DD Reports - Advanced interactive charts, part 1

General
One of the amazing things about charts in Data Dynamics Reports is they may be interactive. You can check out chart drillthrough screencast to see this feature in action. But perhaps you need to adjust the way the charts show the subsequent data by displaying the popup window instead of displaying the drillthrough report in the same window? Yes, sir, Data Dynamics Reports allows to do this. In this article we will see how it can be achieved in the windows-forms based report viewer. The next article will give some ideas on how to implement this feature in the web-based report viewer. As usual let's formulate the problem and then solve it step by step.

The problem
DistrictSales.xml(change extension to rdlx) displays the pie chart of the sales by district. If a user clicks on the piece of pie, then the StoreSales.xml(change extension to rdlx) is opened and shows the detailed information about the sales by store in the corresponding district. Now we want to adjust this interactivity - if a user clicks on the piece of pie, then the popup window hosting the sales by store appears. For your convenience I created AdvancedChartDrillthrough.zip we can start with, implementing the requirements step by step. The rest of the article walks through these steps.

In action
(1)Open AdvancedChartDrillthrough project in Visual Studio 2008. Adjust the references to DDR assemblies if it's needed, compile and run it. The main form hosts the viewer that displays DistrictSales report with the default interactivity - clicking on the piece of the pie chart shows StoreSales report in the same window. We are about to adjust it.

(2)Open the code of MainForm.cs, add the handler of Action event of the viewer before the call to OpenReport method, like below:

public MainForm()
{
// the rest of the code
viewer.Action += viewer_Action;
viewer.OpenReport(new FileInfo("DistrictSales.rdlx"));
}
void viewer_Action(object sender, DataDynamics.Reports.ActionEventArgs e)
{
}

(3)Action event is raised when a user clicks an interactive element - hyperlink, drill through link or bookmark link. In the handler of Action method you can cancel the default processing and perform the custom steps, like opening the drillthrough reports in the popup window. Let's start with handling action event - we will extract the information about report to jump and set its parameters values:

void viewer_Action(object sender, ActionEventArgs e)
{
// we are interested in drill through actions only.
if(e.Action.ActionType==ActionType.DrillThrough)
{
// cancel the default processing
e.Cancel = true;
var drillthroughInformation = e.Action.Drillthrough;
// get the report to jump
ReportRuntime drillthroughReport = drillthroughInformation.ReportRuntime as ReportRuntime;
if(drillthroughReport == null)
throw new InvalidOperationException("unable to get the drillthrow report!");
// set the parameters if any
for(var i=0;i<drillthroughInformation.NumberOfParameters;i++)
{
drillthroughReport.Parameters[i].CurrentValue = drillthroughInformation.Parameters[i].Value;
}
}
}

(4) The remaining steps are trivial - we need to open the drillthrough report in the popup form. To do this let's adjust the code of PopupForm.cs - extract "viewer" field and add OpenReport method:

public partial class PopupForm : Form
{
private readonly ReportPreview viewer = null;
public PopupForm()
{
InitializeComponent();
viewer = new ReportPreview();
viewer.IsToolbarVisible = false;
viewer.PageBorder = PageBorder.None;
viewer.Dock = DockStyle.Fill;
viewer.PreviewMode = PreviewMode.Layout;
Controls.Add(viewer);
}
public void OpenReport(ReportRuntime reportRuntime)
{
viewer.OpenReport(reportRuntime, "sales by store");
}
}

and finally let's show the popup form in viewer_Action handler code(at the end of the method body):

void viewer_Action(object sender, ActionEventArgs e)
{
// the rest of the code
var popupForm = new PopupForm();
popupForm.Show();
popupForm.OpenReport(drillthroughReport);
}

(5)AdvancedChartDrillthrough_final.zip the final version of AdvancedChartDrillthrough project.


Next time
Interactivity involves the popup form on the web-based report viewer is going to be interesting to deal with!