Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
815
How to create basic LineSeries chart with XamDataChart and no XAML
posted

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

    }
}