首页 > 解决方案 > SciChart TradeChartAxisLabelProvider 自定义格式以显示天、月

问题描述

如何自定义 X 轴以显示新日/月/年开始的日期(或月或年,基于所选范围)?我正在使用CategoryDateAxisCreateMultiPaneStockChartsFragment 示例)。

我想要的是:

更大的范围:

在此处输入图像描述

较小的范围(很容易看到新的一天从哪里开始):

在此处输入图像描述

我有的:

现在我正在使用默认标签提供程序,很难看到新的一天/月/年何时开始。例如 7 天范围:

在此处输入图像描述

轴是这样构造的:

final CategoryDateAxis xAxis = sciChartBuilder.newCategoryDateAxis()
                .withVisibility(isMainPane ? View.VISIBLE : View.GONE)
                .withVisibleRange(sharedXRange)
                //.withLabelProvider(new TradeChartAxisLabelProviderDateTime())
                .withGrowBy(0.01d, 0.01d)
                .build();

我如何实现这一目标?

public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
    public TradeChartAxisLabelProviderDateTime() {
        super();
    }

    @Override
    public String formatLabel(Comparable dataValue) {
        if(currentRange == RANGE_1_YEAR) {

        } else if(currentRange == RANGE_1_MONTH) {

        } else if(currentRange == RANGE_1_DAY) {

        }
        String text = super.formatLabel(dataValue).toString();
        return text;
    }
}

标签: androidscichart

解决方案


要实现基于 VisibleRange 的标签选择,您可以使用如下代码:

public static class TradeChartAxisLabelProviderDateTime extends TradeChartAxisLabelProvider {
    public TradeChartAxisLabelProviderDateTime() {
        super(new TradeChartAxisLabelFormatterDateTime());
    }

    private static class TradeChartAxisLabelFormatterDateTime implements ILabelFormatter<CategoryDateAxis> {
        private final SimpleDateFormat labelFormat, cursorLabelFormat;

        private TradeChartAxisLabelFormatterDateTime() {
            labelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
            cursorLabelFormat = new SimpleDateFormat(CategoryDateAxis.DEFAULT_TEXT_FORMATTING, Locale.getDefault());
        }

        @Override
        public void update(CategoryDateAxis axis) {
            final ICategoryLabelProvider labelProvider = Guard.instanceOfAndNotNull(axis.getLabelProvider(), ICategoryLabelProvider.class);

            // this is range of indices which are drawn by CategoryDateAxis
            final IRange<Double> visibleRange = axis.getVisibleRange();

            // convert indicies to range of dates
            final DateRange dateRange = new DateRange(
                    ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.floor(visibleRange.getMin()), 0, Integer.MAX_VALUE))),
                    ComparableUtil.toDate(labelProvider.transformIndexToData((int) NumberUtil.constrain(Math.ceil(visibleRange.getMax()), 0, Integer.MAX_VALUE))));

            if (dateRange.getIsDefined()) {
                long ticksInViewport = dateRange.getDiff().getTime();

                // select formatting based on diff in time between Min and Max
                if (ticksInViewport > DateIntervalUtil.fromYears(1)) {
                    // apply year formatting
                    labelFormat.applyPattern("");
                    cursorLabelFormat.applyPattern("");
                } else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
                    // apply month formatting
                    labelFormat.applyPattern("");
                    cursorLabelFormat.applyPattern("");
                } else if (ticksInViewport > DateIntervalUtil.fromMonths(1)) {
                    // apply day formatting
                    labelFormat.applyPattern("");
                    cursorLabelFormat.applyPattern("");
                }
            }
        }

        @Override
        public CharSequence formatLabel(Comparable dataValue) {
            final Date valueToFormat = ComparableUtil.toDate(dataValue);
            return labelFormat.format(valueToFormat);
        }

        @Override
        public CharSequence formatCursorLabel(Comparable dataValue) {
            final Date valueToFormat = ComparableUtil.toDate(dataValue);
            return cursorLabelFormat.format(valueToFormat);
        }
    }
}

update()中,您可以访问轴的 VisibleRange 并基于它选择标签格式,然后使用 SimpleDateFormat 格式化日期。

但是据我了解,您的情况比这更复杂,因为您无法获得允许根据当前 VisibleRange 查看新的日期/月份/年份何时开始的标签。对于这种情况,您需要根据之前格式化的值选择格式字符串,并跟踪日/月/年何时发生变化。


推荐阅读