如何在Silverlight 4中的一个图表上绘制多行图表

时间:2011-05-23 19:09:46

标签: silverlight charts graphing

我是Silverlight的新手,我只是想弄清楚如何做一些事情。我想做的是有一个图表,有2行可能相互重叠。我的代码发生的是显示一行,然后显示下一行,一个接一个而不是重叠。

这是我的xaml

    <navigation:Page x:Class="SilverlightNav1.Home" 
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:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="547"
Title="Home"
Style="{StaticResource PageStyle}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

<Grid x:Name="LayoutRoot" Height="544">
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="First Name" />
    <TextBox Height="23" Margin="91,8,0,0" Name="txtFirstName" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" >
        <TextBox.RenderTransform>
            <SkewTransform AngleX="2" AngleY="12" />
        </TextBox.RenderTransform>
        <TextBox.Effect>
            <DropShadowEffect />
        </TextBox.Effect>
    </TextBox>
    <Button Content="Greet Me" Height="23" HorizontalAlignment="Left" Margin="91,72,0,0" Name="GreetMe" VerticalAlignment="Top" Width="75" Click="GreetMe_Click" />
    <TextBlock Margin="91,116,25,0" Name="Greeting" Text="" Height="105" VerticalAlignment="Top" FontSize="20" />
    <toolkit:Chart HorizontalAlignment="Left" Margin="21,246,0,0" Name="chart1" Title="Testing graph" VerticalAlignment="Top" Height="249" Width="400">
        <toolkit:Chart.Series>
            <toolkit:LineSeries Title="Guys" ItemsSource="{Binding p}" IndependentValueBinding="{Binding FirstName}" DependentValueBinding="{Binding Greeting}" Name="testGraph">
            </toolkit:LineSeries>
            <toolkit:LineSeries Title="Girls" ItemsSource="{Binding girls}" IndependentValueBinding="{Binding FirstName}" DependentValueBinding="{Binding Greeting}" Name="lsGirls">
            </toolkit:LineSeries>
        </toolkit:Chart.Series>
    </toolkit:Chart>
</Grid>

这是我的代码隐藏。

        List<Model.Person> p = new List<Model.Person>();
        p.Add(new Model.Person() { FirstName = "Bob", Greeting = 1 });
        p.Add(new Model.Person() { FirstName = "Phil", Greeting = 3 });
        p.Add(new Model.Person() { FirstName = "Dennis", Greeting = 4});
        p.Add(new Model.Person() { FirstName = "Chewie", Greeting = 2 });
        //this.DataContext = p;

        List<Model.Person> girls = new List<Model.Person>();
        girls.Add(new Model.Person() { FirstName = "Jamie", Greeting = 4 });
        girls.Add(new Model.Person() { FirstName = "Abby", Greeting = 7 });
        girls.Add(new Model.Person() { FirstName = "M", Greeting = 5 });
        girls.Add(new Model.Person() { FirstName = "K", Greeting = 6 });

        ((LineSeries)chart1.Series[0]).ItemsSource = p;
        ((LineSeries)chart1.Series[1]).ItemsSource = girls;

最后,这是我在person.cs中定义的类

   namespace SilverlightNav1.Model
   {
   public class Person
   {
   private string _firstName;
   public string FirstName
   {
   get { return _firstName; }
   set
   {
   _firstName = value;
   }
   }

   private int _greeting;
   public int Greeting
   {
   get { return _greeting; }
   set { _greeting = value; }
   }
   }
   }

1 个答案:

答案 0 :(得分:0)

问题是两个数据集都有不同的X轴值 - 例如Bob和Jamie不相同,因此工具包不会在相同的X轴值上显示它们。您可以通过将女孩数据集FirstName更改为Bob,Phil,Dennis和Chewie来了解如何解决这个问题。

一种选择是为男孩和女孩组分配一个增加的数字计数器,并将其用作X轴,以便将它们绘制在一起。因此,将X轴绑定到“Counter”属性,并使bob / phil / dennis / chewie各自具有0,1,2和3的值,并且对于Jamie到K,也是相同的。您可以设置图形的样式显示数据点本身旁边的实际FirstName,或其他此类方法。