WPFTookit Chart 高级进阶

数据源增加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);
        }
    }
}
时间: 2024-08-24 14:53:51

WPFTookit Chart 高级进阶的相关文章

潭州学院-JavaVIP的Javascript的高级进阶-KeKe老师

潭州学院-JavaVIP的Javascript的高级进阶-KeKe老师 讲的不错,可以学习 下面是教程的目录截图: 下载地址:http://www.fu83.cn/thread-283-1-1.html 觉得教程比较好,可以推荐哈!

ES系统封装教程 高级进阶版 提供Wind7,xp系统下载

 ES系统封装教程 高级进阶版,提供我自己封装的Wind7 x86&x64和XP三个版本的系统下载.这个教程不是为没有基础的人准备的,要想从头学起,我推荐几个基础的教程. 1.使用 VMware Player 创建适合封装的虚拟机 2.使用 Easy Sysprep v4 封装 Windows XP 基础篇 3.使用 Easy Sysprep v4 封装 Windows 7 凡是里面用到的的工具我都会提供下载地址. 虚拟机VM ware10 .系统补丁到2014年10月.系统运行库.封装用的

Animation动画详解(六)——ValueAnimator高级进阶(二)

前言:人生总有不平时,无论何时,不后悔就好. 相关文章: 1.<Animation 动画详解(一)--alpha.scale.translate.rotate.set的xml属性及用法>2.<Animation动画详解(二)--Interpolator插值器>3.<Animation动画详解(三)-- 代码生成alpha.scale.translate.rotate.set及插值器动画>4.<Animation动画详解(四)--ValueAnimator基本使用&

6.MySQL优化---高级进阶之表的设计及优化

转自互联网整理. 优化之路高级进阶--表的设计及优化 优化①:创建规范化表,消除数据冗余 数据库范式是确保数据库结构合理,满足各种查询需要.避免数据库操作异常的数据库设计方式.满足范式要求的表,称为规范化表,范式产生于20世纪70年代初,一般表设计满足前三范式就可以,在这里简单介绍一下前三范式. 通俗的给大家解释一下(可能不是最科学.最准确的理解) 第一范式:属性(字段)的原子性约束,要求属性具有原子性,不可再分割: 第二范式:记录的惟一性约束,要求记录有惟一标识,每条记录需要有一个属性来做为实

【区块链】以太坊(Ethereum )高级进阶实战视频教程

[区块链]以太坊(Ethereum )高级进阶实战视频教程视频教程地址:http://edu.51cto.com/course/14785.html 课程大纲: 课程概要介绍使用bootnode搭建以太坊私有链web3j介绍及基本使用使用web3j管理账户default block parameter以太坊交易详解ERC20代币介绍使用web3j部署ERC20代币合约账户解锁web3j调用代币合约方法(一)web3j调用代币合约方法(二)web3j调用代币合约方法(三)深入sendTransac

SOLR的db-data-config.xml高级进阶(处理CLOB和BLOB)

SOLR的db-data-config.xml高级进阶(处理CLOB和BLOB) 我们在使用solr处理数据库时,很多时候需要处理一下数据库中的CLOB.BLOB字段,对于这两种字段如何处理,下面以代码的形式给大家演示,(不用写Java代码啊) 1)定义数据源 <dataSource name="ora" driver="oracle.jdbc.OracleDriver" url="...." /> <datasource na

Eclipse插件开发 学习笔记 PDF 第一篇到第四篇 免分下载 开发基础 核心技术 高级进阶 综合实例

<<Eclipse插件开发 学习笔记>>,本书由浅入深.有重点.有针对性地介绍了Eclipse插件开发技术,全书分为4篇共24章.第一篇介绍Eclipse平台界面开发的基础知识.包含SWT控件的使用.界面布局.事件处理等内容:第二篇是插件开发核心技术,主要介绍插件开发的核心知识要点,包含行为(Action).视图(ViewPart).编辑器(Editor).透视图(Perspective)等10章的内容.第三篇主要讲述插件开发的高级内容,包含开发高级内容.富client平台技术(R

C#可扩展编程之MEF(五):MEF高级进阶

好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用的基本已经讲完了,相信大家已经能看出MEF所带来的便利了.今天就介绍一些MEF中一些较为不常用的东西,也就是大家口中的所谓的比较高级的用法. 前面讲的导出都是在每个类上面添加Export注解,实现导出的,那么有没有一种比较简便的方法呢?答案是有的,就是在接口上面写注解,这样只要实现了这个接口的类都会

.Net高级进阶,WebApi和MVC进行模型验证的时候,教你如何自由控制需要进行验证的字段?

现在,你有一个MVC架构的web项目,你要完成一个注册功能. 前台传了3个值到你的控制器,分别是账号.密码.邮箱. 如图:现在你要在控制器里面判断,账号名称.密码.邮箱不能为空,并且名称和密码不超过16位. 上面这个图只是个理想中的小例子,实际开发情况是,可能一次性要传十几个字段甚至更多. 那么在实际开发中,通常为了复用性,我们将这3个参数用一个实体类来代替. 即如下所示. 注:这一步会有个知识点,叫做模型验证,不懂的童鞋可以百度下,MVC会通过一定规则自动直接将参数反序列化成所对应的实体类,但