DateTimeContinuousAxis labels format

4 posts, 0 answers
  1. Elton
    Elton avatar
    3 posts
    Member since:
    Jul 2014

    Posted 18 Jul 2014 Link to this post

    Hi,

    I am working on a RadCartesianChartView with a DateTimeContinuousAxis horizontal axis and  I can run the example on http://docs.telerik.com/devtools/android/controls/chart/axes/chart-axes-datetimecontinuous.

    When  I change the data to hourly and set the steps to:

    horizontalAxis.setMajorStepUnit(TimeInterval.HOUR);
    horizontalAxis.setMajorStep(1);

    the label format is still the default "MMM DD, YYYY" (i.e. Jan 4, 2014) format.

    1- How can I change the label format on a DateTimeContinuousAxis?
    2 - Is there a way to change the format of label as you zoom in? (e.g. the default format is "MMM DD" then as you zoom in on a certain day the axis re-scales and labels becomes by hour "HH:MM").

    Thanks.
  2. Antony Jekov
    Admin
    Antony Jekov avatar
    88 posts

    Posted 21 Jul 2014 Link to this post

    Hello Elton,

    Thank you for contacting Telerik support.

    1 You can change the format of the labels by using a custom value to string converter:

    horizontalAxis.setLabelValueToStringConverter(new Function<Object, String>() {
                SimpleDateFormat simpleFormat = new SimpleDateFormat("HH:MM");
     
                @Override
                public String apply(Object o) {
                    return this.simpleFormat.format((((MajorTickModel) o).value()));
                }
            });

    2 You can extend the default pan and zoom behavior in order to apply your custom logic once a certain zoom level has been reached:

    private static class CustomPanAndZoomBehavior extends ChartPanAndZoomBehavior {
     
            private static final double MAX_ZOOM_WIDTH = 2;
     
            @Override
            public void setZoomToChart(double scale) {
                super.setZoomToChart(scale);
     
                RadSize currentZoom = this.chart.getZoom();
                if (currentZoom.width > MAX_ZOOM_WIDTH) {
                    // apply different format to the labels here
                }
            }
        }

    The default zoom, or no zoom, is 1, 1. Going out of there you can decide at which point you want to switch the value-to-string converter.

    I hope this answers your questions. Please feel free to add to this ticket if something stays unclear, or if you need additional assistance.

    Thank you for your time and all best!

    Regards,
    Antony Jekov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. Elton
    Elton avatar
    3 posts
    Member since:
    Jul 2014

    Posted 21 Jul 2014 in reply to Antony Jekov Link to this post

    Thanks for your answer Antony. Label formatting works fine. But when I set the CustomPanAndZoomBehavior the labels on x-axis are just disappearing when zooming in. I get the same issue with a normal zoom behavior as well.
    ChartPanAndZoomBehavior behavior = new ChartPanAndZoomBehavior();
    chartView.getBehaviors().add(behavior);

    Is this a known issue or a work around to fix it?
    Thanks.
  5. Antony Jekov
    Admin
    Antony Jekov avatar
    88 posts

    Posted 23 Jul 2014 Link to this post

    Hi Elton,

    I can see there is a problem with the DateTimeContinousAxis when panning and zooming. The labels indeed are not being calculated correctly.

    Thank you for reporting this. It is logged and it will be fixed for the next service pack. I have a workaround for you, that I hope will suit your needs:
    private static class CustomDateTimeContinuousAxis extends DateTimeContinuousAxis {
     
            public CustomDateTimeContinuousAxis(Context context) {
                super(context);
            }
     
            @Override
            public AxisModel createModel() {
                return new CustomDateTimeContinuousAxisModel();
            }
     
            private static class CustomDateTimeContinuousAxisModel extends DateTimeContinuousAxisModel {
                @Override
                protected Iterable<AxisTickModel> generateTicks(ValueRange<Double> visibleRange) {
                    return super.generateTicks(new ValueRange<Double>(0.0, 1.0));
                }
            }
        }

    I believe this will work for you. Please feel free to inform me if it doesn't, so I can provide you with an internal build as soon as the issue has been fixed on our side.

    I am sorry you had to struggle with this and I hope this will not affect your project's development process.

    Thank you for your time and all best!

    Regards,
    Antony Jekov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft banner