use of org.jfree.data.xy.IntervalXYDataset in project SIMVA-SoS by SESoS.
the class CandlestickRenderer method drawItem.
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @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 crosshairState crosshair information for the plot
* (<code>null</code> permitted).
* @param pass the pass index.
*/
@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) {
boolean horiz;
PlotOrientation orientation = plot.getOrientation();
if (orientation == PlotOrientation.HORIZONTAL) {
horiz = true;
} else if (orientation == PlotOrientation.VERTICAL) {
horiz = false;
} else {
return;
}
// setup for collecting optional entity info...
EntityCollection entities = null;
if (info != null) {
entities = info.getOwner().getEntityCollection();
}
OHLCDataset highLowData = (OHLCDataset) dataset;
double x = highLowData.getXValue(series, item);
double yHigh = highLowData.getHighValue(series, item);
double yLow = highLowData.getLowValue(series, item);
double yOpen = highLowData.getOpenValue(series, item);
double yClose = highLowData.getCloseValue(series, item);
RectangleEdge domainEdge = plot.getDomainAxisEdge();
double xx = domainAxis.valueToJava2D(x, dataArea, domainEdge);
RectangleEdge edge = plot.getRangeAxisEdge();
double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, edge);
double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, edge);
double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, edge);
double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, edge);
double volumeWidth;
double stickWidth;
if (this.candleWidth > 0) {
// These are deliberately not bounded to minimums/maxCandleWidth to
// retain old behaviour.
volumeWidth = this.candleWidth;
stickWidth = this.candleWidth;
} else {
double xxWidth = 0;
int itemCount;
switch(this.autoWidthMethod) {
case WIDTHMETHOD_AVERAGE:
itemCount = highLowData.getItemCount(series);
if (horiz) {
xxWidth = dataArea.getHeight() / itemCount;
} else {
xxWidth = dataArea.getWidth() / itemCount;
}
break;
case WIDTHMETHOD_SMALLEST:
// Note: It would be nice to pre-calculate this per series
itemCount = highLowData.getItemCount(series);
double lastPos = -1;
xxWidth = dataArea.getWidth();
for (int i = 0; i < itemCount; i++) {
double pos = domainAxis.valueToJava2D(highLowData.getXValue(series, i), dataArea, domainEdge);
if (lastPos != -1) {
xxWidth = Math.min(xxWidth, Math.abs(pos - lastPos));
}
lastPos = pos;
}
break;
case WIDTHMETHOD_INTERVALDATA:
IntervalXYDataset intervalXYData = (IntervalXYDataset) dataset;
double startPos = domainAxis.valueToJava2D(intervalXYData.getStartXValue(series, item), dataArea, plot.getDomainAxisEdge());
double endPos = domainAxis.valueToJava2D(intervalXYData.getEndXValue(series, item), dataArea, plot.getDomainAxisEdge());
xxWidth = Math.abs(endPos - startPos);
break;
}
xxWidth -= 2 * this.autoWidthGap;
xxWidth *= this.autoWidthFactor;
xxWidth = Math.min(xxWidth, this.maxCandleWidth);
volumeWidth = Math.max(Math.min(1, this.maxCandleWidth), xxWidth);
stickWidth = Math.max(Math.min(3, this.maxCandleWidth), xxWidth);
}
Paint p = getItemPaint(series, item);
Paint outlinePaint = null;
if (this.useOutlinePaint) {
outlinePaint = getItemOutlinePaint(series, item);
}
Stroke s = getItemStroke(series, item);
g2.setStroke(s);
if (this.drawVolume) {
int volume = (int) highLowData.getVolumeValue(series, item);
double volumeHeight = volume / this.maxVolume;
double min, max;
if (horiz) {
min = dataArea.getMinX();
max = dataArea.getMaxX();
} else {
min = dataArea.getMinY();
max = dataArea.getMaxY();
}
double zzVolume = volumeHeight * (max - min);
g2.setPaint(getVolumePaint());
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
if (horiz) {
g2.fill(new Rectangle2D.Double(min, xx - volumeWidth / 2, zzVolume, volumeWidth));
} else {
g2.fill(new Rectangle2D.Double(xx - volumeWidth / 2, max - zzVolume, volumeWidth, zzVolume));
}
g2.setComposite(originalComposite);
}
if (this.useOutlinePaint) {
g2.setPaint(outlinePaint);
} else {
g2.setPaint(p);
}
double yyMaxOpenClose = Math.max(yyOpen, yyClose);
double yyMinOpenClose = Math.min(yyOpen, yyClose);
double maxOpenClose = Math.max(yOpen, yClose);
double minOpenClose = Math.min(yOpen, yClose);
// draw the upper shadow
if (yHigh > maxOpenClose) {
if (horiz) {
g2.draw(new Line2D.Double(yyHigh, xx, yyMaxOpenClose, xx));
} else {
g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
}
}
// draw the lower shadow
if (yLow < minOpenClose) {
if (horiz) {
g2.draw(new Line2D.Double(yyLow, xx, yyMinOpenClose, xx));
} else {
g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
}
}
// draw the body
Rectangle2D body;
Rectangle2D hotspot;
double length = Math.abs(yyHigh - yyLow);
double base = Math.min(yyHigh, yyLow);
if (horiz) {
body = new Rectangle2D.Double(yyMinOpenClose, xx - stickWidth / 2, yyMaxOpenClose - yyMinOpenClose, stickWidth);
hotspot = new Rectangle2D.Double(base, xx - stickWidth / 2, length, stickWidth);
} else {
body = new Rectangle2D.Double(xx - stickWidth / 2, yyMinOpenClose, stickWidth, yyMaxOpenClose - yyMinOpenClose);
hotspot = new Rectangle2D.Double(xx - stickWidth / 2, base, stickWidth, length);
}
if (yClose > yOpen) {
if (this.upPaint != null) {
g2.setPaint(this.upPaint);
} else {
g2.setPaint(p);
}
g2.fill(body);
} else {
if (this.downPaint != null) {
g2.setPaint(this.downPaint);
} else {
g2.setPaint(p);
}
g2.fill(body);
}
if (this.useOutlinePaint) {
g2.setPaint(outlinePaint);
} else {
g2.setPaint(p);
}
g2.draw(body);
// add an entity for the item...
if (entities != null) {
addEntity(entities, hotspot, dataset, series, item, 0.0, 0.0);
}
}
use of org.jfree.data.xy.IntervalXYDataset in project SIMVA-SoS by SESoS.
the class StackedXYBarRenderer method drawItem.
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the area within which the plot is being drawn.
* @param info collects information 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 crosshairState crosshair information for the plot
* (<code>null</code> permitted).
* @param pass the pass index.
*/
@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) {
if (!getItemVisible(series, item)) {
return;
}
if (!(dataset instanceof IntervalXYDataset && dataset instanceof TableXYDataset)) {
String message = "dataset (type " + dataset.getClass().getName() + ") has wrong type:";
boolean and = false;
if (!IntervalXYDataset.class.isAssignableFrom(dataset.getClass())) {
message += " it is no IntervalXYDataset";
and = true;
}
if (!TableXYDataset.class.isAssignableFrom(dataset.getClass())) {
if (and) {
message += " and";
}
message += " it is no TableXYDataset";
}
throw new IllegalArgumentException(message);
}
IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
double value = intervalDataset.getYValue(series, item);
if (Double.isNaN(value)) {
return;
}
// if we are rendering the values as percentages, we need to calculate
// the total for the current item. Unfortunately here we end up
// repeating the calculation more times than is strictly necessary -
// hopefully I'll come back to this and find a way to add the
// total(s) to the renderer state. The other problem is we implicitly
// assume the dataset has no negative values...perhaps that can be
// fixed too.
double total = 0.0;
if (this.renderAsPercentages) {
total = DatasetUtilities.calculateStackTotal((TableXYDataset) dataset, item);
value = value / total;
}
double positiveBase = 0.0;
double negativeBase = 0.0;
for (int i = 0; i < series; i++) {
double v = dataset.getYValue(i, item);
if (!Double.isNaN(v) && isSeriesVisible(i)) {
if (this.renderAsPercentages) {
v = v / total;
}
if (v > 0) {
positiveBase = positiveBase + v;
} else {
negativeBase = negativeBase + v;
}
}
}
double translatedBase;
double translatedValue;
RectangleEdge edgeR = plot.getRangeAxisEdge();
if (value > 0.0) {
translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, edgeR);
translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, edgeR);
} else {
translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, edgeR);
translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, edgeR);
}
RectangleEdge edgeD = plot.getDomainAxisEdge();
double startX = intervalDataset.getStartXValue(series, item);
if (Double.isNaN(startX)) {
return;
}
double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, edgeD);
double endX = intervalDataset.getEndXValue(series, item);
if (Double.isNaN(endX)) {
return;
}
double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, edgeD);
double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
double translatedHeight = Math.abs(translatedValue - translatedBase);
if (getMargin() > 0.0) {
double cut = translatedWidth * getMargin();
translatedWidth = translatedWidth - cut;
translatedStartX = translatedStartX + cut / 2;
}
Rectangle2D bar = null;
PlotOrientation orientation = plot.getOrientation();
if (orientation == PlotOrientation.HORIZONTAL) {
bar = new Rectangle2D.Double(Math.min(translatedBase, translatedValue), Math.min(translatedEndX, translatedStartX), translatedHeight, translatedWidth);
} else if (orientation == PlotOrientation.VERTICAL) {
bar = new Rectangle2D.Double(Math.min(translatedStartX, translatedEndX), Math.min(translatedBase, translatedValue), translatedWidth, translatedHeight);
} else {
throw new IllegalStateException();
}
boolean positive = (value > 0.0);
boolean inverted = rangeAxis.isInverted();
RectangleEdge barBase;
if (orientation == PlotOrientation.HORIZONTAL) {
if (positive && inverted || !positive && !inverted) {
barBase = RectangleEdge.RIGHT;
} else {
barBase = RectangleEdge.LEFT;
}
} else {
if (positive && !inverted || !positive && inverted) {
barBase = RectangleEdge.BOTTOM;
} else {
barBase = RectangleEdge.TOP;
}
}
if (pass == 0) {
if (getShadowsVisible()) {
getBarPainter().paintBarShadow(g2, this, series, item, bar, barBase, false);
}
} else if (pass == 1) {
getBarPainter().paintBar(g2, this, series, item, bar, barBase);
// add an entity for the item...
if (info != null) {
EntityCollection entities = info.getOwner().getEntityCollection();
if (entities != null) {
addEntity(entities, bar, dataset, series, item, bar.getCenterX(), bar.getCenterY());
}
}
} else if (pass == 2) {
// been drawn...
if (isItemLabelVisible(series, item)) {
XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
drawItemLabel(g2, dataset, series, item, plot, generator, bar, value < 0.0);
}
}
}
use of org.jfree.data.xy.IntervalXYDataset in project SIMVA-SoS by SESoS.
the class IntervalXYItemLabelGenerator method createItemArray.
/**
* Creates the array of items that can be passed to the
* {@link MessageFormat} class for creating labels.
*
* @param dataset the dataset (<code>null</code> not permitted).
* @param series the series (zero-based index).
* @param item the item (zero-based index).
*
* @return An array of seven items from the dataset formatted as
* <code>String</code> objects (never <code>null</code>).
*/
@Override
protected Object[] createItemArray(XYDataset dataset, int series, int item) {
IntervalXYDataset intervalDataset = null;
if (dataset instanceof IntervalXYDataset) {
intervalDataset = (IntervalXYDataset) dataset;
}
Object[] result = new Object[7];
result[0] = dataset.getSeriesKey(series).toString();
double x = dataset.getXValue(series, item);
double xs = x;
double xe = x;
double y = dataset.getYValue(series, item);
double ys = y;
double ye = y;
if (intervalDataset != null) {
xs = intervalDataset.getStartXValue(series, item);
xe = intervalDataset.getEndXValue(series, item);
ys = intervalDataset.getStartYValue(series, item);
ye = intervalDataset.getEndYValue(series, item);
}
DateFormat xdf = getXDateFormat();
if (xdf != null) {
result[1] = xdf.format(new Date((long) x));
result[2] = xdf.format(new Date((long) xs));
result[3] = xdf.format(new Date((long) xe));
} else {
NumberFormat xnf = getXFormat();
result[1] = xnf.format(x);
result[2] = xnf.format(xs);
result[3] = xnf.format(xe);
}
NumberFormat ynf = getYFormat();
DateFormat ydf = getYDateFormat();
if (Double.isNaN(y) && dataset.getY(series, item) == null) {
result[4] = getNullYString();
} else {
if (ydf != null) {
result[4] = ydf.format(new Date((long) y));
} else {
result[4] = ynf.format(y);
}
}
if (Double.isNaN(ys) && intervalDataset != null && intervalDataset.getStartY(series, item) == null) {
result[5] = getNullYString();
} else {
if (ydf != null) {
result[5] = ydf.format(new Date((long) ys));
} else {
result[5] = ynf.format(ys);
}
}
if (Double.isNaN(ye) && intervalDataset != null && intervalDataset.getEndY(series, item) == null) {
result[6] = getNullYString();
} else {
if (ydf != null) {
result[6] = ydf.format(new Date((long) ye));
} else {
result[6] = ynf.format(ye);
}
}
return result;
}
use of org.jfree.data.xy.IntervalXYDataset in project SIMVA-SoS by SESoS.
the class CombinedXYPlotDemo1 method createCombinedChart.
/**
* Creates an overlaid chart.
*
* @return The chart.
*/
private static JFreeChart createCombinedChart() {
// create plot ...
IntervalXYDataset data1 = createDataset1();
XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
renderer1.setSeriesStroke(0, new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
renderer1.setSeriesPaint(0, Color.blue);
DateAxis domainAxis = new DateAxis("Year");
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.02);
ValueAxis rangeAxis = new NumberAxis("$billion");
XYPlot plot1 = new XYPlot(data1, null, rangeAxis, renderer1);
plot1.setBackgroundPaint(Color.lightGray);
plot1.setDomainGridlinePaint(Color.white);
plot1.setRangeGridlinePaint(Color.white);
// add a second dataset and renderer...
IntervalXYDataset data2 = createDataset2();
XYBarRenderer renderer2 = new XYBarRenderer() {
public Paint getItemPaint(int series, int item) {
XYDataset dataset = getPlot().getDataset();
if (dataset.getYValue(series, item) >= 0.0) {
return Color.red;
} else {
return Color.green;
}
}
};
renderer2.setSeriesPaint(0, Color.red);
renderer2.setDrawBarOutline(false);
renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("$billion"), renderer2);
plot2.setBackgroundPaint(Color.lightGray);
plot2.setDomainGridlinePaint(Color.white);
plot2.setRangeGridlinePaint(Color.white);
CombinedXYPlot cplot = new CombinedXYPlot(domainAxis, rangeAxis);
cplot.add(plot1, 3);
cplot.add(plot2, 2);
cplot.setGap(8.0);
cplot.setDomainGridlinePaint(Color.white);
cplot.setDomainGridlinesVisible(true);
// return a new chart containing the overlaid plot...
JFreeChart chart = new JFreeChart("CombinedXYPlotDemo1", JFreeChart.DEFAULT_TITLE_FONT, cplot, false);
chart.setBackgroundPaint(Color.white);
LegendTitle legend = new LegendTitle(cplot);
chart.addSubtitle(legend);
return chart;
}
use of org.jfree.data.xy.IntervalXYDataset in project SIMVA-SoS by SESoS.
the class XYBarRenderer method drawItem.
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the area within which the plot is being drawn.
* @param info collects information 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 crosshairState crosshair information for the plot
* (<code>null</code> permitted).
* @param pass the pass index.
*/
@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) {
if (!getItemVisible(series, item)) {
return;
}
IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
double value0;
double value1;
if (this.useYInterval) {
value0 = intervalDataset.getStartYValue(series, item);
value1 = intervalDataset.getEndYValue(series, item);
} else {
value0 = this.base;
value1 = intervalDataset.getYValue(series, item);
}
if (Double.isNaN(value0) || Double.isNaN(value1)) {
return;
}
if (value0 <= value1) {
if (!rangeAxis.getRange().intersects(value0, value1)) {
return;
}
} else {
if (!rangeAxis.getRange().intersects(value1, value0)) {
return;
}
}
double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, plot.getRangeAxisEdge());
double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, plot.getRangeAxisEdge());
double bottom = Math.min(translatedValue0, translatedValue1);
double top = Math.max(translatedValue0, translatedValue1);
double startX = intervalDataset.getStartXValue(series, item);
if (Double.isNaN(startX)) {
return;
}
double endX = intervalDataset.getEndXValue(series, item);
if (Double.isNaN(endX)) {
return;
}
if (startX <= endX) {
if (!domainAxis.getRange().intersects(startX, endX)) {
return;
}
} else {
if (!domainAxis.getRange().intersects(endX, startX)) {
return;
}
}
// is there an alignment adjustment to be made?
if (this.barAlignmentFactor >= 0.0 && this.barAlignmentFactor <= 1.0) {
double x = intervalDataset.getXValue(series, item);
double interval = endX - startX;
startX = x - interval * this.barAlignmentFactor;
endX = startX + interval;
}
RectangleEdge location = plot.getDomainAxisEdge();
double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);
double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
double left = Math.min(translatedStartX, translatedEndX);
if (getMargin() > 0.0) {
double cut = translatedWidth * getMargin();
translatedWidth = translatedWidth - cut;
left = left + cut / 2;
}
Rectangle2D bar = null;
PlotOrientation orientation = plot.getOrientation();
if (orientation == PlotOrientation.HORIZONTAL) {
// clip left and right bounds to data area
bottom = Math.max(bottom, dataArea.getMinX());
top = Math.min(top, dataArea.getMaxX());
bar = new Rectangle2D.Double(bottom, left, top - bottom, translatedWidth);
} else if (orientation == PlotOrientation.VERTICAL) {
// clip top and bottom bounds to data area
bottom = Math.max(bottom, dataArea.getMinY());
top = Math.min(top, dataArea.getMaxY());
bar = new Rectangle2D.Double(left, bottom, translatedWidth, top - bottom);
}
boolean positive = (value1 > 0.0);
boolean inverted = rangeAxis.isInverted();
RectangleEdge barBase;
if (orientation == PlotOrientation.HORIZONTAL) {
if (positive && inverted || !positive && !inverted) {
barBase = RectangleEdge.RIGHT;
} else {
barBase = RectangleEdge.LEFT;
}
} else {
if (positive && !inverted || !positive && inverted) {
barBase = RectangleEdge.BOTTOM;
} else {
barBase = RectangleEdge.TOP;
}
}
if (getShadowsVisible()) {
this.barPainter.paintBarShadow(g2, this, series, item, bar, barBase, !this.useYInterval);
}
this.barPainter.paintBar(g2, this, series, item, bar, barBase);
if (isItemLabelVisible(series, item)) {
XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
drawItemLabel(g2, dataset, series, item, plot, generator, bar, value1 < 0.0);
}
// update the crosshair point
double x1 = (startX + endX) / 2.0;
double y1 = dataset.getYValue(series, item);
double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1, plot.getOrientation());
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
addEntity(entities, bar, dataset, series, item, 0.0, 0.0);
}
}
Aggregations