silverlight Toolkit绘制图表

姚烨
2023-12-01

1)前台代码

<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="SilverlightApplication106.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">


    <Grid x:Name="LayoutRoot" Background="White">
        <toolkit:Chart Name="TestChart" Title="学生铅数笔统计表"></toolkit:Chart>
    </Grid>
</UserControl>

2)后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;




namespace SilverlightApplication106
{
    public partial class MainPage : UserControl
    {
        private List<Student> Students = null;
        public MainPage()
        {
            InitializeComponent();
            //菜单
            ContextMenu TestContextMenu = new ContextMenu();
            //柱状图
            MenuItem ColumnMenuItem = new MenuItem();
            ColumnMenuItem.Header = "柱状图";
            ColumnMenuItem.Click += new RoutedEventHandler(ColumnMenuItem_Click);
            TestContextMenu.Items.Add(ColumnMenuItem);
            //饼状图
            MenuItem PieMenuItem = new MenuItem();
            PieMenuItem.Header = "饼状图";
            PieMenuItem.Click += new RoutedEventHandler(PieMenuItem_Click);
            TestContextMenu.Items.Add(PieMenuItem);
            //添加菜单
            ContextMenuService.SetContextMenu(TestChart, TestContextMenu);
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }


        void PieMenuItem_Click(object sender, RoutedEventArgs e)
        {
            BindPieSeries();
        }
        void ColumnMenuItem_Click(object sender, RoutedEventArgs e)
        {
            BindColumnSeries();
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Students = GetStudents();
            BindColumnSeries();
        }
        /// <summary>
        /// 绑定饼状图
        /// </summary>
        private void BindPieSeries()
        {
            PieSeries TestPieSeries = new PieSeries();
            TestPieSeries.ItemsSource = Students;
            TestPieSeries.DependentValueBinding = new Binding("PenNumber");
            TestPieSeries.IndependentValueBinding = new Binding("StudentName");
            TestPieSeries.Title = "铅笔数";
            TestChart.Series.Clear();
            TestChart.Series.Add(TestPieSeries);
        }
        /// <summary>
        /// 绑定柱状图
        /// </summary>
        private void BindColumnSeries()
        {
            ColumnSeries TestColumnSeries = new ColumnSeries();
            TestColumnSeries.ItemsSource = Students;
            TestColumnSeries.DependentValueBinding = new Binding("PenNumber");
            TestColumnSeries.IndependentValueBinding = new Binding("StudentName");
            TestColumnSeries.Title = "铅笔数";
            TestChart.Series.Clear();
            TestChart.Series.Add(TestColumnSeries);
        }
        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <returns></returns>
        private List<Student> GetStudents()
        {
            List<Student> items = new List<Student>();
            items.Add(new Student(1, "张三", 1));
            items.Add(new Student(2, "李四", 2));
            items.Add(new Student(3, "王五", 3));
            items.Add(new Student(4, "马六", 4));
            return items;
        }
    }
    public class Student
    {
        public Student() { }
        public Student(int _studentID, string _studentName, int _penNumber)
        {
            this.StudentID = _studentID;
            this.StudentName = _studentName;
            this.PenNumber = _penNumber;
        }
        private int studentID;


        public int StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }
        private string studentName;


        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        private int penNumber;


        public int PenNumber
        {
            get { return penNumber; }
            set { penNumber = value; }
        }
    }
}


 类似资料: