博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPFTookit Chart 高级进阶
阅读量:4622 次
发布时间:2019-06-09

本文共 6638 字,大约阅读时间需要 22 分钟。

数据源增加SeriesSource

使用方式

 

增加依赖属性

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);        }    }}

转载于:https://www.cnblogs.com/HQFZ/p/4449675.html

你可能感兴趣的文章
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>
angularjs实现首页轮播图
查看>>
Git 对象 和checkout 和stash的笔记
查看>>
团队项目总结2-服务器通信模型和顺序图
查看>>
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>
2018 leetcode
查看>>
PHP中获取当前页面的完整URL
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>