This tutorial extends Tutorial 1: Getting Started by adding a pie chart to the report. The following steps are illustrated in more details:
As mentioned above this tutorial extends Tutorial 1. Therefore it is expected that the reader has finished with Tutorial 1 before reading further.
The PieChart is another type of report element, just like Label and Picture. To add a chart to the report, simply add it to a page (or another container). Once the chart is added to the report, we have to position and resize the chart. This particular chart will hold quite a few pieces, so we will make it big enough. Set the Location of the chart to "225,0" and its Size to "250,250". Adjust the Location of the existing data range so that it does not overlap with the newly added chart. Set it to "0,300". Set some additional visualization properties - set PieBrush to "Blue", PieStroke to "Black" and PieStrokeThickness to "1".
Here is how the Report will look like after these modifications:
XAML Copy Code |
---|
... |
To provide data for the chart, set its DataSource property. In addition, the TargetMember property needs to be set to the name of the field, which will supply values for the chart. In this particular case, we are providing the data source by handling the QueryDetails event. If we pass all products as a data source of the pie chart, the report will look cluttered and the chart pieces will be very small due to the large amount of products displayed. One way to resolve this problem is to filter the data displayed on the chart according to some criteria. To do this we need to create a custom LINQ query.
Navigate to the main page code behind and add the following class member:
C# Copy Code |
---|
private List<XElement> cheapProducts; |
Visual Basic Copy Code |
---|
Private cheapProducts As List(Of XElement) |
This list will contain the result of the filtering query. Now go to the class constructor and add the following code (new portions highlighted in bold):
C# Copy Code |
---|
var document = XDocument.Load("Data/nwind.xml"); |
Visual Basic Copy Code |
---|
Dim document = XDocument.Load("Data/nwind.xml") |
Then modify the contents of the QueryDetails event handler to return the cheapProducts list as a data source for the PieChart. Here is how it will look like after the modification:
C# Copy Code |
---|
private void myReport_QueryDetails(object sender, QueryDetailsEventArgs e) |
Visual Basic Copy Code |
---|
Private Sub myReport_QueryDetails(ByVal sender As Object, ByVal e As QueryDetailsEventArgs) |
Running the application will now display the filtered list of products as illustrated below:
Tutorial 5 demonstrates how to paint the chart pieces with different colors and how to custom render the corresponding items in the data range.