将数据集绑定到图表
我想将数据表中的 2 列绑定到图表。 Visifire 示例显示了使用 observablecollection 的示例,但我不知道如何将数据表与 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 并寻找替代方案,最好有良好的文档和支持。欢迎任何建议。
I have 2 column from a datatable that i want to bind to chart. Visifire sample shows example using observablecollection but i dont know how to relate datatable to observablecollection(which i think is my problem). I created a sample to visualize. I am using visifire for the charts.
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);
}
and here are the 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>
I did try ask on the official visifire forum but the support guy was just plain lazy(which told me to look at the sample when i did told them i already look at the sample. plus, portion of the code are copied from the sample).
this kind of things makes me want to ditch visifire and look for alternative preferably with good documentation and support. any suggestion are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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;
查看下面的示例代码。
XAML:
http://www.visifire.com/silverlight_examples_details.php?id=10
If you look into the above example you can see that DataSource property of DataSeries is set as ItemsSource of Grid which is nothing but a collection (ObservableCollection of Value class).
DataSource="{Binding ItemsSource, ElementName=MyGrid}"
So you need to set DataSource property of DataSeries as collection of rows (nothing but a Table).
ds.DataSource = dtBandwidth.Tables[0].DefaultView;
Checkout the sample code below.
XAML: