大多数金融图表库都使用一些给蜡烛着色的规则:
- 如果
close >= open
, 那么蜡烛是hollow
(未填写) - 如果
close < open
, 那么蜡烛是filled
- 如果
previous close <= close
, 那么蜡烛是green
- 如果
previous close > close
, 那么蜡烛是red
jFreeChart 似乎有自己的着色规则,并且可以更改它们。默认情况下,图表看起来像这样(一些颜色已更改):
所以如您所见,可以更改蜡烛的颜色、背景、绘图边框等。
我想对图表实现规则 1 和 2(上文)。可能吗?
结果应该是这样的:
这是我当前在示例中使用的 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
都用过吗?
现在这是奇怪的结果:
最佳答案
我已经做到了。这是完整的自己的渲染器:
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);
}
}
结果图表:
如果您需要所有示例代码,请随时与我联系。
关于java - 是否可以在 Candlestick jfreechart 中显示空心蜡烛?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152688/