Binding CartesianChart to a Datatable

3 posts, 1 answers
  1. Alberto
    Alberto avatar
    26 posts
    Member since:
    Nov 2012

    Posted 04 Nov 2013 Link to this post

    Hi, 

    I am following some examples to bind a CartesianChart to a Datatable but I haven't been able to do it, this is my code:

    <telerik:RadCartesianChart Name="chartTest" Margin="10" Palette="Grayscale">
        <telerik:RadCartesianChart.Behaviors>
            <telerik:ChartPanAndZoomBehavior PanMode="Both" ZoomMode="Both"/>
        </telerik:RadCartesianChart.Behaviors>
    </telerik:RadCartesianChart>

    And the code behind:

    public class Chemistry
        {
            public string DESCRIPTION { get; set; }
            public double HEATS_IN_PCT { get; set; }
            public double HEATS_IN_PCT_LINE { get; set; }       
        }

    public class MainViewModel
    {
        public ObservableCollection<Chemistry> Data { get; private set; }
                 
        public MainViewModel()
        {
            this.Data = GetTestData();
        }
     
        private static ObservableCollection<Chemistry> GetTestData()
        {
            var result = new ObservableCollection<Chemistry>();
     
            //Custom function to load a stored procedure data
            DataTable dtChemistry = DataRetriever.ExecuteRetrieveProcedure("SB_QUALITY_CHEMISTRY");
     
            foreach (DataRow dr in dtChemistry.Rows)
            {
                result.Add(new Chemistry
                {
                    DESCRIPTION = dr["DESCRIPTION"].ToString(),
                    HEATS_IN_PCT = (double)dr["HEATS_IN_PCT"],
                    HEATS_IN_PCT_LINE = (double)dr["HEATS_IN_PCT_LINE"]
                });
            }
     
            return result;
        }
    }

    void Configure_Chart()
            {
                BarSeries barSer = new BarSeries() { Name = "barCrew" };
                barSer.ShowLabels = true;
                barSer.CombineMode = ChartSeriesCombineMode.Cluster;
     
                barSer.LabelDefinitions.Clear();
                barSer.LabelDefinitions.Add(new ChartSeriesLabelDefinition() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });
     
                Telerik.Windows.Controls.ChartView.LineSeries lineSer = new Telerik.Windows.Controls.ChartView.LineSeries() { Name = "lineCrew" };
                 
                barSer.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "DESCRIPTION" };
                barSer.ValueBinding = new GenericDataPointBinding<Chemistry, double>() { ValueSelector = Chemistry => Chemistry.HEATS_IN_PCT};
    //Also used PropertyNameDataPointBinding("HEATS_IN_PCT");
     
                lineSer.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "DESCRIPTION" };
                lineSer.ValueBinding = new GenericDataPointBinding<Chemistry, double>() { ValueSelector = Chemistry => Chemistry.HEATS_IN_PCT_LINE };
    //Also used PropertyNameDataPointBinding("HEATS_IN_PCT_LINE");
     
                MainViewModel vm = new MainViewModel();
     
                barSer.ItemsSource = vm.Data;
                lineSer.ItemsSource = vm.Data;
     
                chartTest.Series.Add(barSer);
                chartTest.Series.Add(lineSer);
            }

    And when I run the application, the series do not appear.

    I am missing something?

    Thanks,

    Alberto




  2. Answer
    Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 07 Nov 2013 Link to this post

    Hello Alberto,

    Perhaps you forgot to set your axes. Add this to your chart xaml:
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis />
    </telerik:RadCartesianChart.HorizontalAxis>


    Regards,
    Yavor
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alberto
    Alberto avatar
    26 posts
    Member since:
    Nov 2012

    Posted 07 Nov 2013 Link to this post

    My bad =/ I copied code from other project and I forgot that part:

    chartTest.VerticalAxis = new LinearAxis();
    chartTest.HorizontalAxis = new CategoricalAxis();

    Thanks for your time Yavor.

    Regards,

    Alberto
Back to Top