List of usage examples for java.awt Graphics2D fill
public abstract void fill(Shape s);
From source file:org.talend.dataprofiler.chart.util.TopChartFactory.java
/** * Creates a bubble chart with default settings. The chart is composed of an {@link XYPlot}, with a {@link NumberAxis} for the * domain axis, a {@link NumberAxis} for the range axis, and an {@link XYBubbleRenderer} to draw the data items. * //from ww w. jav a 2 s.com * This method is copied from * {@link org.jfree.chart.ChartFactory#createBubbleChart(String, String, String, XYZDataset, PlotOrientation, boolean, boolean, boolean)} * * @param title the chart title (<code>null</code> permitted). * @param xAxisLabel a label for the X-axis (<code>null</code> permitted). * @param yAxisLabel a label for the Y-axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param orientation the orientation (horizontal or vertical) (<code>null</code> NOT permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A bubble chart. */ public static JFreeChart createBubbleChart(String title, String xAxisLabel, String yAxisLabel, XYZDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) { if (orientation == null) { throw new IllegalArgumentException(Messages.getString("TopChartFactory.argument")); //$NON-NLS-1$ } NumberAxis xAxis = new NumberAxis(xAxisLabel); xAxis.setAutoRangeIncludesZero(false); NumberAxis yAxis = new NumberAxis(yAxisLabel); yAxis.setAutoRangeIncludesZero(false); XYPlot plot = new XYPlot(dataset, xAxis, yAxis, null); XYItemRenderer renderer = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_RANGE_AXIS) { @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) { // return straight away if the item is not visible if (!getItemVisible(series, item)) { return; } PlotOrientation orientation = plot.getOrientation(); // get the data point... double x = dataset.getXValue(series, item); double y = dataset.getYValue(series, item); double z = Double.NaN; if (dataset instanceof XYZDataset) { XYZDataset xyzData = (XYZDataset) dataset; z = xyzData.getZValue(series, item); } if (!Double.isNaN(z)) { RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); double transX = domainAxis.valueToJava2D(x, dataArea, domainAxisLocation); double transY = rangeAxis.valueToJava2D(y, dataArea, rangeAxisLocation); double transDomain = 0.0; double transRange = 0.0; double zero; // MOD scorreia +2L avoid points: minimal size of circle must be 1 // z = z * transX + 1; // ADD xqliu 2009-07-06 bug 8035 double zSize = getBubbleSize(z); // calculate the multiple of bubble's default size z = 0; // use bubble's default size // ~ switch (getScaleType()) { case SCALE_ON_DOMAIN_AXIS: zero = domainAxis.valueToJava2D(0.0, dataArea, domainAxisLocation); transDomain = domainAxis.valueToJava2D(z, dataArea, domainAxisLocation) - zero; transRange = transDomain; break; case SCALE_ON_RANGE_AXIS: zero = rangeAxis.valueToJava2D(0.0, dataArea, rangeAxisLocation); transRange = zero - rangeAxis.valueToJava2D(z, dataArea, rangeAxisLocation); transDomain = transRange; break; default: double zero1 = domainAxis.valueToJava2D(0.0, dataArea, domainAxisLocation); double zero2 = rangeAxis.valueToJava2D(0.0, dataArea, rangeAxisLocation); transDomain = domainAxis.valueToJava2D(z, dataArea, domainAxisLocation) - zero1; transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea, rangeAxisLocation); } transDomain = Math.abs(transDomain); transRange = Math.abs(transRange); // MODSCA 2008-11-27 enlarge ellipse by diag% of the total diagonal double diag = Math.sqrt(dataArea.getHeight() * dataArea.getHeight() + dataArea.getWidth() * dataArea.getWidth()); transDomain += diag / 100; transRange += diag / 100; Ellipse2D circle = null; // ADD xqliu 2009-07-06 bug 8035 transDomain *= zSize; transRange *= zSize; // ~ if (orientation == PlotOrientation.VERTICAL) { circle = new Ellipse2D.Double(transX - transDomain / 2.0, transY - transRange / 2.0, transDomain, transRange); } else if (orientation == PlotOrientation.HORIZONTAL) { circle = new Ellipse2D.Double(transY - transRange / 2.0, transX - transDomain / 2.0, transRange, transDomain); } g2.setPaint(getItemPaint(series, item)); g2.fill(circle); g2.setStroke(getItemOutlineStroke(series, item)); g2.setPaint(getItemOutlinePaint(series, item)); g2.draw(circle); if (isItemLabelVisible(series, item)) { if (orientation == PlotOrientation.VERTICAL) { drawItemLabel(g2, orientation, dataset, series, item, transX, transY, false); } else if (orientation == PlotOrientation.HORIZONTAL) { drawItemLabel(g2, orientation, dataset, series, item, transY, transX, false); } } // add an entity if this info is being collected EntityCollection entities = null; if (info != null) { entities = info.getOwner().getEntityCollection(); if (entities != null && circle.intersects(dataArea)) { addEntity(entities, circle, dataset, series, item, circle.getCenterX(), circle.getCenterY()); } } int domainAxisIndex = plot.getDomainAxisIndex(domainAxis); int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis); updateCrosshairValues(crosshairState, x, y, domainAxisIndex, rangeAxisIndex, transX, transY, orientation); } } /** * DOC xqliu : calculate the size of bubble. for bug 8035 2009-07-06. * * @param z multiple of bubble's default size * @return */ private double getBubbleSize(double z) { if (z > 0 && z <= 10) { return 2; } else if (z > 10 && z <= 100) { return 3; } else if (z > 100) { return 4; } return 1; } }; if (tooltips) { renderer.setBaseToolTipGenerator(new StandardXYZToolTipGenerator()); } if (urls) { renderer.setURLGenerator(new StandardXYZURLGenerator()); } plot.setRenderer(renderer); plot.setOrientation(orientation); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
From source file:org.talend.dataprofiler.chart.preview.HideSeriesGanttRenderer.java
/** * Draws the tasks/subtasks for one item. * //from w ww .jav a 2 s . c o m * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the data plot area. * @param plot the plot. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the data. * @param row the row index (zero-based). * @param column the column index (zero-based). */ protected void drawTasks(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, GanttCategoryDataset dataset, int row, int column) { int count = dataset.getSubIntervalCount(row, column); if (count == 0 && getItemVisible(row, column)) { drawTask(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column); } for (int subinterval = 0; subinterval < count; subinterval++) { RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge(); // value 0 Number value0 = dataset.getStartValue(row, column, subinterval); if (value0 == null) { return; } double translatedValue0 = rangeAxis.valueToJava2D(value0.doubleValue(), dataArea, rangeAxisLocation); // value 1 Number value1 = dataset.getEndValue(row, column, subinterval); if (value1 == null) { return; } double translatedValue1 = rangeAxis.valueToJava2D(value1.doubleValue(), dataArea, rangeAxisLocation); if (translatedValue1 < translatedValue0) { double temp = translatedValue1; translatedValue1 = translatedValue0; translatedValue0 = temp; } double rectStart = calculateBarW0(plot, plot.getOrientation(), dataArea, domainAxis, state, row, column); double rectLength = Math.abs(translatedValue1 - translatedValue0); double rectBreadth = state.getBarWidth(); // DRAW THE BARS... Rectangle2D bar = null; if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { bar = new Rectangle2D.Double(translatedValue0, rectStart, rectLength, rectBreadth); } else if (plot.getOrientation() == PlotOrientation.VERTICAL) { bar = new Rectangle2D.Double(rectStart, translatedValue0, rectBreadth, rectLength); } Rectangle2D completeBar = null; Rectangle2D incompleteBar = null; Number percent = dataset.getPercentComplete(row, column, subinterval); double start = getStartPercent(); double end = getEndPercent(); if (percent != null) { double p = percent.doubleValue(); if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { completeBar = new Rectangle2D.Double(translatedValue0, rectStart + start * rectBreadth, rectLength * p, rectBreadth * (end - start)); incompleteBar = new Rectangle2D.Double(translatedValue0 + rectLength * p, rectStart + start * rectBreadth, rectLength * (1 - p), rectBreadth * (end - start)); } else if (plot.getOrientation() == PlotOrientation.VERTICAL) { completeBar = new Rectangle2D.Double(rectStart + start * rectBreadth, translatedValue0 + rectLength * (1 - p), rectBreadth * (end - start), rectLength * p); incompleteBar = new Rectangle2D.Double(rectStart + start * rectBreadth, translatedValue0, rectBreadth * (end - start), rectLength * (1 - p)); } } Paint seriesPaint = getItemPaint(row, column); g2.setPaint(seriesPaint); g2.fill(bar); if (completeBar != null) { g2.setPaint(getCompletePaint()); g2.fill(completeBar); } if (incompleteBar != null) { g2.setPaint(getIncompletePaint()); g2.fill(incompleteBar); } if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { g2.setStroke(getItemStroke(row, column)); g2.setPaint(getItemOutlinePaint(row, column)); g2.draw(bar); } // collect entity and tool tip information... if (state.getInfo() != null) { EntityCollection entities = state.getEntityCollection(); if (entities != null) { String tip = null; if (getToolTipGenerator(row, column) != null) { tip = getToolTipGenerator(row, column).generateToolTip(dataset, row, column); } String url = null; if (getItemURLGenerator(row, column) != null) { url = getItemURLGenerator(row, column).generateURL(dataset, row, column); } CategoryItemEntity entity = new CategoryItemEntity(bar, tip, url, dataset, row, dataset.getColumnKey(column), column); entities.add(entity); } } } }
From source file:extern.NpairsBoxAndWhiskerRenderer.java
/** * Draws the visual representation of a single data item when the plot has * a vertical orientation./* w w w. j a va 2 s. c om*/ * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the plot is being drawn. * @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 (must be an instance of * {@link BoxAndWhiskerCategoryDataset}). * @param row the row index (zero-based). * @param column the column index (zero-based). */ public void drawVerticalItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column) { BoxAndWhiskerCategoryDataset bawDataset = (BoxAndWhiskerCategoryDataset) dataset; double categoryEnd = domainAxis.getCategoryEnd(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); double categoryStart = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); double categoryWidth = categoryEnd - categoryStart; double xx = categoryStart; int seriesCount = getRowCount(); int categoryCount = getColumnCount(); double widthToUse = Math.min(state.getBarWidth(), dataArea.getWidth() / 15); if (seriesCount > 1) { double seriesGap = dataArea.getWidth() * getItemMargin() / (categoryCount * (seriesCount - 1)); double usedWidth = (widthToUse * seriesCount) + (seriesGap * (seriesCount - 1)); // offset the start of the boxes if the total width used is smaller // than the category width double offset = (categoryWidth - usedWidth) / 2; xx = xx + offset + (row * (widthToUse + seriesGap)); } else { // offset the start of the box if the box width is smaller than the // category width double offset = (categoryWidth - widthToUse) / 2; xx = xx + offset; } double yyAverage = 0.0; double yyOutlier; Paint itemPaint = getItemPaint(row, column); g2.setPaint(itemPaint); Stroke s = getItemStroke(row, column); g2.setStroke(s); double aRadius = 0; // average radius RectangleEdge location = plot.getRangeAxisEdge(); Number yQ1 = bawDataset.getQ1Value(row, column); Number yQ3 = bawDataset.getQ3Value(row, column); Number yMax = bawDataset.getMaxRegularValue(row, column); Number yMin = bawDataset.getMinRegularValue(row, column); Shape box = null; double xxmid = xx + widthToUse / 2.0; if (yQ1 != null && yQ3 != null && yMax != null && yMin != null) { double yyQ1 = rangeAxis.valueToJava2D(yQ1.doubleValue(), dataArea, location); double yyQ3 = rangeAxis.valueToJava2D(yQ3.doubleValue(), dataArea, location); double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(), dataArea, location); double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(), dataArea, location); // draw the upper shadow... g2.draw(new Line2D.Double(xxmid, yyMax, xxmid, yyQ3)); g2.draw(new Line2D.Double(xx, yyMax, xx + widthToUse, yyMax)); // draw the lower shadow... g2.draw(new Line2D.Double(xxmid, yyMin, xxmid, yyQ1)); g2.draw(new Line2D.Double(xx, yyMin, xx + widthToUse, yyMin)); // draw the body... box = new Rectangle2D.Double(xx, Math.min(yyQ1, yyQ3), widthToUse, Math.abs(yyQ1 - yyQ3)); if (this.fillBox) { g2.fill(box); } g2.setStroke(getItemOutlineStroke(row, column)); g2.setPaint(getItemOutlinePaint(row, column)); g2.draw(box); } g2.setPaint(this.artifactPaint); // draw mean - SPECIAL AIMS REQUIREMENT... Number yMean = bawDataset.getMeanValue(row, column); if (yMean != null && !Double.isNaN(yMean.doubleValue())) { yyAverage = rangeAxis.valueToJava2D(yMean.doubleValue(), dataArea, location); // THIS IS WHAT WE CHANGED // aRadius = widthWeWannaUse / 4; aRadius = 4; // here we check that the average marker will in fact be visible // before drawing it... // WE HAD TO CHANGE THIS TOO I DON'T KNOW WHAT THEY WERE DOING if ((yyAverage > (dataArea.getMinY() - aRadius)) && (yyAverage < (dataArea.getMaxY() + aRadius))) { Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xxmid - aRadius, yyAverage - aRadius, aRadius * 2, aRadius * 2); g2.fill(avgEllipse); g2.draw(avgEllipse); } } // draw median... Number yMedian = bawDataset.getMedianValue(row, column); if (yMedian != null && !Double.isNaN(yMedian.doubleValue())) { double yyMedian = rangeAxis.valueToJava2D(yMedian.doubleValue(), dataArea, location); g2.draw(new Line2D.Double(xx, yyMedian, xx + widthToUse, yyMedian)); // add a line to connect the means of the boxes if (prevX != -1.0 && prevY != -1.0) { g2.draw(new Line2D.Double(prevX, prevY, xxmid, yyMedian)); } prevX = xxmid; prevY = yyMedian; } // draw yOutliers... g2.setPaint(itemPaint); // draw outliers double oRadius = 4; // outlier radius List<Outlier> outliers = new ArrayList<Outlier>(); // From outlier array sort out which are outliers and put these into a // list If there are any farouts, set the flag on the // OutlierListCollection List<Number> yOutliers = bawDataset.getOutliers(row, column); if (yOutliers != null) { for (int i = 0; i < yOutliers.size(); i++) { double outlier = yOutliers.get(i).doubleValue(); yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea, location); outliers.add(new Outlier(xxmid, yyOutlier, oRadius)); } // Process outliers. Each outlier is either added to the // appropriate outlier list or a new outlier list is made for (Outlier outlier : outliers) { Point2D point = outlier.getPoint(); drawEllipse(point, oRadius, g2); } // for (Iterator iterator = outlierListCollection.iterator(); // iterator.hasNext();) { // OutlierList list = (OutlierList) iterator.next(); // Outlier outlier = list.getAveragedOutlier(); // Point2D point = outlier.getPoint(); // // if (list.isMultiple()) { // drawMultipleEllipse(point, widthWeWannaUse, oRadius, // g2); // } // else { // drawEllipse(point, oRadius, g2); // } // } // // // draw farout indicators // if (outlierListCollection.isHighFarOut()) { // drawHighFarOut(aRadius / 2.0, g2, // xx + widthWeWannaUse / 2.0, maxAxisValue); // } // // if (outlierListCollection.isLowFarOut()) { // drawLowFarOut(aRadius / 2.0, g2, // xx + widthWeWannaUse / 2.0, minAxisValue); // } // } // // collect entity and tool tip information... // if (state.getInfo() != null && box != null) { // EntityCollection entities = state.getEntityCollection(); // if (entities != null) { // addItemEntity(entities, dataset, row, column, box); // } } }
From source file:net.sf.maltcms.chromaui.annotations.PeakAnnotationRenderer.java
private void drawOutline(Shape entity, Graphics2D g2, Color fill, Color stroke, ChartPanel chartPanel, boolean scale, float alpha) { if (entity != null) { //System.out.println("Drawing entity with bbox: "+entity.getBounds2D()); Shape savedClip = g2.getClip(); Rectangle2D dataArea = chartPanel.getScreenDataArea(); Color c = g2.getColor();//from www .jav a 2s . c om Composite comp = g2.getComposite(); g2.clip(dataArea); g2.setColor(fill); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); JFreeChart chart = chartPanel.getChart(); XYPlot plot = (XYPlot) chart.getPlot(); ValueAxis xAxis = plot.getDomainAxis(); ValueAxis yAxis = plot.getRangeAxis(); RectangleEdge xAxisEdge = plot.getDomainAxisEdge(); RectangleEdge yAxisEdge = plot.getRangeAxisEdge(); Rectangle2D entityBounds = entity.getBounds2D(); double viewX = xAxis.valueToJava2D(entityBounds.getCenterX(), dataArea, xAxisEdge); double viewY = yAxis.valueToJava2D(entityBounds.getCenterY(), dataArea, yAxisEdge); double viewW = xAxis.lengthToJava2D(entityBounds.getWidth(), dataArea, xAxisEdge); double viewH = yAxis.lengthToJava2D(entityBounds.getHeight(), dataArea, yAxisEdge); PlotOrientation orientation = plot.getOrientation(); //transform model to origin (0,0) in model coordinates AffineTransform toOrigin = AffineTransform.getTranslateInstance(-entityBounds.getCenterX(), -entityBounds.getCenterY()); //transform from origin (0,0) to model location AffineTransform toModelLocation = AffineTransform.getTranslateInstance(entityBounds.getCenterX(), entityBounds.getCenterY()); //transform from model scale to view scale double scaleX = viewW / entityBounds.getWidth(); double scaleY = viewH / entityBounds.getHeight(); Logger.getLogger(getClass().getName()).log(Level.FINE, "Scale x: {0} Scale y: {1}", new Object[] { scaleX, scaleY }); AffineTransform toViewScale = AffineTransform.getScaleInstance(scaleX, scaleY); AffineTransform toViewLocation = AffineTransform.getTranslateInstance(viewX, viewY); AffineTransform flipTransform = AffineTransform.getScaleInstance(1.0f, -1.0f); AffineTransform modelToView = new AffineTransform(toOrigin); modelToView.preConcatenate(flipTransform); modelToView.preConcatenate(toViewScale); modelToView.preConcatenate(toViewLocation); // // if (orientation == PlotOrientation.HORIZONTAL) { // entity = ShapeUtilities.createTranslatedShape(entity, viewY, // viewX); // } else if (orientation == PlotOrientation.VERTICAL) { // entity = ShapeUtilities.createTranslatedShape(entity, viewX, // viewY); // } FlatteningPathIterator iter = new FlatteningPathIterator(modelToView.createTransformedShape(entity) .getPathIterator(AffineTransform.getTranslateInstance(0, 0)), 5); Path2D.Float path = new Path2D.Float(); path.append(iter, false); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); g2.fill(path); if (stroke != null) { g2.setColor(stroke); g2.draw(path); } g2.setComposite(comp); g2.setColor(c); g2.setClip(savedClip); } else { Logger.getLogger(getClass().getName()).info("Entity is null!"); } }
From source file:open.dolphin.impl.lbtest.IgnoreNullLineRenderer.java
/** * Draw a single data item./*from ww w .j a v a 2 s .c om*/ * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area in which the data is drawn. * @param plot the plot. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param row the row index (zero-based). * @param column the column index (zero-based). * @param pass the pass index. */ @Override public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) { // do nothing if item is not visible if (!getItemVisible(row, column)) { return; } // do nothing if both the line and shape are not visible if (!getItemLineVisible(row, column) && !getItemShapeVisible(row, column)) { return; } // nothing is drawn for null... Number v = dataset.getValue(row, column); if (v == null) { return; } int visibleRow = state.getVisibleSeriesIndex(row); if (visibleRow < 0) { return; } int visibleRowCount = state.getVisibleSeriesCount(); PlotOrientation orientation = plot.getOrientation(); // current data point... double x1; if (getUseSeriesOffset()) { x1 = domainAxis.getCategorySeriesMiddle(column, dataset.getColumnCount(), visibleRow, visibleRowCount, getItemMargin(), dataArea, plot.getDomainAxisEdge()); } else { x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); } double value = v.doubleValue(); double y1 = rangeAxis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge()); if (pass == 0 && getItemLineVisible(row, column)) { if (column != 0) { int prevColumn = getPreviousNotNullColumn(dataset, row, column - 1); Number previousValue = (prevColumn != -1) ? dataset.getValue(row, prevColumn) : null; if (previousValue != null) { // previous data point... double previous = previousValue.doubleValue(); double x0; if (getUseSeriesOffset()) { x0 = domainAxis.getCategorySeriesMiddle(prevColumn, dataset.getColumnCount(), visibleRow, visibleRowCount, getItemMargin(), dataArea, plot.getDomainAxisEdge()); } else { x0 = domainAxis.getCategoryMiddle(prevColumn, getColumnCount(), dataArea, plot.getDomainAxisEdge()); } double y0 = rangeAxis.valueToJava2D(previous, dataArea, plot.getRangeAxisEdge()); Line2D line = null; if (orientation == PlotOrientation.HORIZONTAL) { line = new Line2D.Double(y0, x0, y1, x1); } else if (orientation == PlotOrientation.VERTICAL) { line = new Line2D.Double(x0, y0, x1, y1); } g2.setPaint(getItemPaint(row, column)); g2.setStroke(getItemStroke(row, column)); g2.draw(line); } } } if (pass == 1) { Shape shape = getItemShape(row, column); if (orientation == PlotOrientation.HORIZONTAL) { shape = ShapeUtilities.createTranslatedShape(shape, y1, x1); } else if (orientation == PlotOrientation.VERTICAL) { shape = ShapeUtilities.createTranslatedShape(shape, x1, y1); } if (getItemShapeVisible(row, column)) { if (getItemShapeFilled(row, column)) { if (getUseFillPaint()) { g2.setPaint(getItemFillPaint(row, column)); } else { g2.setPaint(getItemPaint(row, column)); } g2.fill(shape); } if (getDrawOutlines()) { if (getUseOutlinePaint()) { g2.setPaint(getItemOutlinePaint(row, column)); } else { g2.setPaint(getItemPaint(row, column)); } g2.setStroke(getItemOutlineStroke(row, column)); g2.draw(shape); } } // draw the item label if there is one... if (isItemLabelVisible(row, column)) { if (orientation == PlotOrientation.HORIZONTAL) { drawItemLabel(g2, orientation, dataset, row, column, y1, x1, (value < 0.0)); } else if (orientation == PlotOrientation.VERTICAL) { drawItemLabel(g2, orientation, dataset, row, column, x1, y1, (value < 0.0)); } } // submit the current data point as a crosshair candidate int datasetIndex = plot.indexOf(dataset); updateCrosshairValues(state.getCrosshairState(), dataset.getRowKey(row), dataset.getColumnKey(column), value, datasetIndex, x1, y1, orientation); // add an item entity, if this information is being collected EntityCollection entities = state.getEntityCollection(); if (entities != null) { addItemEntity(entities, dataset, row, column, shape); } } }
From source file:gda.plots.SimplePlot.java
/** * Part of the implementation of MouseMotionListener - overrides the super class (ChartPanel) implementation so that * the mouse can be used to select a rectangle as well as for zooming. * /*from ww w .j a v a 2s.c o m*/ * @param e * the mouse event which caused the call */ @Override public void mouseDragged(MouseEvent e) { // If the rectangle dragger is not in operation then call the // super class method (to deal with any possible zooming) then // deal with magnifyingImage or magnifyingData. if (rd == null) { super.mouseDragged(e); if ((magnifyingImage || magnifyingData) && (e.getModifiers() & InputEvent.BUTTON3_MASK) == 0) { Graphics2D g2 = (Graphics2D) getGraphics(); g2.setXORMode(dragColour); if (magnifyRectangle != null) { if (magnifyRectangleIsNew) { magnifyRectangleIsNew = false; } else { g2.fill(magnifyRectangle); } } if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) { magnifyWidth = e.getX() - magnifyXPoint; magnifyHeight = e.getY() - magnifyYPoint; } recalculateMagnifyRectangle(e); if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) { magnifier.update(magnifyRectangle); } if (magnifyRectangle != null) { g2.fill(magnifyRectangle); } g2.dispose(); } } else { rd.mouseDragged(e); } }
From source file:org.trade.ui.chart.renderer.CandleRenderer.java
/** * Method drawItem.// w ww . j a v a 2s . c o m * * @param g2 * Graphics2D * @param state * XYItemRendererState * @param dataArea * Rectangle2D * @param info * PlotRenderingInfo * @param plot * XYPlot * @param domainAxis * ValueAxis * @param rangeAxis * ValueAxis * @param dataset * XYDataset * @param series * int * @param item * int * @param crosshairState * CrosshairState * @param pass * int * @see org.jfree.chart.renderer.xy.XYItemRenderer#drawItem(Graphics2D, * XYItemRendererState, Rectangle2D, PlotRenderingInfo, XYPlot, * ValueAxis, ValueAxis, XYDataset, int, int, CrosshairState, int) */ 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 (dataset instanceof OHLCVwapDataset) { // setup for collecting optional entity info... EntityCollection entities = null; if (info != null) { entities = info.getOwner().getEntityCollection(); } CandleDataset candleDataset = (CandleDataset) dataset; CandleItem candle = (CandleItem) candleDataset.getSeries(series).getDataItem(item); double startX = candle.getPeriod().getFirstMillisecond(); if (Double.isNaN(startX)) { return; } double endX = candle.getPeriod().getLastMillisecond(); if (Double.isNaN(endX)) { return; } if (startX <= endX) { if (!domainAxis.getRange().intersects(startX, endX)) { return; } } else { if (!domainAxis.getRange().intersects(endX, startX)) { return; } } 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)); if (getMargin() > 0.0) { double cut = translatedWidth * getMargin(); translatedWidth = translatedWidth - cut; } double x = candleDataset.getXValue(series, item); double yHigh = candleDataset.getHighValue(series, item); double yLow = candleDataset.getLowValue(series, item); double yOpen = candleDataset.getOpenValue(series, item); double yClose = candleDataset.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); Paint outlinePaint = null; outlinePaint = getItemOutlinePaint(series, item); g2.setStroke(getItemStroke(series, item)); g2.setPaint(outlinePaint); double yyMaxOpenClose = Math.max(yyOpen, yyClose); double yyMinOpenClose = Math.min(yyOpen, yyClose); double maxOpenClose = Math.max(yOpen, yClose); double minOpenClose = Math.min(yOpen, yClose); Shape body = null; boolean highlight = highlight(series, item); /********************************** * draw the upper shadow START **********************************/ if (yHigh > maxOpenClose) { if (highlight) { body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyHigh - 10, translatedWidth, (yyMaxOpenClose - yyHigh) + 10); g2.setPaint(Color.YELLOW); g2.fill(body); g2.draw(body); } } if (yHigh > maxOpenClose) { if (nightMode) { if (yClose > yOpen) { g2.setPaint(upPaint); } else { g2.setPaint(downPaint); } } else { g2.setPaint(Color.black); } g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose)); } /********************************** * draw the lower shadow START **********************************/ if (yLow < minOpenClose) { if (highlight) { body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyMinOpenClose, translatedWidth, (yyLow - yyMinOpenClose) + 10); g2.setPaint(Color.YELLOW); g2.fill(body); g2.draw(body); } if (yLow < minOpenClose) { if (nightMode) { if (yClose > yOpen) { g2.setPaint(upPaint); } else { g2.setPaint(downPaint); } } else { g2.setPaint(Color.BLACK); } g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose)); } } /********************************** * draw the body **********************************/ body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyMinOpenClose, translatedWidth, yyMaxOpenClose - yyMinOpenClose); if (nightMode) { g2.setPaint(Color.white); } else { if (yClose > yOpen) { g2.setPaint(upPaint); } else { g2.setPaint(downPaint); } } g2.fill(body); g2.draw(body); if (nightMode) { if (yClose > yOpen) { g2.setPaint(upPaint); } else { g2.setPaint(downPaint); } } else { g2.setPaint(outlinePaint); } g2.draw(body); // add an entity for the item... if (entities != null) { String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, null); entities.add(entity); } // update the cross hair point double x1 = dataset.getXValue(series, item); 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()); } }
From source file:ucar.unidata.idv.control.chart.MyScatterPlot.java
/** * Draws the fast scatter plot on a Java 2D graphics device (such as the * screen or a printer).//www . jav a2 s.com * a * @param g2 the graphics device. * @param dataArea the data area * @param index which data set * @param info collects chart drawing information (<code>null</code> * permitted). * @param crosshairState crosshairState * * @return did something */ public boolean render(Graphics2D g2, Rectangle2D dataArea, int index, PlotRenderingInfo info, CrosshairState crosshairState) { if (index >= series.size()) { return false; } XYDataset dataset = getDataset(index); g2.setStroke(new BasicStroke()); // getRendererForDataset(dataset).getSeriesStroke(0)); ScatterPlotChartWrapper.MyRenderer renderer = (ScatterPlotChartWrapper.MyRenderer) getRendererForDataset( dataset); g2.setPaint(renderer.getSeriesPaint(0)); int shape = renderer.shape; PlotOrientation orientation = getOrientation(); int seenCnt = 0; int xx = (int) dataArea.getMinX(); int ww = (int) dataArea.getWidth(); int yy = (int) dataArea.getMaxY(); int hh = (int) dataArea.getHeight(); ValueAxis rangeAxis = getRangeAxisForDataset(index); ValueAxis domainAxis = getDomainAxisForDataset(index); double domainMin = domainAxis.getLowerBound(); double domainLength = domainAxis.getUpperBound() - domainMin; double rangeMin = rangeAxis.getLowerBound(); double rangeLength = rangeAxis.getUpperBound() - rangeMin; int boxWidth = 6; double[][] data = (double[][]) series.get(index); double[] d1 = data[0]; double[] d2 = data[1]; int size = d1.length; Hashtable seen = new Hashtable(); int lastX = 0; int lastY = 0; //TODO: Check for clipping //TODO: Try to create a GeneralPath with the points //and cal g2.draw just once GeneralPath path = new GeneralPath(); long t1 = System.currentTimeMillis(); for (int i = 0; i < size; i++) { int transX = (int) (xx + ww * (d1[i] - domainMin) / domainLength); int transY = (int) (yy - hh * (d2[i] - rangeMin) / rangeLength); Object key = transX + "_" + transY; if (seen.get(key) != null) { seenCnt++; continue; } seen.put(key, key); if (crosshairState != null) { crosshairState.updateCrosshairPoint(d1[i], d2[i], transX, transY, orientation); } switch (shape) { case LineState.SHAPE_VLINE: if (i > 1) { g2.drawLine(lastX, lastY, transX, transY); } lastX = transX; lastY = transY; case LineState.SHAPE_POINT: path.append(new Rectangle((int) transX, (int) transY, 1, 1), false); break; case LineState.SHAPE_LARGEPOINT: path.append(new Rectangle((int) transX, (int) transY, 2, 2), false); break; case LineState.SHAPE_RECTANGLE: path.append( new Rectangle((int) transX - boxWidth / 2, (int) transY - boxWidth / 2, boxWidth, boxWidth), false); break; case LineState.SHAPE_X: g2.drawLine(transX - boxWidth / 2, transY - boxWidth / 2, transX + boxWidth - boxWidth / 2, transY + boxWidth - boxWidth / 2); g2.drawLine(transX + boxWidth - boxWidth / 2, transY - boxWidth / 2, transX - boxWidth / 2, transY + boxWidth - boxWidth / 2); break; case LineState.SHAPE_PLUS: g2.drawLine(transX + boxWidth / 2, transY, transX + boxWidth / 2, transY + boxWidth); g2.drawLine(transX, transY + boxWidth / 2, transX + boxWidth, transY + boxWidth / 2); break; } } g2.fill(path); long t2 = System.currentTimeMillis(); // System.out.println ("time:" + (t2-t1)); return true; }
From source file:edu.dlnu.liuwenpeng.render.BarRenderer.java
/** * Draws the bar for a single (series, category) data item. * /*from www. jav a 2 s . c o m*/ * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the data area. * @param plot the plot. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param row the row index (zero-based). * @param column the column index (zero-based). * @param pass the pass index. */ public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) { // nothing is drawn for null values... Number dataValue = dataset.getValue(row, column); if (dataValue == null) { return; } double value = dataValue.doubleValue(); PlotOrientation orientation = plot.getOrientation(); double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, row, column); double[] barL0L1 = calculateBarL0L1(value); if (barL0L1 == null) { return; // the bar is not visible } RectangleEdge edge = plot.getRangeAxisEdge(); double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge); double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge); // in the following code, barL0 is (in Java2D coordinates) the LEFT // end of the bar for a horizontal bar chart, and the TOP end of the // bar for a vertical bar chart. Whether this is the BASE of the bar // or not depends also on (a) whether the data value is 'negative' // relative to the base value and (b) whether or not the range axis is // inverted. This only matters if/when we apply the minimumBarLength // attribute, because we should extend the non-base end of the bar boolean positive = (value >= this.base); boolean inverted = rangeAxis.isInverted(); double barL0 = Math.min(transL0, transL1); double barLength = Math.abs(transL1 - transL0); double barLengthAdj = 0.0; if (barLength > 0.0 && barLength < getMinimumBarLength()) { barLengthAdj = getMinimumBarLength() - barLength; } double barL0Adj = 0.0; if (orientation == PlotOrientation.HORIZONTAL) { if (positive && inverted || !positive && !inverted) { barL0Adj = barLengthAdj; } } else { if (positive && !inverted || !positive && inverted) { barL0Adj = barLengthAdj; } } // draw the bar... Rectangle2D bar = null; if (orientation == PlotOrientation.HORIZONTAL) { bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0, barLength + barLengthAdj, state.getBarWidth()); } else { bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj, state.getBarWidth(), barLength + barLengthAdj); } Paint itemPaint = getItemPaint(row, column); if (dataset.getValue(row, column).doubleValue() >= 0) { itemPaint = Color.red; } else { itemPaint = Color.GREEN; } GradientPaintTransformer t = getGradientPaintTransformer(); if (t != null && itemPaint instanceof GradientPaint) { itemPaint = t.transform((GradientPaint) itemPaint, bar); } g2.setPaint(itemPaint); g2.fill(bar); // draw the outline... if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { Stroke stroke = getItemOutlineStroke(row, column); Paint paint = getItemOutlinePaint(row, column); if (dataset.getValue(row, column).doubleValue() >= 0) { paint = Color.red; } else { paint = Color.green; } if (stroke != null && paint != null) { g2.setStroke(stroke); g2.setPaint(paint); g2.draw(bar); } } CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column); if (generator != null && isItemLabelVisible(row, column)) { drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0)); } // add an item entity, if this information is being collected EntityCollection entities = state.getEntityCollection(); if (entities != null) { addItemEntity(entities, dataset, row, column, bar); } }
From source file:extern.PlotBySplitClassRenderer.java
/** * Draws the visual representation of a single data item when the plot has * a vertical orientation.//from ww w . j av a 2 s. c om * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the plot is being drawn. * @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 (must be an instance of * {@link BoxAndWhiskerCategoryDataset}). * @param row the row index (zero-based). * @param column the column index (zero-based). */ public void drawVerticalItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column) { if (lastSplitObj != column) { lastSplitObj = column; prevX = -1; prevY = -1; } BoxAndWhiskerCategoryDataset bawDataset = (BoxAndWhiskerCategoryDataset) dataset; double categoryEnd = domainAxis.getCategoryEnd(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); double categoryStart = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); double categoryWidth = categoryEnd - categoryStart; double xx = categoryStart; int seriesCount = getRowCount(); int categoryCount = getColumnCount(); double widthToUse = Math.min(state.getBarWidth(), dataArea.getWidth() / 15); if (seriesCount > 1) { double seriesGap = dataArea.getWidth() * getItemMargin() / (categoryCount * (seriesCount - 1)); double usedWidth = (widthToUse * seriesCount) + (seriesGap * (seriesCount - 1)); // offset the start of the boxes if the total width used is smaller // than the category width double offset = (categoryWidth - usedWidth) / 2; xx = xx + offset + (row * (widthToUse + seriesGap)); } else { // offset the start of the box if the box width is smaller than the // category width double offset = (categoryWidth - widthToUse) / 2; xx = xx + offset; } double yyAverage = 0.0; double yyOutlier; Paint itemPaint = getItemPaint(row, column); g2.setPaint(itemPaint); Stroke s = getItemStroke(row, column); g2.setStroke(s); double aRadius = 0; // average radius RectangleEdge location = plot.getRangeAxisEdge(); Number yQ1 = bawDataset.getQ1Value(row, column); Number yQ3 = bawDataset.getQ3Value(row, column); Number yMax = bawDataset.getMaxRegularValue(row, column); Number yMin = bawDataset.getMinRegularValue(row, column); Shape box = null; double xxmid = xx + widthToUse / 2.0; double yyQ1 = 0; double yyQ3 = 0; if (yQ1 != null && yQ3 != null && yMax != null && yMin != null) { yyQ1 = rangeAxis.valueToJava2D(yQ1.doubleValue(), dataArea, location); yyQ3 = rangeAxis.valueToJava2D(yQ3.doubleValue(), dataArea, location); double yyMax = rangeAxis.valueToJava2D(yMax.doubleValue(), dataArea, location); double yyMin = rangeAxis.valueToJava2D(yMin.doubleValue(), dataArea, location); // draw the upper shadow... g2.draw(new Line2D.Double(xxmid, yyMax, xxmid, yyQ3)); g2.draw(new Line2D.Double(xx, yyMax, xx + widthToUse, yyMax)); // draw the lower shadow... g2.draw(new Line2D.Double(xxmid, yyMin, xxmid, yyQ1)); g2.draw(new Line2D.Double(xx, yyMin, xx + widthToUse, yyMin)); // draw the body... box = new Rectangle2D.Double(xx, Math.min(yyQ1, yyQ3), widthToUse, Math.abs(yyQ1 - yyQ3)); if (this.fillBox) { g2.fill(box); } g2.setStroke(getItemOutlineStroke(row, column)); g2.setPaint(getItemOutlinePaint(row, column)); g2.draw(box); } g2.setPaint(this.artifactPaint); // draw mean - SPECIAL AIMS REQUIREMENT... Number yMean = bawDataset.getMeanValue(row, column); if (yMean != null && !Double.isNaN(yMean.doubleValue())) { yyAverage = rangeAxis.valueToJava2D(yMean.doubleValue(), dataArea, location); // THIS IS WHAT WE CHANGED // aRadius = widthWeWannaUse / 4; aRadius = 4; // here we check that the average marker will in fact be visible // before drawing it... // WE HAD TO CHANGE THIS TOO I DON'T KNOW WHAT THEY WERE DOING if ((yyAverage > (dataArea.getMinY() - aRadius)) && (yyAverage < (dataArea.getMaxY() + aRadius))) { Ellipse2D.Double avgEllipse = new Ellipse2D.Double(xxmid - aRadius, yyAverage - aRadius, aRadius * 2, aRadius * 2); //If this is a single point only, then draw this point with //the color of its class. if (yQ1 != null && yQ3 != null) { if (aRadius / 2 > Math.abs(yyQ3 - yyQ1)) { g2.setPaint(itemPaint); } } g2.fill(avgEllipse); g2.draw(avgEllipse); } } // draw median... Number yMedian = bawDataset.getMedianValue(row, column); if (yMedian != null && !Double.isNaN(yMedian.doubleValue())) { double yyMedian = rangeAxis.valueToJava2D(yMedian.doubleValue(), dataArea, location); g2.draw(new Line2D.Double(xx, yyMedian, xx + widthToUse, yyMedian)); // add a line to connect the means of the boxes if (prevX != -1 && prevY != -1) { g2.setPaint(artifactPaint); g2.draw(new Line2D.Double(prevX, prevY, xxmid, yyMedian)); } prevX = xxmid; prevY = yyMedian; } // draw yOutliers... g2.setPaint(itemPaint); // draw outliers double oRadius = 4; // outlier radius List<Outlier> outliers = new ArrayList<Outlier>(); // From outlier array sort out which are outliers and put these into a // list If there are any farouts, set the flag on the // OutlierListCollection List<Number> yOutliers = bawDataset.getOutliers(row, column); if (yOutliers != null) { for (int i = 0; i < yOutliers.size(); i++) { double outlier = yOutliers.get(i).doubleValue(); yyOutlier = rangeAxis.valueToJava2D(outlier, dataArea, location); outliers.add(new Outlier(xxmid, yyOutlier, oRadius)); } // Process outliers. Each outlier is either added to the // appropriate outlier list or a new outlier list is made for (Outlier outlier : outliers) { Point2D point = outlier.getPoint(); drawEllipse(point, oRadius, g2); } } }