Reputation: 4305
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.
Upvotes: 0
Views: 2490
Reputation: 3267
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.
/// <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>
Upvotes: 1