Silverlight Chart + ColumnSeries + DataPointStyle 方法
MainPage.xaml |
<UserControl 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:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="SilverlightApplication.MainPage" mc:Ignorable="d"> <toolkit:Chart x:Name="SalesChart" BorderBrush="{x:Null}" Margin="0,0,0,0" FontFamily="Arial Unicode MS" FontSize="15"> <toolkit:ColumnSeries DependentValueBinding="{Binding SalesAmt}" IndependentValueBinding="{Binding ProductName}" AnimationSequence="FirstToLast"> <toolkit:ColumnSeries.DataPointStyle> <Style TargetType="toolkit:DataPoint"> <Setter Property="Background" Value="#990011"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="toolkit:ColumnDataPoint"> <Border BorderBrush="{x:Null}"> <Border.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="-0.5,0.5"> <GradientStop Color="#000000" Offset="0"/> <GradientStop Color="#990011" Offset="1"/> </LinearGradientBrush> </Border.Background> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </toolkit:ColumnSeries.DataPointStyle> </toolkit:ColumnSeries> </toolkit:Chart> </UserControl> |
MainPage.xaml.cs |
using System; using System.Collections.Generic; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Controls.DataVisualization.Charting; namespace SilverlightApplication { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); List<Product> SalesList = new List<Product> { new Product { ProductName = "Product A", SalesAmt = 850.0 }, new Product { ProductName = "Product B", SalesAmt = 700.0 }, new Product { ProductName = "Product C", SalesAmt = 820.0 }, new Product { ProductName = "Product D", SalesAmt = 600.0 }, new Product { ProductName = "Product E", SalesAmt = 910.0 }, new Product { ProductName = "Product F", SalesAmt = 760.0 } }; ((ColumnSeries)this.SalesChart.Series[0]).ItemsSource = SalesList; } public class Product { public string ProductName { get; set; } public double SalesAmt { get; set; } } } } |