一个图表的例子

<UserControl x:Class="SilverlightExcel.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="490" Height="298">
    <Grid x:Name="LayoutRoot" Background="White">
       
        <!-- Exported with Paste2Xaml and Ab2d.ReaderWmf. See http://www.wpf-graphics.com/ for more WPF tools. -->
        <Canvas>
            <Polygon Points="5,5 5,293 485,293 485,5 5,5" Fill="#FFFFFFFF"/>
            <Polygon Points="87,59 87,251 470,251 470,59 87,59" Fill="#FFFFFFFF"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M87,212L470,212 470,213 87,213 87,212z M87,174L470,174 470,175 87,175 87,174z M87,135L470,135 470,136 87,136 87,135z M87,97L470,97 470,98 87,98 87,97z M87,59L470,59 470,60 87,60 87,59"/>
            <Canvas Name="BarsCanvas">
                <Canvas.RenderTransform>
                    <ScaleTransform x:Name="BarsCanvasScale" ScaleY="0" CenterY="251"/>
                </Canvas.RenderTransform>
                <Canvas.Triggers>
                    <EventTrigger RoutedEvent="Canvas.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation From="0" To="1" Duration="0:0:1" Storyboard.TargetName="BarsCanvasScale" Storyboard.TargetProperty="ScaleY" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Canvas.Triggers>

                <Polygon Name="bar2003" Points="110,200 110,251 141,251 141,200 110,200" Fill="#FF4F81BD"/>
                <Polygon Name="bar2004" Points="187,190 187,251 218,251 218,190 187,190" Fill="#FFC0504D"/>
                <Polygon Name="bar2005" Points="263,186 263,251 294,251 294,186 263,186" Fill="#FF9BBB59"/>
                <Polygon Name="bar2006" Points="340,162 340,251 371,251 371,162 340,162" Fill="#FF8064A2"/>
                <Polygon Name="bar2007" Points="417,90 417,251 447,251 447,90 417,90" Fill="#FF4BACC6"/>
            </Canvas>
            <Polygon Points="87,250 87,59 88,59 88,250" Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M83,250L87,250 87,251 83,251 83,250z M83,212L87,212 87,213 83,213 83,212z M83,174L87,174 87,175 83,175 83,174z M83,135L87,135 87,136 83,136 83,135z M83,97L87,97 87,98 83,98 83,97z M83,59L87,59 87,60 83,60 83,59"/>
            <Polygon Points="87,250 470,250 470,251 87,251" Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="M88,250L88,254 87,254 87,250 88,250z M165,250L165,254 164,254 164,250 165,250z M241,250L241,254 240,254 240,250 241,250z M318,250L318,254 317,254 317,250 318,250z M394,250L394,254 393,254 393,250 394,250z M471,250L471,254 470,254 470,250 471,250"/>
            <TextBlock Text="$0" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="61" Canvas.Top="243"/>
            <TextBlock Text="$50.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="28" Canvas.Top="205"/>
            <TextBlock Text="$100.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="166"/>
            <TextBlock Text="$150.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="128"/>
            <TextBlock Text="$200.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="90"/>
            <TextBlock Text="$250.000" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="20" Canvas.Top="51"/>
            <TextBlock Text="2003" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="111" Canvas.Top="263"/>
            <TextBlock Text="2004" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="188" Canvas.Top="263"/>
            <TextBlock Text="2005" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="265" Canvas.Top="263"/>
            <TextBlock Text="2006" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="341" Canvas.Top="263"/>
            <TextBlock Text="2007" FontFamily="Arial" FontSize="13" Foreground="#FF000000" Canvas.Left="418" Canvas.Top="263"/>
            <TextBlock Text="Net Profit" FontFamily="Arial" FontWeight="Bold" FontSize="24" Foreground="#FF000000" Canvas.Left="191" Canvas.Top="15"/>
            <Path Fill="#FF868686" Stroke="#FF868686" StrokeThickness="1" Data="F1M5,5C5,5,5,5,5,5L485,5C485,5,486,5,486,5L486,293C486,293,485,294,485,294L5,294C5,294,5,293,5,293z M6,293L5,293 485,293 485,293 485,5 485,6 5,6 6,5z"/>

            <TextBlock Name="ValueTextBlock" Visibility="Collapsed" Text="$99.999,00" FontFamily="Arial" FontSize="13" FontWeight="Bold" Foreground="#FF000000"/>
            <!--<Grid Name="ValueCanvas" Visibility="Visible" Width="90" Height="25">
                <Rectangle RadiusX="3" RadiusY="3" Fill="#DDFFFFC0"/>
                <TextBlock Name="ValueTextBlock" Text="$999.999,00" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Arial" FontSize="13" FontWeight="Bold" Foreground="#FF000000"/>
            </Grid>-->
        </Canvas>
       
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
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;

namespace SilverlightExcel
{
    public partial class Page : UserControl
    {
private const int START_YEAR = 2003;
private const int END_YEAR = 2007;

private Polygon _selectedPolygon;

private Dictionary<int, int> _yearProfits;

public Page()
{
    InitializeComponent();

    FillYearProfits();
    RegisterBarEvents();
}

private void FillYearProfits()
{
    _yearProfits = new Dictionary<int, int>();

    _yearProfits.Add(2003, 66000);
    _yearProfits.Add(2004, 79000);
    _yearProfits.Add(2005, 85000);
    _yearProfits.Add(2006, 116000);
    _yearProfits.Add(2007, 210000);
}

private int GetProfitForBar(string barName)
{
    int year;
    int profit;

    try
    {
        year = Int32.Parse(barName.Substring(3));

        profit = _yearProfits[year];
    }
    catch
    {
        profit = 0;
    }

    return profit;
}

private void RegisterBarEvents()
{
    for (int year = START_YEAR; year <= END_YEAR; year++)
    {
        Polygon oneBar = this.FindName("bar" + year.ToString()) as Polygon;

        if (oneBar != null)
        {
            oneBar.MouseEnter += new MouseEventHandler(oneBar_MouseEnter);
            oneBar.MouseLeave += new MouseEventHandler(oneBar_MouseLeave);
        }
    }
}

void oneBar_MouseEnter(object sender, MouseEventArgs e)
{
    Polygon currentBar = sender as Polygon;

    if (currentBar != null)
    {
        currentBar.StrokeThickness = 1;
        currentBar.Stroke = new SolidColorBrush(Colors.Black);

        _selectedPolygon = currentBar;

        ValueTextBlock.Text = string.Format("${0:#,000.00}",
                              GetProfitForBar(currentBar.Name));
        ValueTextBlock.Measure(new Size(double.PositiveInfinity,
                                        double.PositiveInfinity));

        Rect polygonBounds = GetPolygonBounds(currentBar.Points);

        ValueTextBlock.Visibility = Visibility.Visible;
       
        Canvas.SetLeft(ValueTextBlock,
            polygonBounds.X + (polygonBounds.Width / 2) -
            (ValueTextBlock.ActualWidth / 2));

        Canvas.SetTop(ValueTextBlock, polygonBounds.Y - 20);
    }
}

void oneBar_MouseLeave(object sender, MouseEventArgs e)
{
    if (_selectedPolygon != null)
    {
        _selectedPolygon.StrokeThickness = 0;
        _selectedPolygon.Stroke = null;

        ValueTextBlock.Visibility = Visibility.Collapsed;

        _selectedPolygon = null;
    }
}

private Rect GetPolygonBounds(PointCollection points)
{
    double minX, minY;
    double maxX, maxY;

    if (points == null || points.Count == 0) return Rect.Empty;

    maxX = 0;
    maxY = 0;
    minX = double.MaxValue;
    minY = double.MaxValue;

    foreach (Point onePoint in points)
    {
        if (onePoint.X < minX)
            minX = onePoint.X;

        if (onePoint.Y < minY)
            minY = onePoint.Y;

        if (onePoint.X > maxX)
            maxX = onePoint.X;

        if (onePoint.Y > maxY)
            maxY = onePoint.Y;
    }

    return new Rect(minX, minY, maxX - minX, maxY - minY);
}
    }
}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1357365.html