SciChart TradeChartAxisLabelProvider 自定义格式显示天、月
SciChart TradeChartAxisLabelProvider custom formatting to show days, months
如何自定义 X 轴以显示新 day/month/year 开始的日期(或月份或年份,取决于所选范围)?我正在使用 CategoryDateAxis
(CreateMultiPaneStockChartsFragment example)。
我想要的:
更大的范围:
更小的范围(很容易看出新的一天从哪里开始):
我有:
现在我使用的是默认标签提供程序,很难看到新 day/month/year 何时开始。例如。 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;
}
}
要实现 selection of label based on 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 并基于它 select 标签格式,然后使用 SimpleDateFormat 来格式化日期。
但据我了解,您的情况比这更复杂,因为您无法获得允许根据当前 VisibleRange 查看新 day/month/year 何时开始的标签。对于这种情况,您需要 select 根据先前格式化的值格式化字符串并跟踪 day/month/year 更改的时间。
如何自定义 X 轴以显示新 day/month/year 开始的日期(或月份或年份,取决于所选范围)?我正在使用 CategoryDateAxis
(CreateMultiPaneStockChartsFragment example)。
我想要的:
更大的范围:
更小的范围(很容易看出新的一天从哪里开始):
我有:
现在我使用的是默认标签提供程序,很难看到新 day/month/year 何时开始。例如。 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;
}
}
要实现 selection of label based on 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 并基于它 select 标签格式,然后使用 SimpleDateFormat 来格式化日期。
但据我了解,您的情况比这更复杂,因为您无法获得允许根据当前 VisibleRange 查看新 day/month/year 何时开始的标签。对于这种情况,您需要 select 根据先前格式化的值格式化字符串并跟踪 day/month/year 更改的时间。