WPFTookit Chart 高级进阶
数据源增加SeriesSource
使用方式
1 2 3 4 5 6 | < Charting:Chart x:Name="chart" Helper:ChartHelper.DependentValueBinding="Value" Helper:ChartHelper.IndependentValueBinding="Key" Helper:ChartHelper.Title="TitlePropertyOnCollection" Helper:ChartHelper.SeriesType="Line" Helper:ChartHelper.SeriesSource="{Binding Path=MyCollectionofCollections}" /> |
1 | |
增加依赖属性
1 2 3 4 5 6 7 8 | public enum SeriesType { Line, Bar, Column, Scatter, Pie } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | public class ChartHelper { #region SeriesSource public static readonly DependencyProperty SeriesSourceProperty = DependencyProperty.RegisterAttached( "SeriesSource" , typeof (IEnumerable), typeof (ChartHelper), new PropertyMetadata(SeriesSourceChanged)); public static IEnumerable GetSeriesSource(DependencyObject d) { return (IEnumerable)d.GetValue(SeriesSourceProperty); } public static void SetSeriesSource(DependencyObject d, IEnumerable value) { d.SetValue(SeriesSourceProperty, value); } #endregion #region DependentValueBinding public static readonly DependencyProperty DependentValueBindingProperty = DependencyProperty.RegisterAttached( "DependentValueBinding" , typeof ( string ), typeof (ChartHelper), null ); public static string GetDependentValueBinding(DependencyObject d) { return ( string )d.GetValue(DependentValueBindingProperty); } public static void SetDependentValueBinding(DependencyObject d, string value) { d.SetValue(DependentValueBindingProperty, value); } #endregion #region IndependentValueBinding public static readonly DependencyProperty IndependentValueBindingProperty = DependencyProperty.RegisterAttached( "IndependentValueBinding" , typeof ( string ), typeof (ChartHelper), null ); public static string GetIndependentValueBinding(DependencyObject d) { return ( string )d.GetValue(IndependentValueBindingProperty); } public static void SetIndependentValueBinding(DependencyObject d, string value) { d.SetValue(IndependentValueBindingProperty, value); } #endregion #region Title public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached( "Title" , typeof ( string ), typeof (ChartHelper), null ); public static string GetTitle(DependencyObject d) { return ( string )d.GetValue(TitleProperty); } public static void SetTitle(DependencyObject d, string value) { d.SetValue(TitleProperty, value); } #endregion #region SeriesType public static readonly DependencyProperty SeriesTypeProperty = DependencyProperty.RegisterAttached( "SeriesType" , typeof (SeriesType), typeof (ChartHelper), new PropertyMetadata(SeriesType.Bar)); public static SeriesType GetSeriesType(DependencyObject d) { return (SeriesType)d.GetValue(SeriesTypeProperty); } public static void SetSeriesType(DependencyObject d, SeriesType value) { d.SetValue(SeriesTypeProperty, value); } #endregion #region SeriesStyle public static readonly DependencyProperty SeriesStyleProperty = DependencyProperty.RegisterAttached( "SeriesStyle" , typeof (Style), typeof (ChartHelper), null ); public static Style GetSeriesStyle(DependencyObject d) { return (Style)d.GetValue(SeriesStyleProperty); } public static void SetSeriesStyle(DependencyObject d, Style value) { d.SetValue(SeriesStyleProperty, value); } #endregion private static void SeriesSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is Chart)) { throw new Exception( "Series attached property only works on a Chart type" ); } var chart = d as Chart; /* Clear out any old series in the chart */ chart.Series.Clear(); /* Get our collection of data we need for each series */ var chartSeriesSource = e.NewValue as IEnumerable; if (chartSeriesSource == null ) throw new Exception( "The SeriesSource does not support IEnumerable" ); /* Loop over each collection of data */ foreach ( var dataSource in chartSeriesSource) { DynamicSeries series; /* Find out what type of series we want to use */ var seriesType = GetSeriesType(chart); switch (seriesType) { case SeriesType.Line: series = new LineSeries(); break ; case SeriesType.Bar: series = new BarSeries(); break ; case SeriesType.Column: series = new ColumnSeries(); break ; case SeriesType.Pie: series = new PieSeries(); break ; case SeriesType.Scatter: series = new ScatterSeries(); break ; default : throw new ArgumentOutOfRangeException(); } /* Get and set the style of the newly created series */ var seriesStyle = GetSeriesStyle(chart); series.Style = seriesStyle; string titleBindingName = GetTitle(chart); if (! string .IsNullOrEmpty(titleBindingName)) { /* Do some binding of the Title property */ var titleBinding = new Binding(titleBindingName) { Source = series.Title, Mode = BindingMode.TwoWay }; series.SetBinding(Series.TitleProperty, titleBinding); } /* Setup the bindings configured in the attached properties */ series.DependentValueBinding = new Binding(GetDependentValueBinding(chart)); series.IndependentValueBinding = new Binding(GetIndependentValueBinding(chart)); /*Set the ItemsSource property, which gives the data to the series to be rendered */ series.ItemsSource = dataSource as IEnumerable; /* Add the series to the chart */ chart.Series.Add(series); } } } |
作者:旭东
出处:http://www.cnblogs.com/HQFZ
关于作者:专注于微软平台项目架构、管理和企业解决方案。现主要从事WinForm、ASP.NET、WPF、WCF、等方面的项目开发、架构、管理。如有问题或建议,请不吝指教!
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以联系我,非常感谢。
如果您该文觉得不错或者对你有帮助,请点下推荐,让更多的朋友看到,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?