This is a migrated thread and some comments may be shown as answers.

Binding CartesianChart to a Datatable

2 Answers 49 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Alberto
Top achievements
Rank 1
Alberto asked on 04 Nov 2013, 07:43 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 07 Nov 2013, 05:57 PM
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 >>
0
Alberto
Top achievements
Rank 1
answered on 07 Nov 2013, 06:59 PM
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
Tags
Chart
Asked by
Alberto
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Alberto
Top achievements
Rank 1
Share this question
or