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
325
Bind to current DataContext
posted

Hi!

I created a class that represents a model and contains a property "TimeSeries". An instance of that class is set as DataContext of my view that implements a xamChart. Unfortunately the timeseries are not displayed. Following you will find the piece of xaml code that defines the chart:

 <my:XamChart Name="ChartTimeseries" Grid.Row="1" Grid.Column="1" Width="Auto" Height="Auto" Margin="25" >
            <my:XamChart.Series>
                <my:Series
                      StrokeThickness="4"
                      ChartType="Line"
                      DataSource="{Binding TimeSeries, UpdateSourceTrigger=PropertyChanged}"
                      DataMapping="ValueX=ValueAsOf; ValueY=ValueAbsolute"
                      Fill="#FF6CA224" >
                </my:Series>
            </my:XamChart.Series>
            <my:XamChart.Axes>
                <my:Axis AxisType="PrimaryX" >
                    <my:Axis.Label>
                        <my:Label Format="{}{0}" />
                    </my:Axis.Label>
                </my:Axis>

 

 

                <my:Axis AxisType="PrimaryY">
                    <my:Axis.Label>
                        <my:Label Angle="-45" Format="{}{0:c}" />
                    </my:Axis.Label>
                </my:Axis>

            </my:XamChart.Axes>
</my:XamChart>

Please could you help me?

Regards
Dan

  • 30692
    Suggested Answer
    Offline posted

    If you are using a line chart, then the DataMapping doesn’t use ValueX and ValueY chart parameters, but rather Label for the X axis, and Value for the Y axis, because it’s treated as a “Single value chart”. So adjusting the Xaml to look like so should work:

    <Window x:Class="WpfApplication3.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300"

            xmlns:my="http://infragistics.com/Chart">

        <Grid>

            <my:XamChart Name="ChartTimeseries" Grid.Row="1" Grid.Column="1" Width="Auto" Height="Auto" Margin="25" >

                <my:XamChart.Series>

                    <my:Series

                          StrokeThickness="4"

                          ChartType="Line"

                          DataSource="{Binding TimeSeries, UpdateSourceTrigger=PropertyChanged}"

                          DataMapping="Label=ValueAsOf; Value=ValueAbsolute"

                          Fill="#FF6CA224" >

                    </my:Series>

                </my:XamChart.Series>

                <my:XamChart.Axes>

                    <my:Axis AxisType="PrimaryX" >

                        <my:Axis.Label>

                            <my:Label Format="{}{0}" />

                        </my:Axis.Label>

                    </my:Axis>

                    <my:Axis AxisType="PrimaryY">

                        <my:Axis.Label>

                            <my:Label Angle="-45" Format="{}{0:c}" />

                        </my:Axis.Label>

                    </my:Axis>

     

                </my:XamChart.Axes>

            </my:XamChart>

        </Grid>

    </Window>

    And this is the code behind I used:

    public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

     

                this.DataContext = new TimeSeriesViewModel();

            }

        }

     

        public class TimeSeriesViewModel

        {

            private ObservableCollection<TimeEntryViewModel> _timeSeries;

            public ObservableCollection<TimeEntryViewModel> TimeSeries

            {

                get

                {

                    if (_timeSeries == null)

                    {

                        _timeSeries = TestDataProvider.GetData();

                    }

                    return _timeSeries;

                }

            }

        }

     

        public class TimeEntryViewModel

        {

            public DateTime ValueAsOf { get; set; }

            public double ValueAbsolute { get; set; }

        }

     

        public class TestDataProvider

        {

            public static ObservableCollection<TimeEntryViewModel> GetData()

            {

                ObservableCollection<TimeEntryViewModel> list = new ObservableCollection<TimeEntryViewModel>();

     

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 1, 1), ValueAbsolute = 11 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 2, 1), ValueAbsolute = 10 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 3, 1), ValueAbsolute = 10 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 4, 1), ValueAbsolute = 9 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 5, 1), ValueAbsolute = 11 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 6, 1), ValueAbsolute = 10 });

                list.Add(new TimeEntryViewModel() { ValueAsOf = new DateTime(2009, 7, 1), ValueAbsolute = 12 });

     

                return list;

            }

        }