Lets imagine that we have zoom feature and we want to format axis depend on user selected zoom. for example if year select yearly the pattern is yyyy MM dd and if selected zoom is day the pattern is something like this HH:mm:ss. how we can achieve to this goal?
- Vahid Garousi asked 1 year ago
- You must login to post comments
Unfortunately, this line( line 7 ) is never called.
and i can’t use https://www.scichart.com/documentation/android/current/articles/chart2d/axisAPIs/Axis%20Labels%20-%20Formatting%20for%20trading%20charts.html?tabs=kotlin because I am using IndexDateAxis
open class FinanceDateXAxis(context: Context?) : IndexDateAxis(DateRange(), NonClippingXAxisModifierSurface(context)) {
init {
// need to set empty string to use CalendarDateLabelFormatter
textFormattingProperty.setWeakValue(StringUtil.EMPTY)
cursorTextFormattingProperty.setWeakValue(StringUtil.EMPTY)
labelProvider = DateAxisLabelProvider()
this.setVisibleRangeChangeListener { axis, oldRange, newRange, isAnimating ->
Log.i("LOGGER","" + newRange.min) // this line
}
}
class NonClippingXAxisModifierSurface @JvmOverloads constructor(
context: Context?,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
) : AxisModifierSurface(context, attrs, defStyleAttr) {
override fun layoutChildAt(child: View?, left: Int, top: Int, right: Int, bottom: Int) {
when {
left < 0 -> {
super.layoutChildAt(child, 0, top, right - left, bottom)
}
right > width -> {
super.layoutChildAt(child, left - (right - width), top, width, bottom)
}
else -> {
super.layoutChildAt(child, left, top, right, bottom)
}
}
}
}
}
- Vahid Garousi answered 1 year ago
- last edited 4 months ago
- You must login to post comments
To make the axis label format range dependant, you need to create a custom labelFormatter. This gets a reference to the axis it is attached to, so you can look at the visible range when formatting the label. Here is a basic example which formats labels as 1.0K, 1.5K etc when the visible range is greater than 1000.
class RangeDependantLabelProvider extends LabelProviderBase<INumericAxis> {
RangeDependantLabelProvider() {
super(INumericAxis.class);
}
@Override
public CharSequence formatLabel(double dataValue) {
if (ComparableUtil.toDouble(this.axis.getVisibleRange().getDiff()) > 1000) {
return String.format("%.1fK", dataValue / 1000.0);
} else {
return String.format("%.0f", dataValue);
}
}
@Override
public CharSequence formatCursorLabel(double dataValue) {
return String.format("%.0f", dataValue);
}
@Override
public CharSequence formatLabel(Comparable dataValue) {
final double doubleValue = ComparableUtil.toDouble(dataValue);
return this.formatLabel(doubleValue);
}
@Override
public CharSequence formatCursorLabel(Comparable dataValue) {
final double doubleValue = ComparableUtil.toDouble(dataValue);
return formatCursorLabel(doubleValue);
}
}
Regards
David
- David Burleigh answered 1 year ago
- You must login to post comments
Hi Vahid
Check out our documentation on Android Charts – How to Listen to VisibleRange Changes
With this code:
val axis = NumericAxis(context)
axis.setVisibleRangeChangeListener { axis, oldRange, newRange, isAnimating ->
// TODO handle range changes here
}
You can be notified of axis visiblerange changes.
Also you might be interested in this article on Axis Formatting for trading charts. This shows a built-in way to dynamically change textformatting based on zoom.
- Andrew Burnett-Thompson answered 1 year ago
- last edited 4 months ago
- You must login to post comments
Please login first to submit.