Abstract:
This article describes how to use the open-source chart component visifire in siverlight to display data in A Multidimensional Dataset. Because the structure of a multi-dimensional dataset is complex, this article uses one-dimensional query results, focusing on methods from OLAP to Silverlight.
Environment Overview:
This document uses Silverlight 2 and visifire is 2.0.4 beta. The version of the multi-dimensional dataset is 2008. The multi-dimensional dataset used in the example is the Microsoft multi-dimensional dataset adventure works DW 2008. We recommend that you only display the complex multi-dimensional dataset query in the form of tables, in order not to make the chart look messy, we recommend that you use a simple one-dimensional series. In this multi-dimensional data set, we use the following MDX statement as an example:
Select [measures]. [reseller order count] On 0,
[Product]. [category]. members on 1
From [sales targets]
The query result is as follows:
Why visifire?
In fact, Microsoft's Silverlight toolkit already has a chart component, but I think the effect is not as good as that of visifire, and every data point datapoint in visifire can be clicked, this is important for bi-related projects, because operations like drill down/up and drill throught to detail depend on this.
Create SilverlightProject:
Open Visual Studio 2008, select a file, and create a new Silverlight project. When creating a project, you will be prompted whether to use the Asp.net web project to host the Silverlight project or only use a simple test page to host the Silverlight project. Select the Asp.net web application project.
Silverlight itself, as a browser plug-in, does not have system. for namespaces such as data, if you need to obtain data from a database or other data sources, you need to use WebService and similar methods. Therefore, we will build the WebService-related resources that provide data for Silverlight in the Asp.net web applition project of the boarding Silverlight just now.
Add visifireTo the page:
First, add visifire reference to the project. Here we only need to add a DLL file, which can be found in:
Http://visifire.com/download_silverlight_charts.php
Download. After downloading and decompressing the package, you can see the following two DLL files:
Wpfvisifire. Chart. dll is used in the WPF application, and slvisifire. charts. dll is used in the Silverlight project.
Right-click references under the Silverlight project just created, and select Add references ..., In the pop-up add reference form, select the Browse tab, locate the visifire download directory, find slvisifire. charts. dll, and click OK.
Because Visual Studio itself does not support drag-and-drop during the design of the Silverlight control, blend2 is needed here. Right-click the page. XAML file and choose open in expression blend .... If blend2 + SP1 is correctly installed, this option will appear.
In blend, locate the visifire chart control we added and put it in page. XAML. The specific method is as follows:
For example, find> in the blend toolbar and click it. The Asset Library dialog box appears. select Custom Controls and click chart. This chart is the visifire chart we want to add. Now the mouse is changed to a cross shape, and a rectangular area is drawn on the panel. Now the visifire chart has been added.
Versions later than visifire 2 are supported in blend design, so the placeholder can be seen in blend. In addition, we also need to give this chart a name (no name by default), so that it can be referenced in the C # file in the future:
You can also create a chart in the Code to the Panel. The document of visifire contains many sample codes, which are not described in detail here.
Create a WebServiceService:
Return to Visual Studio. Because the data comes from a multi-dimensional dataset, you need to add a reference to adomd. net. Right-click the Asp.net web application project and choose add reference ..., Find Microsoft. analysisservices. adomdclient and select version 10, for example:
Next, add the service file, right-click the Asp.net web application project, and select Add-new item ..., In the displayed dialog box, select Web Service and add a web service to the project.
Open the service file and add the following reference at the top:
Using Microsoft. analysisservices. adomdclient;
Visual Studio adds a hello World method by default, deletes it, And then adds the following code:
[Webmethod]
Public list <chartitem> getresult ()
{
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;
Comm. commandtext = "select [measures]. [reseller order count] On 0, [product]. [category]. [category]. members on 1 from [sales targets]";
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;
}
The return type is list <chartitem>, where chartitem is a custom class, the Code is as follows:
Public class chartitem
{
Public String title {Get; set ;}
Public double value {Get; set ;}
}
The Data Query Process is similar to that of the database query, except that the multidimensional dataset has special objects such as adomdconnection and adomdcommand. There are many cellset methods for receiving results. However, because only one-dimensional series are used, the structure is relatively simple. Therefore, it is sufficient to use adomddatareader to receive the results. Then, the chartitem object created by dr. Read () is added to the result of the List <chartitem> type.
For more information about adomd. net, see the author's article:
Adomd. Net 9 Note --- adomdconnection
10 is used in this article, but according to the experience of aspnetx 9 and 10, it does not change much (I suspect not even ).
Now, the service has been created and you can view the service. If you can return a page similar to the following, the service is running properly:
In addition, we need to record the service address. You can right-click the created web service file and select View in browser. Then, we can see the following in the pop-up browser interface:
Copy this address, which will be used later.
VisifireRead WebServiceData and display:
Return to the Silverlight project, right-click the Silverlight project, and select add service reference... (Note that this is different from adding a reference.Service reference). In the displayed interface, enter the copied address in address and click go. The system will detect the services under this address, select the service we just created, namespace here we select default, and then click OK.
Open page. XAML. CS and add the following reference:
Using slvisifire. servicereference1;
Using visifire. charts;
The first reference is to create a service instance without being too long. The second reference is to use the following classes in visifire: dataseries and datapoint. The former is a data source (sequence) that can be used as a visifire chart instance. It is a set, and the latter is an element.
Finally, replace the existing code with the following code in the page:
Public page ()
{
Initializecomponent ();
This. Loaded + = new routedeventhandler (page_loaded );
}
Void page_loaded (Object sender, routedeventargs E)
{
Webservice1soapclient WC = new webservice1soapclient ();
WC. getresultcompleted + = new eventhandler <getresultcompletedeventargs> (wc_getresultcompleted );
WC. getresultasync ();
}
Void wc_getresultcompleted (Object sender, getresultcompletedeventargs E)
{
Dataseries DS = new dataseries ();
Foreach (chartitem item in E. Result)
{
Datapoint dp = new datapoint ();
DP. axisxlabel = item. title;
DP. yvalue = item. value;
DS. datapoints. Add (DP );
}
Chttest. Series. Add (DS );
}
Now, the project has been created and run. The following page is displayed:
The actual effect is animated, which is better than static images.
[Download project files in this article]
Summary:
This document describes how to use visifire to display data in a multi-dimensional dataset in Silverlight. In this article, I will continue to use visifire to implement more presentation functions.
Chowchow, this is especially for you, I love you.