gpt4 book ai didi

java - 是否可以在 Candlestick jfreechart 中显示空心蜡烛?

转载 作者:搜寻专家 更新时间:2023-11-01 03:21:39 24 4
gpt4 key购买 nike

大多数金融图表库都使用一些给蜡烛着色的规则:

  1. 如果close >= open , 那么蜡烛是 hollow (未填写)
  2. 如果close < open , 那么蜡烛是 filled
  3. 如果previous close <= close , 那么蜡烛是 green
  4. 如果previous close > close , 那么蜡烛是 red

jFreeChart 似乎有自己的着色规则,并且可以更改它们。默认情况下,图表看起来像这样(一些颜色已更改):

candlestick chart jfreechart

所以如您所见,可以更改蜡烛的颜色、背景、绘图边框等。

我想对图表实现规则 1 和 2(上文)。可能吗?

结果应该是这样的:

jfreechart ohlc candlestick

这是我当前在示例中使用的 CandlestickRenderer:

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

public CandlestickRenderer() {
setDrawVolume(false);
}

@Override
public Paint getItemPaint(int series, int item) {
OHLCDataset highLowData = (OHLCDataset)getPlot().getDataset(series);
Number curClose = highLowData.getClose(series, item);
Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);
if (prevClose.doubleValue() <= curClose.doubleValue()) {
return getUpPaint();
}
else {
return getDownPaint();
}
}
}

更新1

看来只要设置up paint就可以了至 black并绘制item pant颜色不变。

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

public CandlestickRenderer() {
setDrawVolume(false);
setUseOutlinePaint(false);
setUpPaint(Color.BLACK); // <-- rule 1. (plot is also black so it looks not filled)
}

@Override
public Paint getItemPaint(int series, int item) {
OHLCDataset highLowData = (OHLCDataset) getPlot().getDataset(series);
Number curClose = highLowData.getClose(series, item);
Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);

if (prevClose.doubleValue() <= curClose.doubleValue()) {
return Color.GREEN; // <-- rule 3.
} else {
return Color.RED; // <-- rule 4.
}
}

}

但主要问题仍然存在:

  • 这是什么意思 down (downPaint)up (upPaint)根据 jFreeChart?它比较prev. close vs. close吗值或 open vs. close ?

  • 在 jFreeChart 的源代码中,我可以在哪里找到 upPaint 的部分? & downPaint都用过吗?

现在这是奇怪的结果:

enter image description here

最佳答案

我已经做到了。这是完整的自己的渲染器:

public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {

private final Paint colorRaising = Color.GREEN;
private final Paint colorFalling = Color.RED;
private final Paint colorUnknown = Color.GRAY;
private final Paint colorTransparent = Color.BLACK;

public CandlestickRenderer() {
setDrawVolume(false);
setUpPaint(colorUnknown); // use unknown color if error
setDownPaint(colorUnknown); // use unknown color if error
}


@Override
public Paint getItemPaint(int series, int item) {
OHLCDataset highLowData = (OHLCDataset) getPlot().getDataset(series);
Number curClose = highLowData.getClose(series, item);
Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);

if (prevClose.doubleValue() <= curClose.doubleValue()) {
return Color.GREEN;
} else {
return Color.RED;
}
}


@Override
public void drawItem(Graphics2D g2, XYItemRendererState state,
Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
int series, int item, CrosshairState crosshairState, int pass) {

OHLCDataset highLowData = (OHLCDataset) dataset;
double yOpen = highLowData.getOpenValue(series, item);
double yClose = highLowData.getCloseValue(series, item);

// set color for filled candle
if (yClose >= yOpen) {
setUpPaint(colorRaising);
setDownPaint(colorFalling);
}

// set color for hollow (not filled) candle
else {
setUpPaint(colorTransparent);
setDownPaint(colorTransparent);
}

// call parent method
super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
}

}

结果图表:

enter image description here

如果您需要所有示例代码,请随时与我联系。

关于java - 是否可以在 Candlestick jfreechart 中显示空心蜡烛?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com