将DataSet绑定到图表

时间:2011-03-04 09:15:45

标签: wpf data-binding datatable observablecollection visifire

我有一个数据表的2列,我想绑定到图表。 Visifire示例使用observablecollection显示示例,但我不知道如何将datatable与observablecollection相关联(我认为这是我的问题)。我创建了一个可视化的样本。我在图表中使用visifire。

    public MainWindow()
    {
        InitializeComponent();

        dtBandwidth = dsBandwidth.Tables.Add();
        dtBandwidth.Columns.Add("ID", typeof(int));
        dtBandwidth.Columns.Add("Time", typeof(double));
        dtBandwidth.Columns.Add("Value", typeof(double));

        dataGrid1.ItemsSource = dtBandwidth.DefaultView;

        DataSeries ds = new DataSeries();
        ds.RenderAs = RenderAs.Line;
        ds.DataSource = dtBandwidth???; //i know this is wrong. what should i do?
        DataMapping dm = new DataMapping();
        dm.MemberName = "XValue";
        dm.Path = "Time";
        ds.DataMappings.Add(dm);
        dm = new DataMapping();
        dm.MemberName = "YValue";
        dm.Path = "Value";
        ds.DataMappings.Add(dm);

        chart1.Series.Add(ds);
        chart1.DataContext = dtBandwidth???; //i know this is wrong. what should i do?

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        dtBandwidth.Rows.Add(1, 1.0, 5.2);
        dtBandwidth.Rows.Add(2, 2.1, 5.1);
        dtBandwidth.Rows.Add(3, 3.2, 5.3);
        dtBandwidth.Rows.Add(4, 4.3, 5.4);
        dtBandwidth.Rows.Add(5, 5.4, 5.5);
    }

这是xaml。

<Grid>
    <DataGrid AutoGenerateColumns="True" Height="311" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="143" />
    <my:Chart HorizontalAlignment="Left" Margin="149,0,0,0" Name="chart1" VerticalAlignment="Top" Height="311" Width="354" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="186,328,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

我确实试过在官方visifire论坛上问,但支持人员只是简单的懒惰(当我告诉他们我已经看过样本时,它告诉我看样本。加上,部分代码是从例子)。

这种事情让我想放弃visifire并寻找替代方案,最好是提供良好的文档和支持。任何建议都是受欢迎的。

1 个答案:

答案 0 :(得分:1)

http://www.visifire.com/silverlight_examples_details.php?id=10

如果查看上面的示例,您可以看到DataSeries的DataSource属性被设置为Grid的ItemsSource,它只是一个集合(Value类的ObservableCollection)。

DataSource =“{Binding ItemsSource,ElementName = MyGrid}”

因此,您需要将DataSeries的DataSource属性设置为行集合(除了表之外)。

ds.DataSource = dtBandwidth.Tables [0] .DefaultView;

查看下面的示例代码。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataSet dtBandwidth = new DataSet();
        dtBandwidth.Tables.Add("BandWidth");
        dtBandwidth.Tables[0].Columns.Add("ID", typeof(int));
        dtBandwidth.Tables[0].Columns.Add("Time", typeof(double));
        dtBandwidth.Tables[0].Columns.Add("Value", typeof(double));

        dtBandwidth.Tables[0].Rows.Add(new object[] { 2, 1, 5 });
        dtBandwidth.Tables[0].Rows.Add(new object[] { 2, 2, 25 });

        DataSeries ds = new DataSeries();
        ds.RenderAs = RenderAs.Line;

        ds.DataSource = dtBandwidth.Tables[0].DefaultView;

        DataMapping dm = new DataMapping();
        dm.MemberName = "XValue";
        dm.Path = "Time";
        ds.DataMappings.Add(dm);
        dm = new DataMapping();
        dm.MemberName = "YValue";
        dm.Path = "Value";
        ds.DataMappings.Add(dm);

        chart1.Series.Add(ds);

    }
}

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:vc="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts">
    <Grid>
        <vc:Chart Name="chart1" Width="500" Height="300" Padding="10,10" Margin="10,0" AnimatedUpdate="True">
    </vc:Chart>
</Grid>