效果图:
前台代码:
里面有一部分是我测试统计图的代码,不想改,感觉应该不影响理解....
<UserControl x:Class="Task_One.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts" mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="1400" > <Grid x:Name="LayoutRoot1" Background="AliceBlue"> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <!--<Grid Grid.Row="0"> <Button Content="Button" HorizontalAlignment="Left" Margin="188,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> <TextBox x:Name="myText" HorizontalAlignment="Left" Height="23" Margin="10,17,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="143"/> <TextBox x:Name="text18" HorizontalAlignment="Left" Height="23" Margin="430,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBox x:Name="text30" HorizontalAlignment="Left" Height="23" Margin="643,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBox x:Name="text40" HorizontalAlignment="Left" Height="23" Margin="858,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <TextBlock HorizontalAlignment="Left" Margin="358,20,0,0" TextWrapping="Wrap" Text="18岁占比:" VerticalAlignment="Top"/> <TextBlock HorizontalAlignment="Left" Margin="569,20,0,0" TextWrapping="Wrap" Text="30岁占比:" VerticalAlignment="Top"/> <TextBlock HorizontalAlignment="Left" Margin="795,20,0,0" TextWrapping="Wrap" Text="40岁占比:" VerticalAlignment="Top"/> <Button Content="修改" HorizontalAlignment="Left" Margin="1014,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/> </Grid>--> <Grid x:Name="LayoutRoot" Grid.Row="1" Background="AntiqueWhite"> <my:DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dgList" Width="250" Margin="10,10,0,60"> <my:DataGrid.Columns> <my:DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=OneWay}" Width="80"></my:DataGridTextColumn> <my:DataGridTextColumn Header="手机号" Binding="{Binding WORKSTATION,Mode=OneWay}" Width="150"></my:DataGridTextColumn> </my:DataGrid.Columns> </my:DataGrid> <vc:Chart Grid.Row="0" Name="chtChartOne" Height="300" Width="500"> <vc:Chart.AxesY> <!--Y间隔--> <vc:Axis Interval="20" Suffix="%"/> </vc:Chart.AxesY> </vc:Chart> </Grid> </Grid> </UserControl>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.ObjectModel; using Task_One.ServiceReference1; using System.Reflection; using Visifire.Charts; //因为要用到Assembly,所以引入此空间 namespace Task_One { public partial class MainPage : UserControl { public MainPage() { _YV18 = new Random().Next(45); _YV30 = new Random().Next(30); _YV40 = new Random().Next(20); InitializeComponent(); InitPage(); } private void client_GetDatabyNameCompleted(object sender, GetDatabyNameCompletedEventArgs e) { System.Collections.ObjectModel.ObservableCollection<ServiceReference1.Class1> temp = new ObservableCollection<ServiceReference1.Class1>(); temp = e.Result; dgList.ItemsSource = temp; //for (int i = 0; i < temp.Count; i++) //{ // MessageBox.Show(temp[i].ID.ToString() + " and " + temp[i].WORKSTATION.ToString()); //} } private void Button_Click_1(object sender, RoutedEventArgs e) { Task_One.ServiceReference1.ServiceClient client = new Task_One.ServiceReference1.ServiceClient(); client.GetDatabyNameAsync(Convert.ToInt32(myText.Text.ToString())); client.GetDatabyNameCompleted += new EventHandler<GetDatabyNameCompletedEventArgs>(client_GetDatabyNameCompleted); client.CloseAsync(); } #region 柱状图示例 /// <summary> /// 画柱状图 /// </summary> public void InitPage() { chtChartOne.Series.Clear(); chtChartOne.View3D = true; //Title title = new Title(); //title.Text = "柱状图的标题内容"; //chtChartOne.Titles.Add(title); //统计资料列 DataSeries ds = new DataSeries(); //柱状类型 ds.RenderAs = RenderAs.StackedColumn; //显示Lable ds.LabelStyle = LabelStyles.OutSide; ds.LabelEnabled = true; //栏 ds.DataPoints.Add(new DataPoint() { AxisXLabel = "18-29岁", YValue = YV18 }); ds.DataPoints.Add(new DataPoint() { AxisXLabel = "30-39岁", YValue = YV30 }); ds.DataPoints.Add(new DataPoint() { AxisXLabel = "40-49岁", YValue = YV40 }); ds.DataPoints.Add(new DataPoint() { AxisXLabel = "50-64岁", YValue = 18.9 }); ds.DataPoints.Add(new DataPoint() { AxisXLabel = "65岁以上", YValue = 17.2 }); chtChartOne.Series.Add(ds); } #endregion /// <summary> /// 修改比例值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_2(object sender, RoutedEventArgs e) { YV18 = Convert.ToInt32(text18.Text.ToString()); if (Convert.ToInt32(text30.Text.ToString()) >= 0) { YV30 = Convert.ToInt32(text30.Text.ToString()); } else { YV30 = 0; } if (Convert.ToInt32(text40.Text.ToString())>=0) { YV40 = Convert.ToInt32(text40.Text.ToString()); } else { YV40 = 0; } InitPage(); } private Int32 _YV18; private Int32 _YV30; private Int32 _YV40; public Int32 YV18 { get { return _YV18; } set { _YV18 = value; } } public Int32 YV30 { get { return _YV30; } set { _YV30 = value; } } public Int32 YV40 { get { return _YV40; } set { _YV40 = value; } } } }
源码:http://pan.baidu.com/s/1o6p2F9o
参考资料:http://www.cnblogs.com/Kinglee/archive/2009/08/25/1553938.html
http://www.nbcoder.net