SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
I am working on CreateMultiPaneStockChartFragment example. Sometimes after I refresh data and call surface.zoomExtents(), the chart does not zoom to fit but stays the same. Why is this? I can then manually zoom to extents by double tapping the chart.
This is my (simplified) fragment code (stemming from the example but optimized for my case of regular updating) below. updateChart() is called periodically every 10 sec.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SciChartBuilder.init(getActivity());
sciChartBuilder = SciChartBuilder.instance();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
surface = view.findViewById(R.id.priceChart);
initChart();
}
private void initChart(){
final DefaultLayoutManager layoutManager = new DefaultLayoutManager.Builder().setRightOuterAxesLayoutStrategy(new RightAlignedOuterVerticallyStackedYAxisLayoutStrategy()).build();
surface.setLayoutManager(layoutManager);
final CategoryDateAxis xAxis = sciChartBuilder.newCategoryDateAxis()
.withLabelProvider(new TradeChartAxisLabelProviderDateTime())
.withAutoRangeMode(AutoRange.Once)
.build();
xAxis.setMinimalZoomConstrain(10d); // minimum data points
surface.getXAxes().add(xAxis);
final NumericAxis yAxisPrice = sciChartBuilder.newNumericAxis()
.withAxisId(PRICES)
.withAutoRangeMode(AutoRange.Always)
.withGrowBy(new DoubleRange(0.01d, 0.01d))
.withLabelProvider(new NumericLabelProviderPrice(coinCurrency))
.build();
yAxisPrice.setMinimalZoomConstrain(0d);
surface.getYAxes().add(yAxisPrice);
// add axes for MACD, RSI, VOL..
legendModifier = sciChartBuilder.newModifierGroup()
.withLegendModifier().withShowCheckBoxes(false).withShowSeriesMarkers(false).build()
.build();
// surface.getChartModifiers().add(legendModifier); // this causes crash; add it after series are present
interactionModifiers = sciChartBuilder.newModifierGroup()
.withPinchZoomModifier().withReceiveHandledEvents(true).withXyDirection(Direction2D.XDirection).build()
.withZoomPanModifier().withReceiveHandledEvents(true).withClipModeX(ClipMode.ClipAtExtents).build()
.withZoomExtentsModifier().withReceiveHandledEvents(true).build()
.build();
surface.getChartModifiers().add(interactionModifiers);
cursorModifier = sciChartBuilder.newModifierGroup()
.withCursorModifier().build()
.build();
cursorModifier.setIsEnabled(false);
surface.getChartModifiers().add(cursorModifier);
}
private void updateChart(int daysToShow) {
final PriceSeries priceData = getData(daysToShow);
surface.getRenderableSeries().clear();
surface.getAnnotations().clear();
// price chart
final OhlcDataSeries<Date, Double> stockPrices = sciChartBuilder.newOhlcDataSeries(Date.class, Double.class).withSeriesName(getString(R.string.price)).build();
stockPrices.append(series.getDateData(), series.getOpenData(), series.getHighData(), series.getLowData(), series.getCloseData());
final BaseRenderableSeries seriesOHLCV = sciChartBuilder.newCandlestickSeries().withDataSeries(stockPrices).withSeriesInfoProvider(new CustomOHLCSeriesInfoProvider()).withYAxisId(PRICES).build();
surface.getRenderableSeries().add(seriesOHLCV);
surface.getAnnotations().add(sciChartBuilder.newAxisMarkerAnnotation().withY1(stockPrices.getYValues().get(stockPrices.getCount()-1)).withBackgroundColor(Color.WHITE).withFontStyle(12, Color.BLACK).withYAxisId(PRICES).build());
// do the same for MACD, RSI, VOL ...
if(!surface.getChartModifiers().contains(legendModifier))
surface.getChartModifiers().add(legendModifier); // this causes crash if added before any series is present
if(!silent) {
Log.d("debug", "surface.zoomExtents()");
surface.zoomExtents(); // sometimes this does not work
}
}
Hi Primoz,
Without project which reproduce this issue it will be very hard to determine the cause of this issue because ZoomExtendsModifier uses zoomExtends() under the hood. So it’s probably some kind of race condition when you call zoomExtends() too soon when data isn’t updated yet or something like that.
Also it could be some exception so I would suggest you to check logcat output. Do you see any errors there?
Best regards,
Yura
Please login first to submit.