当前位置:   article > 正文

WPF柱状图(支持数据库动态更新)

columnseries在wpf中的意识

  之前我们讲到wpf组件基类以及组件开发,现在我们围绕之前的内容去开发一个组件。

效果图请加群查看,在群共享里面。

 

 做出这个呢  是比较繁琐的。

首先要使用我们的基类

继承基类的模板自动生成如下几个文件:HrvColumnSeries这四个文件   HrvColumnSeries.cs是去设置组件的名称和组件在列表上显示的图片,我们所做的组件不是在vs上使用,而是在我们自己开发的设计器上,所以需要这些。

代码如下:

  1. public class HrvColumnSeries : ComponentBase
  2. {
  3. public HrvColumnSeries()
  4. {
  5. Content = new HrvColumnSeriesShape();
  6. }
  7. public override string TextName
  8. {
  9. get { return "HrvColumnSeries"; }
  10. }
  11. public override string IconName
  12. {
  13. get { return "HrvColumnSeries.png"; }
  14. }
  15. }

  HrvColumnSeriesEvent.cs 文件    组件在使用时有相应的事件  比如一个button也有click事件 这是我们的事件  一下为例子:

  1. public class HrvColumnSeriesEvent : ComponentEventBase
  2. {
  3. private string _Click;
  4. [Category("基础事件")]
  5. [Description("单击事件.")]
  6. [EventInfo("Click")]
  7. public string Click
  8. {
  9. get { return _Click; }
  10. set
  11. {
  12. if (_Click == value) return;
  13. _Click = value;
  14. OnPropertyChanged("Click");
  15. }
  16. }
  17. public void OnClick(object sender, RoutedEventArgs e)
  18. {
  19. if (Click != null)
  20. {
  21. }
  22. }
  23. }

  毫无疑问HrvColumnSeriesProperty.cs 这个就是我们的组件属性了

因为这一章不只是组件  还有数据库数据更新   前台动态更新。

所以属性就比较多如下:

  1. public class HrvColumnSeriesProperty : ComponentPropertyBase
  2. {
  3. private static string _ComputerName = ".\\sqlexpress";
  4. [Category("数据源")]
  5. [Description("计算机名称")]
  6. public string ComputerName
  7. {
  8. get { return _ComputerName; }
  9. set
  10. {
  11. _ComputerName = value;
  12. OnPropertyChanged("ComputerName");
  13. }
  14. }
  15. private string _DataBaseName = "ColumnSeriesDB";
  16. [Category("数据源")]
  17. [Description("数据库名称")]
  18. public string DataBaseName
  19. {
  20. get { return _DataBaseName; }
  21. set
  22. {
  23. _DataBaseName = value;
  24. OnPropertyChanged("DataBaseName");
  25. }
  26. }
  27. private string _UID = "sa";
  28. [Category("数据源")]
  29. [Description("用户名")]
  30. public string UID
  31. {
  32. get { return _UID; }
  33. set
  34. {
  35. _UID = value;
  36. OnPropertyChanged("UID");
  37. }
  38. }
  39. private string _PWD = "sa";
  40. [Category("数据源")]
  41. [Description("密码")]
  42. public string PWD
  43. {
  44. get { return _PWD; }
  45. set
  46. {
  47. _PWD = value;
  48. OnPropertyChanged("PWD");
  49. }
  50. }
  51. private bool _IsWindowLogin = true;
  52. [Category("数据源")]
  53. [Description("是否为Window身份验证")]
  54. public bool IsWindowLogin
  55. {
  56. get { return _IsWindowLogin; }
  57. set
  58. {
  59. _IsWindowLogin = value;
  60. OnPropertyChanged("IsWindowLogin");
  61. }
  62. }
  63. private string _XTableName = "ColumnSeriesTable";
  64. [Category("数据源")]
  65. [Description("X轴表名")]
  66. public string XTableName
  67. {
  68. get { return _XTableName; }
  69. set
  70. {
  71. _XTableName = value;
  72. OnPropertyChanged("XTableName");
  73. }
  74. }
  75. private string _YTableName = "ColumnSeriesData";
  76. [Category("数据源")]
  77. [Description("Y轴表名")]
  78. public string YTableName
  79. {
  80. get { return _YTableName; }
  81. set
  82. {
  83. _YTableName = value;
  84. OnPropertyChanged("YTableName");
  85. }
  86. }
  87. private string _XTableID = "ColumnSData";
  88. [Category("数据源")]
  89. [Description("X轴表ID")]
  90. public string XTableID
  91. {
  92. get { return _XTableID; }
  93. set
  94. {
  95. _XTableID = value;
  96. OnPropertyChanged("XTableID");
  97. }
  98. }
  99. private string _YTableID = "ID";
  100. [Category("数据源")]
  101. [Description("Y轴表ID")]
  102. public string YTableID
  103. {
  104. get { return _YTableID; }
  105. set
  106. {
  107. _YTableID = value;
  108. OnPropertyChanged("YTableID");
  109. }
  110. }
  111. private string _Title = "柱状图";
  112. [Category("基本")]
  113. [Description("标题")]
  114. public string Title
  115. {
  116. get { return _Title; }
  117. set
  118. {
  119. _Title = value;
  120. OnPropertyChanged("Title");
  121. }
  122. }
  123. private string _DataColumnName = "MinData;MaxData";
  124. [Category("数据源")]
  125. [Description("数据源列名,多数据源名称用分号隔开")]
  126. public string DataColumnName
  127. {
  128. get { return _DataColumnName; }
  129. set
  130. {
  131. _DataColumnName = value;
  132. OnPropertyChanged("DataColumnName");
  133. }
  134. }
  135. private string _XAxisName = "sad ada";
  136. [Category("数据源")]
  137. [Description("轴线名称")]
  138. public string XAxisName
  139. {
  140. get { return _XAxisName; }
  141. set
  142. {
  143. _XAxisName = value;
  144. OnPropertyChanged("XAxisName");
  145. }
  146. }
  147. private string _ToolTipName = "asdsa($):";
  148. [Category("数据源")]
  149. [Description("轴线提示文本")]
  150. public string ToolTipName
  151. {
  152. get { return _ToolTipName + "{field}"; }
  153. set
  154. {
  155. _ToolTipName = value;
  156. OnPropertyChanged("ToolTipName");
  157. }
  158. }
  159. private bool _ShowValueOnBar=true ;
  160. [Category("数据源")]
  161. [Description("是否显示数据源标注")]
  162. public bool ShowValueOnBar
  163. {
  164. get { return _ShowValueOnBar; }
  165. set
  166. {
  167. _ShowValueOnBar = value;
  168. OnPropertyChanged("ShowValueOnBar");
  169. }
  170. }
  171. //读取保存的数据
  172. public override void ReadXml(System.Xml.Linq.XElement element)
  173. {
  174. base.ReadXml(element);
  175. ShowValueOnBar = element.ReadBool("ShowValueOnBar", ShowValueOnBar);
  176. ToolTipName = element.ReadString("ToolTipName");
  177. XAxisName = element.ReadString("XAxisName");
  178. DataColumnName = element.ReadString("DataColumnName");
  179. Title = element.ReadString("Title");
  180. YTableID = element.ReadString("YTableID");
  181. XTableID = element.ReadString("XTableID");
  182. YTableName = element.ReadString("YTableName");
  183. XTableName = element.ReadString("XTableName");
  184. IsWindowLogin = element.ReadBool("IsWindowLogin", IsWindowLogin);
  185. PWD = element.ReadString("PWD");
  186. UID = element.ReadString("UID");
  187. DataBaseName = element.ReadString("DataBaseName");
  188. ComputerName = element.ReadString("ComputerName");
  189. }
  190. //保存属性
  191. public override void WriteXml(System.Xml.XmlWriter writer)
  192. {
  193. base.WriteXml(writer);
  194. writer.WriteAttributeString("ShowValueOnBar", ShowValueOnBar.ToString());
  195. writer.WriteAttributeString("ToolTipName", ToolTipName);
  196. writer.WriteAttributeString("XAxisName", XAxisName);
  197. writer.WriteAttributeString("DataColumnName", DataColumnName);
  198. writer.WriteAttributeString("Title", Title);
  199. writer.WriteAttributeString("YTableID", YTableID);
  200. writer.WriteAttributeString("XTableID", XTableID);
  201. writer.WriteAttributeString("YTableName", YTableName);
  202. writer.WriteAttributeString("XTableName", XTableName);
  203. writer.WriteAttributeString("IsWindowLogin", IsWindowLogin.ToString());
  204. writer.WriteAttributeString("PWD", PWD);
  205. writer.WriteAttributeString("UID", UID);
  206. writer.WriteAttributeString("DataBaseName", DataBaseName);
  207. writer.WriteAttributeString("ComputerName", ComputerName);
  208. }
  209. }

  在这里我们使用的是固定的数据格式     包括数据库的表也是固定的。

至于这些属性呢    数据库名称什么的我们可以自己去设置  唯有格式是固定的,下面的保存和读取是当组件放在设计器上的时候有编辑完的东西需要保存,当保存了是设计器上的东西时  当然也需要保存组件的属性了,这个保存的方法在基类。

至于HrvColumnSeriesShape.cs  我们用作将组件的属性以及事件去绑定起来(双向绑定)    可以保证属性的动态变化

  1. public class HrvColumnSeriesShape : HrvContent
  2. {
  3. public HrvColumnSeriesProperty _Property;
  4. public HrvColumnSeriesEvent _Event;
  5. public HrvColumnSeriesShape()
  6. {
  7. this.Content = new ColumnSeries();
  8. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.ComputerNameProperty, new Binding("ComputerName") { Source = this.Property, Mode = BindingMode.TwoWay });
  9. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.DataBaseNameProperty, new Binding("DataBaseName") { Source = this.Property, Mode = BindingMode.TwoWay });
  10. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.UIDProperty, new Binding("UID") { Source = this.Property, Mode = BindingMode.TwoWay });
  11. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.PWDProperty, new Binding("PWD") { Source = this.Property, Mode = BindingMode.TwoWay });
  12. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.IsWindowLoginProperty, new Binding("IsWindowLogin") { Source = this.Property, Mode = BindingMode.TwoWay });
  13. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.XTableNameProperty, new Binding("XTableName") { Source = this.Property, Mode = BindingMode.TwoWay });
  14. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.YTableNameProperty, new Binding("YTableName") { Source = this.Property, Mode = BindingMode.TwoWay });
  15. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.TitleProperty, new Binding("Title") { Source = this.Property, Mode = BindingMode.TwoWay });
  16. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.DataColumnNameProperty, new Binding("DataColumnName") { Source = this.Property, Mode = BindingMode.TwoWay });
  17. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.XAxisNameProperty, new Binding("XAxisName") { Source = this.Property, Mode = BindingMode.TwoWay });
  18. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.ToolTipNameProperty, new Binding("ToolTipName") { Source = this.Property, Mode = BindingMode.TwoWay });
  19. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.ShowValueOnBarProperty, new Binding("ShowValueOnBar") { Source = this.Property, Mode = BindingMode.TwoWay });
  20. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.XTableIDProperty, new Binding("XTableID") { Source = this.Property, Mode = BindingMode.TwoWay });
  21. BindingOperations.SetBinding(this.Content as ColumnSeries, ColumnSeries.YTableIDProperty, new Binding("YTableID") { Source = this.Property, Mode = BindingMode.TwoWay });
  22. }
  23. public override ComponentPropertyBase Property
  24. {
  25. get
  26. {
  27. if (_Property == null)
  28. {
  29. _Property = new HrvColumnSeriesProperty();
  30. }
  31. return _Property;
  32. }
  33. }
  34. public override ComponentEventBase Event
  35. {
  36. get
  37. {
  38. if (_Event == null)
  39. {
  40. _Event = new HrvColumnSeriesEvent();
  41. }
  42. return _Event;
  43. }
  44. }

  如此我们的组件基本的构成就讲完了,接下来就是数据的读取以及前端的动态更新了!

ColumnSeries.xaml.cs属性如下:

  1. #region 属性
  2. public static DependencyProperty ComputerNameProperty = DependencyProperty.Register("ComputerName", typeof(string), typeof(ColumnSeries));
  3. public string ComputerName
  4. {
  5. get { return (string)GetValue(ComputerNameProperty); }
  6. set { SetValue(ComputerNameProperty, value); }
  7. }
  8. public static DependencyProperty DataBaseNameProperty = DependencyProperty.Register("DataBaseName", typeof(string), typeof(ColumnSeries));
  9. public string DataBaseName
  10. {
  11. get { return (string)GetValue(DataBaseNameProperty); }
  12. set { SetValue(DataBaseNameProperty, value); }
  13. }
  14. public static DependencyProperty UIDProperty = DependencyProperty.Register("UID", typeof(string), typeof(ColumnSeries));
  15. public string UID
  16. {
  17. get { return (string)GetValue(UIDProperty); }
  18. set { SetValue(UIDProperty, value); }
  19. }
  20. public static DependencyProperty PWDProperty = DependencyProperty.Register("PWD", typeof(string), typeof(ColumnSeries));
  21. public string PWD
  22. {
  23. get { return (string)GetValue(PWDProperty); }
  24. set { SetValue(PWDProperty, value); }
  25. }
  26. public static DependencyProperty IsWindowLoginProperty = DependencyProperty.Register("IsWindowLogin", typeof(bool), typeof(ColumnSeries));
  27. public bool IsWindowLogin
  28. {
  29. get { return Convert.ToBoolean(GetValue(IsWindowLoginProperty)); }
  30. set { SetValue(IsWindowLoginProperty, value); }
  31. }
  32. public static DependencyProperty XTableNameProperty = DependencyProperty.Register("XTableName", typeof(string), typeof(ColumnSeries));
  33. public string XTableName
  34. {
  35. get { return (string)GetValue(XTableNameProperty); }
  36. set { SetValue(XTableNameProperty, value); }
  37. }
  38. public static DependencyProperty YTableNameProperty = DependencyProperty.Register("YTableName", typeof(string), typeof(ColumnSeries));
  39. public string YTableName
  40. {
  41. get { return (string)GetValue(YTableNameProperty); }
  42. set { SetValue(YTableNameProperty, value); }
  43. }
  44. public static DependencyProperty XTableIDProperty = DependencyProperty.Register("XTableID", typeof(string), typeof(ColumnSeries));
  45. public string XTableID
  46. {
  47. get { return (string)GetValue(XTableIDProperty); }
  48. set { SetValue(XTableIDProperty, value); }
  49. }
  50. public static DependencyProperty YTableIDProperty = DependencyProperty.Register("YTableID", typeof(string), typeof(ColumnSeries));
  51. public string YTableID
  52. {
  53. get { return (string)GetValue(YTableIDProperty); }
  54. set { SetValue(YTableIDProperty, value); }
  55. }
  56. public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ColumnSeries));
  57. public string Title
  58. {
  59. get { return (string)GetValue(TitleProperty); }
  60. set { SetValue(TitleProperty, value); }
  61. }
  62. public static DependencyProperty DataColumnNameProperty = DependencyProperty.Register("DataColumnName", typeof(string), typeof(ColumnSeries));
  63. public string DataColumnName
  64. {
  65. get { return (string)GetValue(DataColumnNameProperty); }
  66. set { SetValue(DataColumnNameProperty, value); }
  67. }
  68. public static DependencyProperty XAxisNameProperty = DependencyProperty.Register("XAxisName", typeof(string), typeof(ColumnSeries));
  69. public string XAxisName
  70. {
  71. get { return (string)GetValue(XAxisNameProperty); }
  72. set { SetValue(XAxisNameProperty, value); }
  73. }
  74. public static DependencyProperty ToolTipNameProperty = DependencyProperty.Register("ToolTipName", typeof(string), typeof(ColumnSeries));
  75. public string ToolTipName
  76. {
  77. get { return (string)GetValue(ToolTipNameProperty); }
  78. set { SetValue(ToolTipNameProperty, value); }
  79. }
  80. public static DependencyProperty ShowValueOnBarProperty = DependencyProperty.Register("ShowValueOnBar", typeof(bool), typeof(ColumnSeries));
  81. public bool ShowValueOnBar
  82. {
  83. get { return Convert.ToBoolean(GetValue(ShowValueOnBarProperty)); }
  84. set { SetValue(ShowValueOnBarProperty, value); }
  85. }
  86. private DataSet _Data;
  87. public DataSet Data
  88. {
  89. get { return _Data; }
  90. set { _Data = value; }
  91. }
  92. #endregion

  为什么定义这种呢?  这是为了将组件的属性和数据读取这一块绑定   为了保证在绑定之后属性的动态变化,所以我们使用了依赖项属性。

关于监听数据库值得改变去动态显示数据(SqlDependency)这部分内容在

WPF非轮询方式更新数据库变化SqlDependency(数据库修改前台自动更新)

这一章节会有详细的讲解,以下内容  看的懂看一下   觉得繁琐的   就不看了。

数据的读取:

  1. private string con;
  2. private string[] dataColumn;
  3. private string sql;
  4. #region 数据库连接
  5. public void GetDataSet()
  6. {
  7. SqlConnection connection = new SqlConnection(con);
  8. Data = new DataSet();
  9. SqlCommand command = new SqlCommand(sql, connection);
  10. command.CommandType = CommandType.Text;
  11. connection.Open();
  12. SqlDependency dependency = new SqlDependency(command);
  13. dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
  14. DataTable dt = new DataTable();
  15. SqlDataReader sda = command.ExecuteReader();
  16. foreach (string data in dataColumn)
  17. {
  18. DataColumn dc = new DataColumn(data);
  19. dt.Columns.Add(dc);
  20. }
  21. while (sda.Read())
  22. {
  23. DataRow row = dt.NewRow();
  24. foreach (string data in dataColumn)
  25. {
  26. row[data] = sda[data].ToString();
  27. }
  28. dt.Rows.Add(row);
  29. }
  30. Data.Tables.Add(dt);
  31. sda.Close();
  32. //}
  33. //catch (Exception ex)
  34. //{
  35. //}
  36. }
  37. private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
  38. {
  39. DrawChart();
  40. }
  41. #endregion
  42. string CName;
  43. string DName;
  44. string ID;
  45. string PassWord;
  46. bool windowLogin;
  47. string XTName;
  48. string YTName;
  49. string DCName;
  50. string XAName;
  51. string XTID;
  52. string YTID;
  53. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  54. {
  55. base.OnPropertyChanged(e);
  56. if (CName != null && DName != null && ID != null && PassWord != null && XTName != null && YTName != null && DCName != null && XAName != null && XTID != null && YTID != null)
  57. if (CName != ComputerName || DName != DataBaseName || ID != UID || PassWord != PWD || windowLogin != IsWindowLogin || XTName != XTableName || YTName != YTableName || DCName != DataColumnName || XAName != XAxisName || XTID != XTableID || YTID != YTableID)
  58. DrawChart();
  59. CName = ComputerName;
  60. DName = DataBaseName;
  61. ID = UID;
  62. PassWord = PWD;
  63. windowLogin = IsWindowLogin;
  64. XTName = XTableName;
  65. YTName = YTableName;
  66. DCName = DataColumnName;
  67. XAName = XAxisName;
  68. XTID = XTableID;
  69. YTID = YTableID;
  70. }
  71. #region 显示数据
  72. private void DrawChart()
  73. {
  74. this.Dispatcher.Invoke(new System.Action(() =>
  75. {
  76. con = @"server=" + ComputerName + @";database=" + DataBaseName + ";uid=" + UID + ";pwd=" + PWD + ";Integrated Security=" + IsWindowLogin;
  77. dataColumn = (DataColumnName + ";Name").Split(';');
  78. sql = "SELECT ";
  79. foreach (string data in dataColumn)
  80. {
  81. sql += data + ",";
  82. }
  83. sql = sql.Remove(sql.Length - 1);
  84. sql += " FROM [dbo].[" + XTableName + "] INNER JOIN [dbo].[" + YTableName + "] ON " + YTableName + "." + YTableID + "=" + XTableName + "." + XTableID;
  85. try
  86. {
  87. SqlDependency.Start(con);
  88. GetDataSet();
  89. }
  90. catch(Exception ex)
  91. {
  92. MessageBox.Show(ex.Message.ToString(),"数据连接错误!");
  93. }
  94. chart2.Reset();
  95. Binding titleBind = new Binding { Source = this, Path = new PropertyPath("Title") };
  96. BindingOperations.SetBinding(this.chart2, BarChart.TitleProperty, titleBind);
  97. string[] column = new string[dataColumn.Length - 1];
  98. for (int i = 0; i < dataColumn.Length - 1; i++)
  99. {
  100. column[i] = dataColumn[i];
  101. }
  102. if (column == null)
  103. return;
  104. foreach (string data in column)
  105. {
  106. chart2.ValueField.Add(data);
  107. }
  108. Binding ToolTipNameBind = new Binding { Source = this, Path = new PropertyPath("ToolTipName") };
  109. BindingOperations.SetBinding(this.chart2, BarChart.ToolTipTextProperty, ToolTipNameBind);
  110. chart2.XAxisText = XAxisName;
  111. chart2.XAxisField = "Name";
  112. Binding showBarBind = new Binding { Source = this, Path = new PropertyPath("ShowValueOnBar") };
  113. BindingOperations.SetBinding(this.chart2, BarChart.ShowValueOnBarsProperty, showBarBind);
  114. if (Data == null)
  115. return;
  116. Binding DataSourceBind = new Binding { Source = this, Path = new PropertyPath("Data") };
  117. BindingOperations.SetBinding(this.chart2, BarChart.DataSourceProperty, DataSourceBind);
  118. chart2.Generate();
  119. }));
  120. }
  121. #endregion
  122. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  123. {
  124. DrawChart();
  125. }
  126. private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  127. {
  128. chart2.Width = 450;
  129. chart2.Height = 300;
  130. }

  

OnPropertyChanged解释下这个   这个是在属性更改的时候发生,因为数据库的连接字符串是无法做动态更新的   比如更改完数据库名称之后马上要自动去连接那个数据库之前的方法就无法实现了,所以我们使用了这个事件    当数据库名称、表名、列明等发生改变的时候去重新获取数据。目前就讲这些,至于柱状图下章继续,柱状图更加繁琐。需要组件基类模板的,想要柱状图源码的请加群:

WPF、AE技术交流群:94234450   

点击加入QQ群:

不管你遇到了什么问题,我们都不会让你独自去面对!

转载于:https://www.cnblogs.com/BeiJing-Net-DaiDai/p/3449359.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/270756
推荐阅读
相关标签
  

闽ICP备14008679号