WPFTookit Chart 高级进阶
2024-10-14 16:21:54
数据源增加SeriesSource
使用方式
<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}" />
增加依赖属性
public enum SeriesType
{
Line,
Bar,
Column,
Scatter,
Pie
}
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);
}
}
}
最新文章
- Java程序设计之整数分解
- 170105、MySQL 性能优化的最佳 20+ 条经验
- 从输入 URL 到浏览器接收的过程中发生了什么事情
- php操作mysql数据库
- 深入理解Java:注解(Annotation)基本概念
- add user
- Storm系列之二——Guaranteeing Message Processing
- python 练习 2
- STL 速解
- JavaScript高级程序设计43.pdf
- 【转】C++读写二进制文件
- 关于code reivew
- Python笔记4-20151029
- 从零开始理解JAVA事件处理机制(3)
- HTML (Hyper Text Markup Language) 常用标签
- JavaScript定时器:setTimeout()和setInterval()
- SmartUpload文件上传组件的使用教程
- SSM框架整合搭建教程
- Entity Framework学习 - 5.DB First执行时提示model没有key
- 使用外网访问阿里云服务器ZooKeeper