C#--工作笔记(直方图整体UserControls)
2013-12-25 10:16
295 查看
<UserControl x:Class="Health365IIProjectView.HartHistogramControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Loaded="UserControl_Loaded" SizeChanged="UserControl_SizeChanged"> <Canvas x:Name="HeartbeatHistogram" Background="DarkGray"> </Canvas> </UserControl>
===================================================================================================================
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; using Health365IIProjectBLL; namespace Health365IIProjectView { //internal delegate void user_OnClick(int index); /// </summary> /// HartHistogramControl.xaml 的交互逻辑 /// </summary> public partial class HartHistogramControl : UserControl { //声明委托 internal delegate void user_OnClick(int index); //声明事件 internal event user_OnClick UXEvent; /// <summary> /// 用来矩形颜色的显示 /// </summary> private Color[,] colorsSolution = new Color[2, 3] { {Color.FromRgb(0,142,143),Color.FromRgb(0,113,113),Color.FromRgb(0,92,93)}, {Color.FromRgb(246,188,16),Color.FromRgb(194,153,11),Color.FromRgb(111,139,161)} }; /// <summary> /// 保存心率直方图的数组 /// </summary> private int[] _HeartRateArray; /// <summary> /// 左边框的属性值 /// </summary> private int _leftBorder = 35; /// <summary> /// <summary> /// 右边框的属性值 /// </summary> private int _rightBorder = 20; /// <summary> /// 上边框的属性值 /// </summary> private int _topBorder = 10; /// <summary> /// 下边框的属性值 /// </summary> private int _bottomBorder = 20; /// <summary> /// 数据的单位 /// </summary> private int _dataUnit; /// <summary> /// 数据单位的显示 /// </summary> private string _unitName; /// <summary> /// x轴单位像素宽度 /// </summary> private double _xUnitP; /// 纵轴刻度个数 /// </summary> private int _yUnitCount; /// <summary> /// 纵轴一个刻度对应的值 /// </summary> private int _yUnitValue; public HartHistogramControl() { InitializeComponent(); } /// <summary> /// 设置数据,并开始构建直方图; /// </summary> /// <param name="data"></param> /// <param name="dataUnit"></param> /// <param name="unitName"></param> public void SetData(int index,int[] data,int dataUnit,string unitName) { //获取数组 _HeartRateArray = data; //用来显示一个值 _dataUnit = dataUnit; //单位显示 _unitName = unitName; if (_HeartRateArray != null) { Drawing(index); } //if (index != 0) //{ // if (_HeartRateArray[index] == 0) // { // } // else // { // CreateChartColumn(index, _xUnitP - ActualWidth / 350, _HeartRateArray[index] * 30 / _yUnitValue, _leftBorder + _xUnitP * index , _bottomBorder + 2, Color, _HeartRateArray[index]); // } //} } /// <summary> /// 开始画心率直方图 /// </summary> private void Drawing(int _index) { _yUnitCount = Convert.ToInt32(ActualHeight - _topBorder - _bottomBorder) / 30; //求出一个刻度的单位大小 if (_yUnitCount <= 0) { } else { _yUnitValue = _HeartRateArray.Max() / _yUnitCount; } this.HeartbeatHistogram.Children.Clear(); //清理画板 //当前宽度-左边距-右边距=X轴的长度/数组长度=X轴一个单位的像素 _xUnitP = (ActualWidth -_leftBorder -_rightBorder)/_HeartRateArray.Length; //y轴的长度 int y0 =Convert.ToInt32(ActualHeight - _bottomBorder); //画坐标X从左边距为起点,到右边距结束,定位为y轴的开始位置 DrawingPolyline(new Point(_leftBorder, y0), new Point(ActualWidth - _rightBorder, y0)); //画坐标Y长度开始,到上边距结束,定位为坐边距 DrawingPolyline(new Point(_leftBorder, y0), new Point(_leftBorder, _topBorder)); //x坐标间距 for (int i = 1; i <= _HeartRateArray.Length; i++) { //5个大格1个单位 if (i % 5 == 0) { Label l = new Label(); //设置X轴显示的位置 l.SetValue(Canvas.TopProperty, y0+2.0); //设置X轴间隔与字体的对应位置 l.SetValue(Canvas.LeftProperty, _leftBorder + _xUnitP * i - 15); //显示值 l.Content = (_dataUnit * i).ToString(); this.HeartbeatHistogram.Children.Add(l); //画X坐标的间距 从左边距+一个格子像素值*显示值的距离=坐标间距的长度 定位Y轴长度 +3为了让坐标显示; DrawingPolyline(new Point(_leftBorder + _xUnitP * i, y0), new Point(_leftBorder + _xUnitP * i, y0 + 3)); } } //y坐标间距 for (int i = 0; i <= _yUnitCount; i++) { //画Y坐标的间距 从左边距开始 -2为了让坐标显示,y轴的长度-30(显示的距离)*i=要显示Y间距的长度 DrawingPolyline(new Point(_leftBorder, y0 - 30 * i), new Point(_leftBorder - 2, y0 - 30 * i)); Label l = new Label(); //设置Y值显示的位置 l.SetValue(Canvas.TopProperty, y0 - 30 * i - 13.0); //设置Y值对齐方式,以右边框对齐(值的个位对齐) l.SetValue(Canvas.RightProperty, ActualWidth - _leftBorder - 3); //设置显示的值 l.Content = (_yUnitValue * i).ToString(); this.HeartbeatHistogram.Children.Add(l); } //画柱状体,根据数组的长度添加的高度和宽度 for (int index = 0; index < _HeartRateArray.Length; index++) { //当等于0时,不显示值 if (_HeartRateArray[index] == 0) { } else { if (_yUnitCount <= 0) { } else { if (_index == index) { CreateChartColumn(_index, _xUnitP - ActualWidth / 350, _HeartRateArray[index] * 30 / _yUnitValue, _leftBorder + _xUnitP * index, _bottomBorder + 2, 1, _HeartRateArray[index]); } else { //开始画矩形图 1.宽度 除350是为了不要显示太宽 2.显示的值 3.左边距 4.下边距 5.颜色 CreateChartColumn(index, _xUnitP - ActualWidth / 350, _HeartRateArray[index] * 30 / _yUnitValue, _leftBorder + _xUnitP * index, _bottomBorder + 2, 0, _HeartRateArray[index]); } } } } //总和 int sum = _HeartRateArray.Sum(); DrawTextValue("R--R期间直方图,共计统计心搏" + sum + "次",_leftBorder,0); //数量 DrawTextValue("数量",0,0); //用来传入单位的 //1.单位。2,X轴的终点位置。3,Y轴的终点位置 DrawTextValue(_unitName, ActualWidth - _rightBorder - 2, y0-10); } public void DrawTextValue(String result, double Left, double top) { TextBlock tb = new TextBlock(); tb.Text = result; tb.Margin = new Thickness(Left,top,0,0); this.HeartbeatHistogram.Children.Add(tb); } /// <summary> /// 画XY坐标方法 /// </summary> private void DrawingPolyline(Point p1, Point p2) { Polyline p = new Polyline(); p.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)); p.StrokeThickness = 1.0; p.Points.Add(p1); p.Points.Add(p2); this.HeartbeatHistogram.Children.Add(p); } //布局调整 private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { //SetCreateChartColumn(21); //if (_HeartRateArray != null) // { // this.HeartbeatHistogram.Children.Clear(); // Drawing(); // } } private void UserControl_Loaded(object sender, RoutedEventArgs e) { } /// <summary> /// 绘制3D矩形图 /// </summary> /// <param name="width">宽度</param> /// <param name="height">显示的值</param> /// <param name="left">左边距</param> /// <param name="bottom">下边距</param> /// <param name="colorFill">颜色</param> public void CreateChartColumn(int index,double width, double height, double left,int bottom,int colorFill,int arr) { Cylinder3DControl p3dc = new Cylinder3DControl(); Color[] cList = setColor(colorFill); if (width < 0 || height < 0) { } else { p3dc.InitSettingCylinder3DControl(width, height, cList); p3dc.Height = height + width / 3; p3dc.Width = width / 3 * 4; } p3dc.Tag = index; p3dc.ToolTip=arr; Canvas.SetLeft(p3dc, left);//用来控制X矩形显示开始的位置 Canvas.SetBottom(p3dc, bottom); p3dc.Margin = new Thickness(0, 0, 0, 0); //事件点击 p3dc.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown); this.HeartbeatHistogram.Children.Add(p3dc); } /// <summary> /// 改变颜色 /// </summary> /// <param name="index"></param> /// <returns></returns> private Color[] setColor(int index) { Color[] clist = new Color[3]; clist[0] = colorsSolution[index, 0]; clist[1] = colorsSolution[index, 1]; clist[2] = colorsSolution[index, 2]; return clist; } /// <summary> /// 只做变色处理,逻辑处理过程在page页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void sp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Color[] cDefaultList=setColor(0); Color[] cNewList = setColor(1); Cylinder3DControl p3dc = (Cylinder3DControl)sender; foreach (UIElement uie in HeartbeatHistogram.Children) { if (uie is Cylinder3DControl) ((Cylinder3DControl)uie).SettingPlume3DColors(cDefaultList); } p3dc.SettingPlume3DColors(cNewList); //逻辑事件处理,用来发送委托到Page层 // UXEvent((int)p3dc.Tag); UXEvent((int)p3dc.Tag); } } }
相关文章推荐
- C#--工作笔记(直方图单个UserControls)
- C#--工作笔记(时间单个控件UserControls和使用)
- 统计学 学习笔记 (四)—— 掌握数据的整体状态 直方图
- C#--工作笔记(直方图Page页)
- PHP工作笔记:数组转字符串与字符串转数组
- SEO的工作笔记
- 浏览器工作原理相关笔记
- 互感器的使用 工作笔记
- 学习工作笔记(2013-10-18)
- 工作笔记----------<一>
- Intent工作笔记,getIntent() 为空的问题
- 工作笔记2012-3-8
- 【工作笔记0006】C#调用HtmlAgilityPack类库实现网页数据抓取
- 工作学习笔记——6月
- 我的OpenCV学习笔记(14):用直方图对比完成基于内容的图像检索
- 工作笔记之32BPP
- 工作笔记之socket
- Spring Security学习笔记之整体配置
- 工作中的笔记
- 软件公司美女多,可以明显提高纯爷们的整体的工作效率