0

I am trying to plot an Rectangle annotation at the given X,Y coordinates. It does actually try to draw something at the coordninates as you can see in the image. But for some reason it just wont apply the set width and height. So i just get a small vertical line. Any help would be greatly appreciated!

enter image description here

public class Scatter {
static Graphics2D g2;
private static final BasicStroke stroke = new BasicStroke(2.0f);



public static void main(String[] args) throws ParseException {
    //Download caandles and store it in an OhlcSeriesCollection
    FetchCandleSticks fetch = new FetchCandleSticks();
    List<Candlestick> candles = fetch.getFourHourlyCandles("BTCUSDT", TimeStamps.weekAgo());
    OHLCSeriesCollection serie = createDataSet(candles);


    System.out.println(serie.getYValue(0, 22));
    System.out.println(candles.get(22).getClose());

    //Chart properties
    JFreeChart chart = ChartFactory.createCandlestickChart("CandleSticks", "X", "Y", serie, false);
    XYPlot plot = chart.getXYPlot();
    CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer();

    ///Create a shape and it on candle 15
     //Shape rect = new Rectangle.Double(serie.getX(0, 22).doubleValue(), 
      serie.getYValue(0, 22), 1000, 150);
    //plot.addAnnotation(new XYShapeAnnotation(rect, stroke, blue));

    //Add a shape on candle 22
     Rectangle2D rect=new Rectangle2D.Double(serie.getX(0, 22).doubleValue(), 
     serie.getYValue(0, 22),500,700);
    XYShapeAnnotation xyShape = (new XYShapeAnnotation(rect,stroke,Color.BLUE,Color.BLUE));
    plot.addAnnotation(xyShape,false);

    //Set time axis
    DateAxis timeAxis = (DateAxis) plot.getDomainAxis();
    timeAxis.setStandardTickUnits(DateAxis.createStandardDateTickUnits());
    timeAxis.setTickMarkPosition(DateTickMarkPosition.START);//sets label in middle
    timeAxis.setAutoRange(false);
    timeAxis.setTickLabelsVisible(true);
    timeAxis.setAutoTickUnitSelection(true);

    //Set price axis
    NumberAxis priceAxis = (NumberAxis) plot.getRangeAxis();
    priceAxis.setAutoRangeIncludesZero(false);

    //Misc
    plot.setDomainPannable(true);
    plot.setRangePannable(true);
    renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
    renderer.setDefaultSeriesVisible(true);

    displayChart(chart);
}

//Display chart
private static void displayChart(JFreeChart chart) {
    // Chart panel
    ChartPanel panel = new ChartPanel(chart);
    panel.setFillZoomRectangle(true);
    panel.setMouseWheelEnabled(true);
    panel.setPreferredSize(new java.awt.Dimension(1000, 700));

    // Application frame
    ApplicationFrame frame = new ApplicationFrame("Candlestick chart");
    frame.setContentPane(panel);
    frame.pack();

    frame.setVisible(true);
}


public static OHLCSeriesCollection createDataSet(List<Candlestick> candlesticks) {

    OHLCSeries s1 = new OHLCSeries("CandleSticks");

    for (Candlestick c : candlesticks) {
        Date date = new Date(c.getOpenTime());
        double open = convertToDouble(c.getOpen());
        double high = convertToDouble(c.getHigh());
        double low = convertToDouble(c.getLow());
        double close = convertToDouble(c.getClose());

        s1.add(RegularTimePeriod.createInstance(Minute.class, date, TimeZone.getTimeZone("CET"), Locale.ENGLISH), open, high, low, close);
    }

    OHLCSeriesCollection ohlcCollection = new OHLCSeriesCollection();
    ohlcCollection.addSeries(s1);

    return ohlcCollection;
}

private static double convertToDouble(String value) {
    return Double.parseDouble(value);
}

}

leroyv
  • 39
  • 4
  • 1
    Possible [duplicate](https://stackoverflow.com/a/56587266/230513). If this is not a duplicate, please [edit] your question to include a [mre] that shows your revised approach. – trashgod Nov 04 '21 at 19:57

0 Answers0