Use visifire in Silverlight to display data in a cube (2)

Source: Internet
Author: User

Abstract:

Based on the previous article, this article describes how to add the filter function to charts and dynamically change chart types based on data.

Add a filter for the chart:

Implementation: Based on the MDX statement in the previous example, see the following MDX statement:

Select [measures]. [reseller order count] On 0,

[Product]. [category]. members on 1

From [sales targets]

Where [sales territory]. [sales territory country]. & [United States]

The where section is added, so we implement the where query to filter the results.

By the way, the filtering of multiple values is as follows:

Select [measures]. [reseller order count] On 0,

[Product]. [category]. members on 1

From [sales targets]

Where {[sales territory]. [sales territory country]. & [United States], [sales territory]. [sales territory country]. & [United Kingdom]}

Here we only use simple examples, so there is only one filter condition.

First, you must obtain all the members in a dimension. Although the dimension information can be obtained under the adomdconnection object, because the Members in both layers and levels are not well determined, I use a stupid method to query it through the MDX statement. As follows:

Select [measures]. [reseller order count] On 0,

[Sales territory]. [sales territory country]. [sales territory country]. members on 1

From [sales targets]

Query Result:

For example, the start position of the column and the start position of the row are marked. Based on this result, you only need to read the information of the 0th columns cyclically in the adomddatareader result. (I admit, this method is stupid)

Open the project created in the previous section, find the Web service file under the WEB Project, and add the following service code:

[Webmethod]

Public list <string> getcountrymember ()

{

List <string> result = new list <string> ();

Adomdconnection conn = new adomdconnection ();

Conn. connectionstring = "provider = msolap; Integrated Security = sspi; Data Source = localhost; Catalog = adventure works DW 2008 ;";

Conn. open ();

Adomdcommand comm = new adomdcommand ();

Comm. Connection = conn;

String strmdx = "select [measures]. [reseller order count] On 0, [sales territory]. [sales territory country]. [sales territory country]. members on 1 from [sales targets] ";

Comm. commandtext = strmdx;

Adomddatareader DR = comm. executereader ();

While (dr. Read ())

{

Result. Add (Dr [0]. tostring ());

}

Return result;

}

This service can (indirectly) Obtain members of the sales territory country level.

Then add a method to the service. This service is similar to the service mentioned in the previous section, except that there is a string-type parameter that is used to construct the part after where in the MDX query. The Code is as follows:

[Webmethod]

Public list <chartitem> getresultbycountry (string countryname)

{

List <chartitem> result = new list <chartitem> ();

Adomdconnection conn = new adomdconnection ();

Conn. connectionstring = "provider = msolap; Integrated Security = sspi; Data Source = localhost; Catalog = adventure works DW 2008 ;";

Conn. open ();

Adomdcommand comm = new adomdcommand ();

Comm. Connection = conn;

String strmdx = "select [measures]. [reseller order count] On 0, [product]. [category]. [category]. members on 1 from [sales targets]";

Strmdx + = "Where [sales territory]. [sales territory country]. & [" + countryname + "]";

Comm. commandtext = strmdx;

Adomddatareader DR = comm. executereader ();

While (dr. Read ())

{

Chartitem CI = new chartitem ();

CI. Title = Dr [0]. tostring ();

CI. value = double. parse (Dr [1]. tostring ());

Result. Add (CI );

}

Return result;

}

So far, the code of the service has been built.

Next, you need to update the service reference of Silverlight:

For example, in the Silverlight project, expand service references service reference, find the added service in the previous section, right-click it, and choose Update Service reference to update service reference. Visual Studio will regenerate the local proxy class based on the WSDL in the Web service.

After the service is updated, open the visifire chart interface created in the previous section and add a ListBox to the interface to filter the country list.

The XAML file here is as follows:

<Usercontrol X: class = "slvisifire. Page"

Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"

Width = "500" Height = "300" xmlns: visifire_charts = "CLR-namespace: visifire. charts; Assembly = slvisifire. Charts">

<Grid X: Name = "layoutroot" background = "white">

<Visifire_charts: Chart margin = "31,38, 170,77" name = "chttest"/>

<ListBox horizontalalignment = "right" margin = "33,103, 113" width = "" name = "lstcountry"/>

</GRID>

</Usercontrol>

Note: You can add ListBox through blend. aspnetx acknowledges that hard-hitting in Visual Studio is painful. The result XAML code is provided directly here. For details about how to add ListBox in blend, refer to the Silverlight tutorial of terrylee.

Finally, open the corresponding XAML. CS file. here you need to make major changes to the previous Code. All the code is as follows:

Code
Namespace slvisifire

{

Public partial class page: usercontrol

{

Webservice1soapclient WC = new webservice1soapclient ();

List <chartitem> lstitem = new list <chartitem> ();

Public page ()

{

Initializecomponent ();

This. Loaded + = new routedeventhandler (page_loaded );

}

Void page_loaded (Object sender, routedeventargs E)

{

WC. getcountrymembercompleted + = new eventhandler <getcountrymembercompletedeventargs> (wc_getcountrymembercompleted );

WC. getresultbycountrycompleted + = new eventhandler <getresultbycountrycompletedeventargs> (wc_getresultbycountrycompleted );

WC. getcountrymemberasync ();

// Call the service for loading chart data only when the ListBox option is changed

Lstcountry. selectionchanged + = new selectionchangedeventhandler (lstcountry_selectionchanged );

// Place an empty Data Sequence in it for subsequent Index

Dataseries DS = new dataseries ();

Chttest. Series. Add (DS );

}

Void lstcountry_selectionchanged (Object sender, selectionchangedeventargs E)

{

WC. getresultbycountryasync (listboxitem) lstcountry. selecteditem). content. tostring ());

}

Void wc_getresultbycountrycompleted (Object sender, getresultbycountrycompletedeventargs E)

{

Lstitem. Clear ();

Foreach (chartitem item in E. Result)

{

Lstitem. Add (item );

}

Loadchart ();

}

Void wc_getcountrymembercompleted (Object sender, getcountrymembercompletedeventargs E)

{

Foreach (string item in E. Result)

{

Listboxitem li = new listboxitem ();

Li. content = item;

Lstcountry. Items. Add (LI );

}

// The first element is automatically selected after the country list is loaded to trigger the chart loading event.

Lstcountry. selectedindex = 0;

}

// Load the chart

Void loadchart ()

{

// Obtain the previously added data sequence and re-assign the data

Dataseries DS = chttest. Series [0];

DS. datapoints. Clear ();

Foreach (chartitem item in lstitem)

{

Datapoint dp = new datapoint ();

DP. axisxlabel = item. title;

DP. yvalue = item. value;

DS. datapoints. Add (DP );

}

}

}

}

 

You need to call the service twice. Load the country list for the first time, and then automatically load the other service after each list option change. Transfer the selected country to obtain the chart data again.

Since then, the filtering function for the chart has been modified. Run the program on the following interface:

The code in the preceding example uses one dimension as the basis for filtering. Of course, you can also use other or more dimensions for filtering. The method basically repeats the preceding steps and is not described too much here, if you have any questions, contact aspnetx.

Add dynamic selection for charts:

So far, we may not be able to meet the above implementation features, so we will add some dazzling features. For the effect of the previous graph, only the bar chart can be displayed, Which is boring. How can I change the chart type if I add one?

The visifire function can be easily implemented. However, we recommend that you use bar chart, pie chart, and horizontal chart to display discrete row variables, for continuous variables, line charts are suitable for display. Therefore, we can switch between a bar chart, a pie chart, and a horizontal chart.

First, add another ListBox in the interface.

<ListBox Height = "72" horizontalalignment = "right" margin = "113," verticalignment = "bottom" width = "" name = "lstcharttype" selectedindex = "0">

<Listboxitem content = "Bar Chart"/>

<Listboxitem content = "Pie Chart"/>

<Listboxitem content = "Horizontal Chart"/>

</ListBox>

Similarly, we recommend that you generate this operation in the blend operation.

Then, register the following event in the page (more specifically, the user control) constructor:

Lstcharttype. selectionchanged + = new selectionchangedeventhandler (lstcharttype_selectionchanged );

Then add the following response code:

Void lstcharttype_selectionchanged (Object sender, selectionchangedeventargs E)

{

Switch (listboxitem) lstcharttype. selecteditem). content. tostring ())

{

Case "Bar Chart ":

Chttest. Series [0]. renderas = renderas. column;

Break;

Case "Pie Chart ":

Chttest. Series [0]. renderas = renderas. Pie;

Break;

Case "Horizontal Chart ":

Chttest. Series [0]. renderas = renderas. bar;

Break;

}

}

Now, the function has been added and the program can be run to change the chart type through the newly added ListBox. Let's take a look at the two figures below:

It seems that it is not as boring as the previous functions?

 

[Click here to download the related project files]

Summary:

This article expands functions based on the previous article to achieve richer data presentation results. Through these two articles, I believe you will be more impressed with Silverlight and multi-dimensional data set MDX, and understand the advantages of using Silverlight to show Bi data.

In a future article, aspnetx intends to use its own control to display data in a multi-dimensional dataset.

Chowchow, this is especially for you, I love you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.