Hi everyone,
I’m working on a 2D chart on Android that requires the distance between major axes to be exactly 5 millimetres. How do I configure the major axis delta/ticks or resize the chart to meet my need?
Thank you.
- Natasya Saai asked 4 years ago
- You must login to post comments
Hi Natasya,
Well you can create custom TickProvider with DeltaCalculator to calculate desired dimensions:
public class CustomNumericTickProvider extends NumericTickProvider {
private final CustomNumericDeltaCalculator deltaCalculator;
public CustomNumericTickProvider(float majorTickSizeMm) {
this(new CustomNumericDeltaCalculator(majorTickSizeMm));
}
public CustomNumericTickProvider(CustomNumericDeltaCalculator deltaCalculator){
super(deltaCalculator);
this.deltaCalculator = deltaCalculator;
}
@Override
public void attachTo(IServiceContainer services) {
super.attachTo(services);
deltaCalculator.axis = axis;
}
@Override
public void detach() {
deltaCalculator.axis = null;
super.detach();
}
public static class CustomNumericDeltaCalculator extends NumericDeltaCalculator {
private final float majorSizeInPixels;
private IAxisCore axis;
public CustomNumericDeltaCalculator(float majorSizeMm) {
// calculate how many device pixels are in 5mm on screen
majorSizeInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, majorSizeMm, Resources.getSystem().getDisplayMetrics());
}
@Override
protected IAxisDelta<Double> calculateDeltaInternal(double min, double max, int minorsPerMajor, int maxTicks) {
final ICoordinateCalculator coordCalc = axis.getCurrentCoordinateCalculator();
// calculate how many data representation of 5mm for current VisibleRange
final double doubleDelta = Math.abs(coordCalc.getDataValue(majorSizeInPixels) - coordCalc.getDataValue(0f));
return new AxisDelta<>(doubleDelta / minorsPerMajor, doubleDelta);
}
}
}
Then apply it for both X and Y axes:
xAxis.setTickProvider(new CustomNumericTickProvider(5f));
yAxis.setTickProvider(new CustomNumericTickProvider(5f));
Is this suitable for your needs?
Best regards,
Yura
- Yura Khariton answered 4 years ago
- You must login to post comments
Please login first to submit.