wpf chart使用

1.创建wpf项目,在引用上面点右键,管理nuget程序包,选择浏览,输入框里面输入DataVisualization,下面就会显示System.Windows.Controls.DataVisualization.Toolkit,安装它

2. xaml

<Window x:Class="ChartDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        
        xmlns:local="clr-namespace:ChartDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <charting:Chart Height="271" Name="chart1" Width="379">
        <charting:Chart.Series>
            <!--PieSeries ColumnSeries  LineSeries-->
            <charting:PieSeries ItemsSource="{Binding Pair1}" 
                    DependentValuePath="Value" 
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True" />
            
        </charting:Chart.Series>
    </charting:Chart>
</Window>

3 后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ChartDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            chart1.DataContext = new ViewModel();
        }


    }

    public class ViewModel
    {
        public KeyValuePair<string, int>[] Pair1 { get; set; }
        public KeyValuePair<string, int>[] Pair2 { get; set; }

        public ViewModel()
        {
            Pair1 = new KeyValuePair<string, int>[] {

                new KeyValuePair<string, int>("Dog", 30),

                                    new KeyValuePair<string, int>("Cat", 25),

                                    new KeyValuePair<string, int>("Rat", 5),

                                    new KeyValuePair<string, int>("Hampster", 8),

                                    new KeyValuePair<string, int>("Rabbit", 12) };

            Pair2 = new KeyValuePair<string, int>[] {

                new KeyValuePair<string, int>("Dog", 50),

                                    new KeyValuePair<string, int>("Cat", 35),

                                    new KeyValuePair<string, int>("Rat", 50),

                                    new KeyValuePair<string, int>("Hampster", 80),

                                    new KeyValuePair<string, int>("Rabbit", 12) };
        }


    }
}

4效果

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
之前三此发的库http://download.csdn.net/detail/maiker/9621027可以作废,本次为最新的,其中测试了历史曲线的动态显示,这是对Microsoft WpfToolkit的更新和扩展: 1、将库版本升级到.NET 4.6.1,对命名控件进行了替换处理 2、实现了单数据Chart图的混合颜色显示,可通过修改资源字典调整和添加颜色 3、可控制各种Chart图形的数据值显示 4、坐标轴文本可倾斜显示 5、添加了StepLine图和圆环图,圆环半径比例系数可设定,同时添加了饼图的半径比系数,这样可以更好的控制饼图的标签显示,避免重叠 6、Legend可位于区域四侧,对齐方式也可以设定,以及是否显示, 可控制Chart Title是否显示 7、柱状图缝隙间隔可调整 8、全方面的加入了各种加载动画效果,动画效果可屏蔽,同时原系统自带动画效果,可叠加,这是两种不同的效果,可以互补性的单个使用 9、设计了四种主题颜色,各人可以针对图形颜色和背景色进行更好的搭配,在用户项目中,可以自己添加新的主题。 10、扩展了时间轴的应用,在新的DateTimeChart中可以通过鼠标平移和缩放时间轴,并测试了两个例子用于动态显示历史曲线,一个是外部定时更新,一个是内部定时更新 11、对LineDataPoint样式进行了设计,现在可以选择线图的点样式(如空心圆、五角星、三角形,矩形等),同时这些不同的点样式可以体现在Legend上,从而实现颜色和图形的双重区分。 总而言之,微软的控件库做得很标准,还是很好修改的。 最后一直想在历史曲线中取消点动画(及默认的透明度动画),以便提高执行效率,但微软的那部分动画不熟,总是修改失败。 可以发邮件wuyang26@live.cn讨论。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值