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 have looked at your issue again and I tried copy/pasting the C# code from the link I had provided. I did have to add a few lines of code in order to get rid of the errors. I have attached the sample project (DataChart_code_beind.zip) for your reference. I have marked the added lines with comments in xaml and code.
Hi,
This example (DataChart_code_behid.zip) works fine. But doesn't work like
DataPresenterExcelExporter excelExporter = new DataPresenterExcelExporter();
excelExporter.Export(myXamDataChart, "mmmd", WorkbookFormat.Excel2007); it works well without adding DataGrid to visual tree.
The Excel Exporter doesn't required you put a container Grid to the xaml.
The purpose I am asking for this is, some time we need batch mode / console mode.
then it is not possible to add Grid.
Any thought ? thx.
David
Hello David,
I have been looking into your reply and am not sure what exactly is your question. Please explain in more details what is goal here regarding the batch mode / console mode applications and what are your concerns in the implementation.
Looking forward to hearing from you.
Petar,
What I am looking is similar funtionality like what DataPresenter.Export offered. as we know DataPresenter.Export() can export XamDataGrid to excel without requiring XamDataGrid to be part of visual tree. While xamDataChart seems require xamDataChart must be showing in the visual tree (Grid container).
We can use console app. to export xamDataGrid to excel without any problem., but now we can not export xamDataChart to excet in console.exe.
Am I clear ?