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" }); }
}}
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 ?
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.
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
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.