一天终于又结束了!突然发现,不论我们做什么,在哪个岗位上,每天都会有一个或者多个的让我们意想不到的困难。对于那些困难,有的或许我们直接就能解决,有的或许需要我们去思考一下,而更有的要困扰我们几个小时,甚至是更长的时间。当 看到问题的原因所在时,很多时候我们都觉得这是一个很滑稽的答案,好像老天爷跟自己开的玩笑似的。
废话不多说了,今天在做统计图的时候,由于前段时间一直找不到合适的统计图插件,而项目又催的很紧,所以领导要求使用了C# 中的用户自定义控件制作的一个统计图来配合WPF的使用。
在此期间,也是费了我很大的精力去解决一个很小很小的问题,在此把整个过程也罗列出来,供大家参考。
(一)用户控件的创建
(二)实现代码
WPF窗体中:
<Window xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" x:Class="C1Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" WindowStyle="ToolWindow" WindowState="Maximized" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderThickness="1" x:Name="boderLeft" BorderBrush="#FFD4C9C9">
<StackPanel >
<Button Content="玫瑰图" x:Name="btnRoseDiagram" Click="btnRoseDiagram_Click"></Button>
<Button Content="柱状图" x:Name="btnBarChart" Click="btnBarChart_Click"></Button>
</StackPanel>
</Border>
<my:WindowsFormsHost x:Name="host" Grid.Column="1"/>
</Grid>
</Window>
用户控件中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C1Test
{
public partial class ucInterview : UserControl
{
public ucInterview()
{
InitializeComponent();
}
Random rand = new Random(DateTime.Now.Millisecond);
private void ucInterview_Load(object sender, EventArgs e)
{
chart1.Legends[0].Enabled = false; //设置不显示图例
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false; //设置没有网格水平方向的网格线
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; //设置没有垂直方向的网格线
#region 测试假数据
//测试假数据
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(double));
dt.Rows.Add("lcy", rand.Next(0, 100));
dt.Rows.Add("nq", rand.Next(0, 100));
dt.Rows.Add("xl", rand.Next(0, 100));
dt.Rows.Add("x2", rand.Next(0, 100));
dt.Rows.Add("x3", rand.Next(0, 100));
dt.Rows.Add("x4", rand.Next(0, 100));
dt.Rows.Add("x5", rand.Next(0, 100));
#endregion
double d = Convert.ToDouble(dt.Rows[0][1]);
string m = dt.Rows[0][0].ToString();
//将数据依次加载到柱形图控件中显示
for (int i = 0; i < dt.Rows.Count -1; i++)
{
chart1.Series[0].Points.AddXY(dt.Rows[i][0], dt.Rows[i][1]);
}
//timer.Start();
}
}
}
在
WPF
窗体中的代码,可以实现柱形图的动态更新:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace C1Test
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
System.Windows.Threading.DispatcherTimer timer = new
System.Windows.Threading.DispatcherTimer(); //创建一个时间对象进程
private void btnRoseDiagram_Click(object sender, RoutedEventArgs e)
{
timer.Tick += new EventHandler(timer_Tick); //设置更新事件
timer.Interval = new TimeSpan(0, 0, 1, 0, 5000); //设置timer事件的更新频率(天、时、分、秒、毫秒)
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
ucInterview interview = new ucInterview(); //在主窗体添加一个用户控件——柱形图
host.Child = interview;
}
private void btnBarChart_Click(object sender, RoutedEventArgs e)
{
}
}
}
最后的效果展示:
总结:关于VS总自带的这个chart控件,我感觉他是功能强大的,但是在页面上展示的效果确实有点让人难以接受。另外,对于这个Demo,就我自己而言,里边需要总结的东西还是比较多的,比如柱形图的X轴的值怎么从数字改成文字,怎么绑定数据,怎么动态更新,对我这个新手还是需要总结下的。如果哪个前辈有更好的解决方案,还望不吝赐教~~