silverlight之图表控件应用

  快过年了,工作不怎么忙,闲来无事,把BI中客户端显示用silverlight图表实现了一下,思路还是老一套,ado.net读取数据,wcf推送数据,silverlight显示。

不废话,晒解决方案

为了最大化减少数据的传输量,我们采取服务端集中抓取数据,然后客户端用linq实现数据筛选,重新定义显示实体类,实现数据绑定

服务端同样新建一个实体类,根据返回的数据集合

  // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class ProjectDistributed
{
[DataMember]
public int proj_unid { set; get; }
[DataMember]
public string projid { set; get; }

[DataMember]
public string cname { set; get; }
[DataMember]
public string projstatus { set; get; }
[DataMember]
public string projtype { set; get; }

[DataMember]
public int projtypecount { get; set; }
[DataMember]
public int Days { set; get; }
[DataMember]
public int functionnum { set; get; }
[DataMember]
public int realworkload { set; get; }

}

新建服务契约,并实现:

 [ServiceContract]
public interface IService1
{
[OperationContract]
List<ProjectDistributed> GetData(int value);
}
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public List<ProjectDistributed> GetData(int value)
{
StringBuilder sqlString = new StringBuilder();

sqlString.Append(" select proj_unid,projid,cname,projstatus,projtype,Days,functionnum,realworkload as realworkload from Func_GetProjectDistributed('2006/1/1', GETDATE()) where dept_unid in ( select dept_unid from View_DepartmentName) and projtype in (select projtype from View_ProjectType) and projstatus in ( select projstatus from View_ProjectStatus )");
List<ProjectDistributed> myCL = null;
ProjectDistributed myOnePro = null;
using (SqlDataReader SDR = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, sqlString.ToString()))
{
myCL = new List<ProjectDistributed>();
while (SDR.Read())
{
myOnePro = new ProjectDistributed();
myOnePro.projid = SDR["projid"].ToString();
myOnePro.cname = SDR["cname"].ToString();
myOnePro.Days = Convert.ToInt32(SDR["Days"].ToString());
myOnePro.functionnum = Convert.ToInt32(SDR["functionnum"].ToString());
myOnePro.proj_unid = Convert.ToInt32(SDR["proj_unid"].ToString());
myOnePro.projstatus = SDR["proj_unid"].ToString();
myOnePro.realworkload = Convert.ToInt32(SDR["realworkload"].ToString());
myOnePro.projtype = SDR["projtype"].ToString();
//myOnePro.projtypecount = Convert.ToInt32(SDR["projtypecount"]);
myCL.Add(myOnePro);

}
}
return myCL;

}
}
}

公开服务,本来我本打算采用tcp协议传输的,优点不用说,但是调试了半天没通过,最后采用http协议,这个简单,记住加跨域文件,自定义binging实现数据量的增加:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DataConnectionString" connectionString="Data Source=WUXUELEI\MSSQLSERVER2008;Initial Catalog=kmswork;User ID=sa;Password=wuxuelei" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>

<services>
<service behaviorConfiguration="mybehavior" name="WcfService1.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"/>
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
<endpoint address="" bindingConfiguration="higherMessageSize" binding="basicHttpBinding" contract="WcfService1.IService1">

<!--
部署时,应删除或替换下列标识元素,以反映
用来运行所部署服务的标识。删除之后,WCF 将
自动推断相应标识。
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mybehavior">
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="higherMessageSize"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"/>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

客户端很简单,拖上控件,后台读取数据显示就行,记住先添加服务引用,生成客户端代码类:

 <Grid x:Name="LayoutRoot" Background="White">
<toolkit:Chart HorizontalAlignment="Left" Margin="37,36,0,0" Name="Mychart" Title="中心类型项目分布" VerticalAlignment="Top" Height="352" Width="427">
</toolkit:Chart>
</Grid>
  void client_GetDataCompleted(object sender, MyService.GetDataCompletedEventArgs e)
{
if (e.Error == null)
{
List<ProjectDistributed> myResult = e.Result.ToList<ProjectDistributed>();
myAxis axis = new myAxis()
{
FontStyle = FontStyles.Normal,
FontSize = 12f
};
PieSeries series = new PieSeries();
System.Collections.Generic.IEnumerable<Model> mySource = new List<Model>();
mySource = from p in myResult
group p by p.projtype
into g
select new Model
{
projtype = g.Key,
projtypecount = g.Count()
};
//myResult = myResult.GroupBy(p => p.projtype).
// Select
// (k => new Model
// {
// projtype = k.Key,
// projtypecount = k.Count()
// });

series.ItemsSource = mySource;
series.IndependentValueBinding = new System.Windows.Data.Binding("projtype");
series.DependentValueBinding = new System.Windows.Data.Binding("projtypecount");
series.AnimationSequence = AnimationSequence.LastToFirst;
Mychart.Series.Add(series);
}
else
MessageBox.Show(e.Error.ToString());
}

挺简单的一个实例,主要是思路,权作自己笔记只用,晒下结果,我们采用OOB模式:

呵呵..就到这里,上传下源码

原文地址:https://www.cnblogs.com/zhijianliutang/p/2325976.html