📄 candlestickrenderer.java
字号:
Number yLow = highLowData.getLowValue(series, item);
Number yOpen = highLowData.getOpenValue(series, item);
Number yClose = highLowData.getCloseValue(series, item);
double xx = domainAxis.translateValueToJava2D(x.doubleValue(), dataArea,
plot.getDomainAxisEdge());
RectangleEdge edge = plot.getRangeAxisEdge();
double yyHigh = rangeAxis.translateValueToJava2D(yHigh.doubleValue(), dataArea, edge);
double yyLow = rangeAxis.translateValueToJava2D(yLow.doubleValue(), dataArea, edge);
double yyOpen = rangeAxis.translateValueToJava2D(yOpen.doubleValue(), dataArea, edge);
double yyClose = rangeAxis.translateValueToJava2D(yClose.doubleValue(), dataArea, edge);
double exactCandleWidth = candleWidth;
double thisCandleWidth = candleWidth;
if (candleWidth <= 0.0) {
int itemCount = highLowData.getItemCount(series);
exactCandleWidth = (dataArea.getHeight()) / itemCount * 4.5 / 7;
if (exactCandleWidth < 1) {
exactCandleWidth = 1;
}
thisCandleWidth = exactCandleWidth;
if (thisCandleWidth < 3) {
thisCandleWidth = 3;
}
}
Paint p = getItemPaint(series, item);
Stroke s = getItemStroke(series, item);
g2.setStroke(s);
if (drawVolume) {
int volume = highLowData.getVolumeValue(series, item).intValue();
int maxVolume = 1;
int maxCount = highLowData.getItemCount(series);
for (int i = 0; i < maxCount; i++) {
int thisVolume = highLowData.getVolumeValue(series, i).intValue();
if (thisVolume > maxVolume) {
maxVolume = thisVolume;
}
}
double volumeHeight = volume / (double) maxVolume;
double minX = dataArea.getMinX();
double maxX = dataArea.getMaxX();
double xxVolume = volumeHeight * (maxX - minX);
g2.setPaint(Color.gray);
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2.fill(new Rectangle2D.Double(minX,
xx - exactCandleWidth / 2,
xxVolume, exactCandleWidth));
g2.setComposite(originalComposite);
}
g2.setPaint(p);
// draw the upper shadow
if ((yyHigh > yyOpen) && (yyHigh > yyClose)) {
g2.draw(new Line2D.Double(yyHigh, xx, Math.max(yyOpen, yyClose), xx));
}
// draw the lower shadow
if ((yyLow < yyOpen) && (yyLow < yyClose)) {
g2.draw(new Line2D.Double(yyLow, xx, Math.min(yyOpen, yyClose), xx));
}
// draw the body
Shape body = null;
if (yyOpen < yyClose) {
body = new Rectangle2D.Double(yyOpen, xx - thisCandleWidth / 2,
yyClose - yyOpen, thisCandleWidth);
if (upPaint != null) {
g2.setPaint(upPaint);
g2.fill(body);
}
g2.setPaint(p);
g2.draw(body);
}
else {
body = new Rectangle2D.Double(yyClose, xx - thisCandleWidth / 2,
yyOpen - yyClose, thisCandleWidth);
if (downPaint != null) {
g2.setPaint(downPaint);
}
g2.fill(body);
g2.setPaint(p);
g2.draw(body);
}
// add an entity for the item...
if (entities != null) {
String tip = null;
if (getToolTipGenerator() != null) {
tip = getToolTipGenerator().generateToolTip(dataset, series, item);
}
String url = null;
if (getURLGenerator() != null) {
url = getURLGenerator().generateURL(dataset, series, item);
}
XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, url);
entities.addEntity(entity);
}
}
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param dataArea the area within which the plot is being drawn.
* @param info collects info about the drawing.
* @param plot the plot (can be used to obtain standard color information etc).
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param series the series index (zero-based).
* @param item the item index (zero-based).
* @param crosshairInfo information about crosshairs on a plot.
* @param pass the pass index.
*/
public void drawVerticalItem(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info,
XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
XYDataset dataset, int series, int item,
CrosshairInfo crosshairInfo,
int pass) {
// setup for collecting optional entity info...
EntityCollection entities = null;
if (info != null) {
entities = info.getEntityCollection();
}
HighLowDataset highLowData = (HighLowDataset) dataset;
Number x = highLowData.getXValue(series, item);
Number yHigh = highLowData.getHighValue(series, item);
Number yLow = highLowData.getLowValue(series, item);
Number yOpen = highLowData.getOpenValue(series, item);
Number yClose = highLowData.getCloseValue(series, item);
double xx = domainAxis.translateValueToJava2D(x.doubleValue(), dataArea,
plot.getDomainAxisEdge());
RectangleEdge edge = plot.getRangeAxisEdge();
double yyHigh = rangeAxis.translateValueToJava2D(yHigh.doubleValue(), dataArea, edge);
double yyLow = rangeAxis.translateValueToJava2D(yLow.doubleValue(), dataArea, edge);
double yyOpen = rangeAxis.translateValueToJava2D(yOpen.doubleValue(), dataArea, edge);
double yyClose = rangeAxis.translateValueToJava2D(yClose.doubleValue(), dataArea, edge);
double exactCandleWidth = candleWidth;
double thisCandleWidth = candleWidth;
if (candleWidth <= 0.0) {
int itemCount = highLowData.getItemCount(series);
exactCandleWidth = (dataArea.getMaxX() - dataArea.getMinX()) / itemCount * 4.5 / 7;
if (exactCandleWidth < 1) {
exactCandleWidth = 1;
}
thisCandleWidth = exactCandleWidth;
if (thisCandleWidth < 3) {
thisCandleWidth = 3;
}
}
Paint p = getItemPaint(series, item);
Stroke s = getItemStroke(series, item);
g2.setStroke(s);
if (drawVolume) {
int volume = highLowData.getVolumeValue(series, item).intValue();
int maxVolume = 1;
int maxCount = highLowData.getItemCount(series);
for (int i = 0; i < maxCount; i++) {
int thisVolume = highLowData.getVolumeValue(series, i).intValue();
if (thisVolume > maxVolume) {
maxVolume = thisVolume;
}
}
double volumeHeight = volume / (double) maxVolume;
double minY = dataArea.getMinY();
double maxY = dataArea.getMaxY();
double yyVolume = volumeHeight * (maxY - minY);
g2.setPaint(Color.gray);
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2.fill(new Rectangle2D.Double(xx - exactCandleWidth / 2,
maxY - yyVolume, exactCandleWidth, yyVolume));
g2.setComposite(originalComposite);
}
g2.setPaint(p);
// draw the upper shadow
if ((yyHigh < yyOpen) && (yyHigh < yyClose)) {
g2.draw(new Line2D.Double(xx, yyHigh, xx,
Math.min(yyOpen, yyClose)));
}
// draw the lower shadow
if ((yyLow > yyOpen) && (yyLow > yyClose)) {
g2.draw(new Line2D.Double(xx, yyLow, xx,
Math.max(yyOpen, yyClose)));
}
// draw the body
Shape body = null;
if (yyOpen > yyClose) {
body = new Rectangle2D.Double(xx - thisCandleWidth / 2, yyClose,
thisCandleWidth, yyOpen - yyClose);
if (upPaint != null) {
g2.setPaint(upPaint);
g2.fill(body);
}
g2.setPaint(p);
g2.draw(body);
}
else {
body = new Rectangle2D.Double(xx - thisCandleWidth / 2, yyOpen,
thisCandleWidth, yyClose - yyOpen);
if (downPaint != null) {
g2.setPaint(downPaint);
}
g2.fill(body);
g2.setPaint(p);
g2.draw(body);
}
// add an entity for the item...
if (entities != null) {
String tip = null;
if (getToolTipGenerator() != null) {
tip = getToolTipGenerator().generateToolTip(dataset, series, item);
}
String url = null;
if (getURLGenerator() != null) {
url = getURLGenerator().generateURL(dataset, series, item);
}
XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, url);
entities.addEntity(entity);
}
}
/**
* Tests this renderer for equality with another object.
*
* @param obj the object.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof CandlestickRenderer) {
CandlestickRenderer renderer = (CandlestickRenderer) obj;
boolean result = super.equals(obj);
result = result && (this.candleWidth == renderer.getCandleWidth());
result = result && (this.upPaint.equals(renderer.getUpPaint()));
result = result && (this.downPaint.equals(renderer.getDownPaint()));
result = result && (this.drawVolume == renderer.drawVolume);
return result;
}
return false;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writePaint(this.upPaint, stream);
SerialUtilities.writePaint(this.downPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.upPaint = SerialUtilities.readPaint(stream);
this.downPaint = SerialUtilities.readPaint(stream);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -