Create Chart with Code

4 posts, 1 answers
  1. YongKoo Kang
    YongKoo Kang avatar
    101 posts
    Member since:
    Jan 2010

    Posted 21 Feb 2014 Link to this post

    Hi telerik,

    I have to create chart with code not xaml having LogarithmicAxis and LinearAxis, but can't display series successfully
    and don't know  what is my mistake, though I refered your sample code in this forum and your doc as well.
    My code snippet is as follows;
    private RadCartesianChart GetChart()
         {
             RadCartesianChart cartChart = new RadCartesianChart();
     
             cartChart.HorizontalAxis = new LinearAxis() { Maximum = 50, Minimum = 17, MajorStep = 3 };
             cartChart.VerticalAxis = new LogarithmicAxis() { LogarithmBase = 10, Minimum = 1000, Maximum = 1000000 };
     
             LineSeries lineSeries = new LineSeries() { ShowLabels = true };
             // lineSeries.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "Age" };
             lineSeries.CategoryBinding = new GenericDataPointBinding<CordXy, double>() { ValueSelector = value => value.Age };
             lineSeries.ValueBinding = new GenericDataPointBinding<CordXy, double>() { ValueSelector = cord => cord.Num };
             // lineSeries.ValueBinding = new PropertyNameDataPointBinding() { PropertyName = "Num" };
             lineSeries.ItemsSource = CordXy.GetFollicleList();
     
             cartChart.Series.Add(lineSeries);
     
             DoubleCollection aray = new DoubleCollection(new double[] {1,1});
     
             cartChart.Annotations.Add(
                     new CartesianCustomLineAnnotation
                     {
                         HorizontalFrom = 35,
                         HorizontalTo = 35,
                         VerticalFrom = 0,
                         VerticalTo = 1000000,
     
                         Stroke = new SolidColorBrush(Colors.Blue),
                         StrokeThickness = 2,
                         DashArray = aray
                     });
             return cartChart;
         }
     }
     
     public class CordXy
     {
         public double Age { get; set; }
         public double Num { get; set; }
     
         public static ObservableCollection<CordXy> GetFollicleList()
         {
             var result = new ObservableCollection<CordXy>();
             for (int i = 0; i < 33; i++)
             {
                 var add = new CordXy();
                 add.Age = i + 18;
                 add.Num = Math.Pow(10, (5.717 - 0.00019 * Math.Pow(add.Age, 2.452)));
                 result.Add(add);
             }
             return result;
         }
     }

    As you see, x-axis is linear which shows age of person and y-axis shows calculated value as logarithmic.
    I think I did correct as you told with your sample and doc, but no luck.
    Pls see my attached image and advise me what I made a mistake.

    Thank you in advance.

    RGDS
    HK.Lee






  2. Answer
    Milena
    Admin
    Milena avatar
    204 posts

    Posted 26 Feb 2014 Link to this post

    Hello Kang,

    You can combine different type of series with the appropriate type of axis – more about the possible combinations you can see in our Series and Axes article. In your RadChartView you have Logarithmic Axis and Linear Axis, which required using of Scatter Series, that can be plotted on two linear axes unlike the other series (like Line Series ) which are always plotted on a categorical and a linear or another categorical axis. So, if you change the Line Series with Scatter Series, they will be displayed successfully.

    For your convenience I’m sending you a sample project (following your code), where I’ve used a ScatterLineSeries.

    I hope this information helps you.


    Regards,
    Milena
    Telerik
  3. UI for WPF is Visual Studio 2017 Ready
  4. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 26 Feb 2014 Link to this post

    Hello Kang,

    You can combine different type of series with the appropriate type of axis – more about the possible combinations you can see in our Series and Axes article. In your RadChartView you have Logarithmic Axis and Linear Axis, which required using of Scatter Series, that can be plotted on two linear axes unlike the other series (like Line Series ) which are always plotted on a categorical and a linear or another categorical axis. So, if you change the Line Series with Scatter Series, they will be displayed successfully.

    For your convenience I’m sending you a sample project (following your code), where I’ve used a ScatterLineSeries.

    I hope this information helps you.


    Regards,
    Pavel R. Pavlov
    Telerik
  5. YongKoo Kang
    YongKoo Kang avatar
    101 posts
    Member since:
    Jan 2010

    Posted 02 Mar 2014 Link to this post

    Thank you Milena,
    It works great and your kind explanation was a great help.

    RGDS
    Kang
Back to Top