0

I have an app and I somehow successfully managed to get a candlestick chart visible. However, once the chart is displayed, it shows all the candles (can be several hundred) which causes the chart to be very tiny and zoomed out. What I actually want is, the chart to be zoomed into the range of the last N candles so it looks more visible to the eye.

Here is how my charts look once they get created enter image description here

I tried working with setRange, the Range on the Date Axis returns values in the format of [09.02.2023, 23:21:02 --> 10.02.2023, 08:29:56]

I dont seem to get how to make the chart like zoom in to a range of for example range.length-100 -> range.length. Whatever I tried, I always end up somewhere randomly without any data really shown.

Is there a way to define the last n entries on the range and set the dateAxis to start showing this range?

What I actually want to see after creation is something like this enter image description here

 private  void displayChart(String title, String quoteMarket, BarSeries series) {
    OHLCDataset dataSet = createOHLCDataset(title,series);
    JFreeChart chart = ChartFactory.createCandlestickChart(
          title,
            "Time",
            quoteMarket,
            dataSet,
            false);
    chart.setBackgroundPaint(new Color(28, 39, 57));
    chart.setBorderVisible(false);
    chart.getTitle().setPaint(Color.WHITE);
    // Candlestick rendering
    CandlestickRenderer renderer = new CandlestickRenderer();
    renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_INTERVALDATA);
    renderer.setUpPaint(new Color(0, 214, 110));
    renderer.setDownPaint(new Color(196, 19, 30));
    renderer.setSeriesPaint(0,new Color(211, 211, 211));
    renderer.setCandleWidth(3.0);
    XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    plot.setRangeGridlinePaint(Color.lightGray);
    plot.setBackgroundPaint(new Color(28, 39, 57));
    plot.setDomainGridlinesVisible( false );
    plot.setRangeGridlinesVisible( false );

    NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
    numberAxis.setAutoRangeIncludesZero(false);
    numberAxis.setLabelPaint(Color.WHITE);
    numberAxis.setTickLabelPaint(Color.WHITE);
    numberAxis.setAutoRangeIncludesZero(false);


    DateAxis dateAxis = (DateAxis) plot.getDomainAxis();

    dateAxis.setAutoRange(true);
    dateAxis.setLabelPaint(Color.WHITE);
    dateAxis.setTickLabelPaint(Color.WHITE);
    dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    ChartPanel panel = new ChartPanel(chart);

    panel.setMouseWheelEnabled(true);
    panel.setMouseZoomable(true);
    panel.setRangeZoomable(true);
    panel.setDomainZoomable(true);
    this.add(panel);

    System.out.println(dateAxis.getRange());
}
BitQueen
  • 585
  • 1
  • 6
  • 20
  • Try `setRange()`, for [example](https://stackoverflow.com/a/67230096/230513). – trashgod Feb 09 '23 at 13:35
  • But what would the range be? – BitQueen Feb 09 '23 at 21:23
  • The range you choose, typically based on values in your dataset; if you have trouble, please [edit] your question to include a [mre] that shows your revised approach with a small subset of data. – trashgod Feb 09 '23 at 22:44
  • I'm not sure where your code goes awry; the example cited also uses dates; I don't see your sample data or call to `setRange()`. – trashgod Feb 10 '23 at 13:44
  • A related example is seen [here](https://stackoverflow.com/q/75726661/230513). – trashgod Mar 16 '23 at 22:53

0 Answers0