android - SciChart TradeChartAxisLabelProvider 自定义格式以显示天、月
问题描述
如何自定义 X 轴以显示新日/月/年开始的日期(或月或年,基于所选范围)?我正在使用CategoryDateAxis
(CreateMultiPaneStockChartsFragment 示例)。
我想要的是:
更大的范围:
较小的范围(很容易看到新的一天从哪里开始):
我有的:
现在我正在使用默认标签提供程序,很难看到新的一天/月/年何时开始。例如 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;
}
}
解决方案
要实现基于 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 查看新的日期/月份/年份何时开始的标签。对于这种情况,您需要根据之前格式化的值选择格式字符串,并跟踪日/月/年何时发生变化。
推荐阅读
- python - 如何测试从为 conda 构建 perl 模块的导入?
- javascript - PHP - 在回显语句中缺少 onClick 输出的输出
- excel - 通过单元格的值动态设置 Excel 图表数据系列范围
- c# - 无法使用 Visual Studio 2017 调试 Azure 函数
- javascript - 将表单提交推送到数组
- react-apollo - 如何通过道具而不是上下文提供 ApolloClient
- php - 当用户离开 Laravel-Echo 中的频道时,有没有办法触发后端事件?
- postgresql - Express PUT 端点函数不断返回 undefined
- ruby-on-rails - rspec / 无需重新加载即可测试 AR 对象
- r - 如何将具有多列的列表转换为R中的数据框