1

I'm developing an app that displays daily financial data, and have chosen to use JFreeChart. I was able to learn how to create a candlestick chart, but my problem lies in customization.

You see, what I'm aiming for is more along the lines of

this

While, so far all I've been able to manage is

this.

No matter how far I zoom in, the candlesticks do not increase in width.

I'm fairly certain that somehow the thin candlesticks have something to do with being bound to a certain time range.. I've tried to remedy that but am not sure what I'm doing wrong here.

SSCE

    public void showStockHistory(OHLCDataset dataset, String stockName) {
    JFreeChart candleChart = ChartFactory.createCandlestickChart("History of " + stockName, "Date", "Stock Points", dataset, true);

    XYPlot plot = candleChart.getXYPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);

    ValueAxis domain = plot.getDomainAxis();
        domain.setAutoRange(true);

    NumberAxis range = (NumberAxis)plot.getRangeAxis();
        range.setUpperMargin(0.0D);
        range.setLowerMargin(0.0D);
        range.setAutoRange(true);
        range.setAutoRangeIncludesZero(false);

    ChartPanel chartPanel = new ChartPanel(candleChart);
        chartPanel.setMouseWheelEnabled(true);
        chartPanel.setMouseZoomable(true);

    getViewport().add(chartPanel);
    }
steelmonkey
  • 415
  • 2
  • 6
  • 19
  • 1
    Possible [duplicate](http://stackoverflow.com/q/18419795/230513) related to using a `SegmentedTimeline`. – trashgod Sep 18 '15 at 09:06
  • 1
    @trashgod I'm not quite sure if it is related to that. In my case, the chart only displays thin candles regardless of dragging movement or zoom depth. Not sure what to do here.. so maybe I'll try playing around with Timelines to see if any changes occur.. – steelmonkey Sep 18 '15 at 09:30
  • 1
    Dragging just makes it easier to see, as the width waxes and wanes. You might also experiment with `setAutoWidthMethod()`. – trashgod Sep 18 '15 at 11:00
  • @trashgod Thanks. Although its not exactly what I would call perfect, setting `setAutoWidthMethod()` to `CandlestickRenderer.WIDTHMETHOD_SMALLEST` more or less does fine considering the size of the data set I'm using. – steelmonkey Sep 18 '15 at 11:14
  • Sounds like it's not a duplicate; I would encourage you to [answer your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod Sep 18 '15 at 15:24

1 Answers1

3

Although my given example seems to have to no differing method calls from the demo in the first picture's code above, it nevertheless only shows thin candlesticks. I assume this to be some kind of bug.

However, I was able to rectify the issue as follows:

  1. getting the renderer for the chart,

  2. casting it to a type of CandlestickRenderer, and

  3. setting its setAutoWidthMethod() method to CandlestickRenderer.WIDTHMETHOD_SMALLEST.

This is how you do it:

JFreeChart candleChart = ChartFactory.createCandlestickChart(
    "History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer();
renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
Community
  • 1
  • 1
steelmonkey
  • 415
  • 2
  • 6
  • 19