Search Results for

    Show / Hide Table of Contents

    Axis Ticks - TickProvider and DeltaCalculator API

    It is possible to further customize the MajorDelta, MinorDelta and affect the tick frequency of an axis, to have a totally custom set of axis ticks (gridlines, label intervals) on the chart. That can be achieved quite easily through DeltaCalculator and TickProvider APIs.

    Creating your own DeltaCalculator

    To have full control over the calculations of MajorDelta and MinorDelta you will need to create a custom IDeltaCalculator. By default each ITickProvider has a DeltaCalculator created for it. The type of DeltaCalculator depends on the TickProvider. Below is a table of the DeltaCalculator already defined in SciChart Android.

    Delta Calculator Type Used By
    NumericDeltaCalculator NumericTickProvider and CategoryTickProvider
    LogarithmicDeltaCalculator LogarithmicNumericTickProvider
    DateDeltaCalculator DateTickProvider

    To create a custom IDeltaCalculator, you need to inherit from the correct class, according to the table above, then override getDeltaFromRange(Comparable<?> min, Comparable<?> max, int minorsPerMajor, int maxTicks) method. This method is called internally by ITickProvider for every axis when it needs to recalculate AxisDelta<T>.

    Let's create a custom IDeltaCalculator and use it with NumericTickProvider for NumericAxis.

    • Java
    • Java with Builders API
    • Kotlin
    class CustomNumericDeltaCalculator extends NumericDeltaCalculator {
        @Override
        public IAxisDelta<Double> getDeltaFromRange(Comparable min, Comparable max, int minorsPerMajor, int maxTicks) {
            // For the sake of simplicity, we will use simple constants.
            // But you might want to have some complex calculations here.
            double minorDelta = 0.07;
            double majorDelta = 0.1;
            return new AxisDelta<>(minorDelta, majorDelta);
        }
    }
    
    // create a NumericTickProvider with CustomNumericDeltaCalculator and assign it to the axis
    final NumericAxis axis = new NumericAxis(getContext());
    axis.setTickProvider(new NumericTickProvider(new CustomNumericDeltaCalculator()));
    
    class CustomNumericDeltaCalculator extends NumericDeltaCalculator {
        @Override
        public IAxisDelta<Double> getDeltaFromRange(Comparable min, Comparable max, int minorsPerMajor, int maxTicks) {
            // For the sake of simplicity, we will use simple constants.
            // But you might want to have some complex calculations here.
            double minorDelta = 0.07;
            double majorDelta = 0.1;
            return new AxisDelta<>(minorDelta, majorDelta);
        }
    }
    
    // create a NumericTickProvider with CustomNumericDeltaCalculator and assign it to the axis
    final NumericAxis axis = sciChartBuilder.newNumericAxis().build();
    axis.setTickProvider(new NumericTickProvider(new CustomNumericDeltaCalculator()));
    
    class CustomNumericDeltaCalculator : NumericDeltaCalculator() {
        override fun getDeltaFromRange(min: Comparable<*>?, max: Comparable<*>?, minorsPerMajor: Int, maxTicks: Int): IAxisDelta<Double> {
            // For the sake of simplicity, we will use simple constants.
            // But you might want to have some complex calculations here.
            val minorDelta = 0.07
            val majorDelta = 0.1
            return AxisDelta(minorDelta, majorDelta)
        }
    }
    
    // create a NumericTickProvider with CustomNumericDeltaCalculator and assign it to the axis
    val axis = NumericAxis(context)
    axis.tickProvider = NumericTickProvider(CustomNumericDeltaCalculator())
    

    Numeric Delta Calculator

    Creating your own TickProvider

    If you need to have fine grained control over axis ticks output, and custom Delta Calculator isn't enough - you might want to use tickProvider property, which is available for all Axis Types. To use it - you will need to provide a custom ITickProvider and set it on your axis.

    By default each axis has a ITickProvider created and assigned to it. The type of TickProvider depends on the type of Axis. Below is a table of the TickProviders already defined in SciChart Android.

    Tick Provider Type Provide ticks For
    NumericTickProvider NumericAxis
    LogarithmicNumericTickProvider LogarithmicNumericAxis
    DateTickProvider DateAxis
    CategoryTickProvider CategoryDateAxis

    To create a custom TickProvider, you need to inherit from the correct class, according to the Axis Type you have, and override updateTicks(DoubleValues minorTicks, DoubleValues majorTicks) method which is called internally for every axis when it needs to recalculate major and minor ticks for drawing.

    Let's create custom NumericTickProvider for NumericAxis

    • Java
    • Java with Builders API
    • Kotlin
    class CustomNumericTickProvider extends NumericTickProvider {
        @Override
        protected void updateTicks(DoubleValues minorTicks, DoubleValues majorTicks) {
            double min = axis.getVisibleRange().getMinAsDouble();
            double max = axis.getVisibleRange().getMaxAsDouble();
            majorTicks.add(min);
            majorTicks.add((min + max) / 2);
            majorTicks.add(max);
        }
    }
    
    final NumericAxis axis = new NumericAxis(getContext());
    // create a CustomNumericTickProvider and assign it to the axis
    axis.setTickProvider(new CustomNumericTickProvider());
    
    class CustomNumericTickProvider extends NumericTickProvider {
        @Override
        protected void updateTicks(DoubleValues minorTicks, DoubleValues majorTicks) {
            double min = axis.getVisibleRange().getMinAsDouble();
            double max = axis.getVisibleRange().getMaxAsDouble();
            majorTicks.add(min);
            majorTicks.add((min + max) / 2);
            majorTicks.add(max);
        }
    }
    
    final NumericAxis axis = sciChartBuilder.newNumericAxis().build();
    // create a CustomNumericTickProvider and assign it to the axis
    axis.setTickProvider(new CustomNumericTickProvider());
    
    class CustomNumericTickProvider : NumericTickProvider() {
        override fun updateTicks(minorTicks: DoubleValues, majorTicks: DoubleValues) {
            val min = axis.visibleRange.minAsDouble
            val max = axis.visibleRange.maxAsDouble
            majorTicks.add(min)
            majorTicks.add((min + max) / 2)
            majorTicks.add(max)
        }
    }
    
    val axis = NumericAxis(context)
    // create a CustomNumericTickProvider and assign it to the axis
    axis.tickProvider = CustomNumericTickProvider()
    

    Numeric Tick Provider

    Back to top © 2011-2025 SciChart. All rights reserved. | sitemap.xml