Using nullable decimal fields to optionally set ValueAxis Min and Max

3 posts, 1 answers
  1. James
    James avatar
    20 posts
    Member since:
    Feb 2015

    Posted 31 Mar Link to this post

    I'm hoping this is possible as we are allowing end users to optionally set the min and max value axis amounts. I would like the chart itself to set either or both the Min or Max if the decimal? field is null. Right now I'm using the following code snippet:

    .ValueAxis(x => x.Numeric()
            .Min(Model.YAxisRangeMin.GetValueOrDefault())
            .Max(Model.YAxisRangeMax.GetValueOrDefault()))

     This works fine if the decimal? fields have values, but if they are null, the defaults are set to 0, which is not good.

    Problem is, I really only want to set a Min/Max value if it is not null. Any ideas how to do that within the control? Am I going to have to calculate the max on the server and use that as the default for the YAxisRangeMax if null? Min as 0 is fine.

    Thanks!

  2. James
    James avatar
    20 posts
    Member since:
    Feb 2015

    Posted 01 Apr Link to this post

    For now, I just stuffed the max Value into a field and am using that to set the default Max value (i.e., Max(Model.YAxisRangeMax.GetValueOrDefault(Model.YAxisMaxDefault)), which seems to be working (I set the max to 1.1 X the actual max value). Since the end user can also select different chart types, I added another property for StackedArea YAxisMax (3 x max). Let me know if there is a better way.

     

    Thanks!

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Answer
    Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 04 Apr Link to this post

    Hello,

    You could check if the value is not null before setting the option to the axis e.g.
    .ValueAxis(axis =>
        {
            var numericAxis = axis.Numeric();
            if (Model.YAxisRangeMin.HasValue)
            {
                numericAxis.Min(Model.YAxisRangeMin.Value);
            }
            if (Model.YAxisRangeMax.HasValue)
            {
                numericAxis.Max(Model.YAxisRangeMax.Value);
            }
        }
    )


    Regards,
    Daniel
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top