We need to dynamically create an XamlDataChart in a WPF application. The user will click a button to add/remove line series from the chart. The series are determined dynamically at run time.
The example below is based on the Getting Started with XamDataChart in the infragistics help. Is there any reason why it only shows an empty chart?
It is the main window from a newly created WPF application. C# and XAML are below.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="400" Width="525"> <Grid Height="400" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Aqua"> <StackPanel x:Name="MyContentPanel" Background="lightyellow" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</StackPanel> </Grid></Window>
using System.Collections.ObjectModel;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using Infragistics.Controls.Charts;
namespace WpfApplication1{ /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); testChart(); }
/// <summary> /// /// </summary> public class SimpleDataPoint { /// <summary> /// /// </summary> public string Label; /// <summary> /// /// </summary> public double Value; }
/// <summary> /// /// </summary> public class SimpleDataCollection : ObservableCollection<SimpleDataPoint> { /// <summary> /// Initializes a new instance of the <see cref="SimpleDataCollection"/> class. /// </summary> public SimpleDataCollection() { this.Add(new SimpleDataPoint() { Label = "1", Value = 3.0 }); this.Add(new SimpleDataPoint() { Label = "2", Value = 2.0 }); this.Add(new SimpleDataPoint() { Label = "3", Value = 3.0 }); this.Add(new SimpleDataPoint() { Label = "4", Value = 4.0 }); this.Add(new SimpleDataPoint() { Label = "5", Value = 5.0 }); this.Add(new SimpleDataPoint() { Label = "6", Value = 6.0 }); this.Add(new SimpleDataPoint() { Label = "7", Value = 5.0 }); } }
private void testChart() { XamDataChart chart = new XamDataChart(); chart.Visibility = System.Windows.Visibility.Visible; chart.Background = new SolidColorBrush(Colors.LightGray); chart.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; chart.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; //Add the xamDataChart control with SimpleDataCollection model bound to the control’s DataContext property.
SimpleDataCollection data = new SimpleDataCollection(); chart.DataContext = data;
//Add CategoryXAxis (horizontal) and NumericYAxis (vertical) axis to the control’s Axes collection.
// XAxis CategoryXAxis xmXAxis = new CategoryXAxis(); xmXAxis.ItemsSource = data; xmXAxis.Label = "{Label}"; xmXAxis.LabelSettings = new AxisLabelSettings(); //ccc xmXAxis.LabelSettings.Extent = 35; xmXAxis.LabelSettings.Location = AxisLabelsLocation.OutsideBottom; chart.Axes.Add(xmXAxis);
// YAxis NumericYAxis xmYAxis = new NumericYAxis(); xmYAxis.LabelSettings = new AxisLabelSettings(); //ccc xmYAxis.LabelSettings.Extent = 55; xmYAxis.LabelSettings.Location = AxisLabelsLocation.OutsideLeft; chart.Axes.Add(xmYAxis);
//Add SplineAreaSeries object to the Series collection with the following attributes.
// Series LineSeries series = new LineSeries(); series.ValueMemberPath = "Value"; series.XAxis = xmXAxis; series.YAxis = xmYAxis; series.ItemsSource = data; series.XAxis.ItemsSource = data; chart.Series.Add(series);
this.MyContentPanel.Children.Add(chart); this.MyContentPanel.Children.Add(new TextBlock() { Background = new SolidColorBrush(Colors.Red), Text = "text block text" }); }
}}
Hello,
I have been looking into your requirement and I can suggest you go through this link in the online documentation: http://help.infragistics.com/NetAdvantage/DV/2011.2/CLR4.0/?page=xamDataChart_Getting_Started_with_xamDataChart.html . Usually in the documentation the samples code snippets are available in xaml, C# and Visual Basic. All you would have to do is change the SplineAreaSeries with LineSeries.
Please let me know if I can be of any further assistance on the matter.
Sincerely,
Petar, MCTS
Developer Support Engineer II
Infragistics
www.infragistics.com/support
I tried the example code again with no success. I think the sample code depends on the axis and graph control being included in the xaml. We need to dynamically add the graph to a container control. The container control, graph contents and data source are all determined at run time based on the user input.
Is there a sample application available for download that does not have any of the XamDataChart defined in XAML for a simple chart?
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
If the above suggestion helped you solve your issue please verify the thread as answered so other users may take better advantage of it.
Petar,
Thanks to your help here. It greatly helped us get started using the xamDataChart.
It worked for us. Please update the help for the XamDataChart since the code comes from the getting started example. Dynamic creation of ad hoc charts is a common scenario in the Infragistics forums.
Thank you for your great help.