Search in sources :

Example 1 with XYZDataset

use of org.jfree.data.xy.XYZDataset in project pentaho-platform by pentaho.

the class BubbleRenderer 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 data 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 (horizontal) axis.
 * @param rangeAxis
 *          the range (vertical) axis.
 * @param dataset
 *          the dataset (an {@link XYZDataset} is expected).
 * @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(final Graphics2D g2, final XYItemRendererState state, final Rectangle2D dataArea, final PlotRenderingInfo info, final XYPlot plot, final ValueAxis domainAxis, final ValueAxis rangeAxis, final XYDataset dataset, final int series, final int item, final CrosshairState crosshairState, final int pass) {
    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 circleSize;
        circleSize = maxSize * (z / maxZ);
        circleSize = Math.abs(circleSize);
        Ellipse2D circle = null;
        if (orientation == PlotOrientation.VERTICAL) {
            circle = new Ellipse2D.Double(transX - circleSize / 2.0, transY - circleSize / 2.0, circleSize, circleSize);
        } else if (orientation == PlotOrientation.HORIZONTAL) {
            circle = new Ellipse2D.Double(transY - circleSize / 2.0, transX - circleSize / 2.0, circleSize, circleSize);
        }
        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);
            }
        }
        // setup for collecting optional entity info...
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }
        // 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);
            }
            String url = null;
            if (getURLGenerator() != null) {
                url = getURLGenerator().generateURL(dataset, series, item);
            }
            XYItemEntity entity = new XYItemEntity(circle, dataset, series, item, tip, url);
            entities.add(entity);
        }
        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x, y, domainAxisIndex, rangeAxisIndex, transX, transY, orientation);
    }
}
Also used : XYItemEntity(org.jfree.chart.entity.XYItemEntity) PlotOrientation(org.jfree.chart.plot.PlotOrientation) EntityCollection(org.jfree.chart.entity.EntityCollection) XYToolTipGenerator(org.jfree.chart.labels.XYToolTipGenerator) XYZDataset(org.jfree.data.xy.XYZDataset) Ellipse2D(java.awt.geom.Ellipse2D) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 2 with XYZDataset

use of org.jfree.data.xy.XYZDataset in project tdq-studio-se by Talend.

the class TopChartFactory method createBubbleChart.

/**
 * 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.
 *
 * 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) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(Messages.getString("TopChartFactory.argument"));
    }
    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
                // calculate the multiple of bubble's default size
                double zSize = getBubbleSize(z);
                // use bubble's default size
                z = 0;
                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;
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) NumberAxis(org.jfree.chart.axis.NumberAxis) PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) Rectangle2D(java.awt.geom.Rectangle2D) CrosshairState(org.jfree.chart.plot.CrosshairState) DefaultXYZDataset(org.jfree.data.xy.DefaultXYZDataset) XYZDataset(org.jfree.data.xy.XYZDataset) Ellipse2D(java.awt.geom.Ellipse2D) Paint(java.awt.Paint) JFreeChart(org.jfree.chart.JFreeChart) Graphics2D(java.awt.Graphics2D) StandardXYZURLGenerator(org.jfree.chart.urls.StandardXYZURLGenerator) XYBubbleRenderer(org.jfree.chart.renderer.xy.XYBubbleRenderer) XYPlot(org.jfree.chart.plot.XYPlot) EntityCollection(org.jfree.chart.entity.EntityCollection) ValueAxis(org.jfree.chart.axis.ValueAxis) XYItemRendererState(org.jfree.chart.renderer.xy.XYItemRendererState) XYDataset(org.jfree.data.xy.XYDataset) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) RectangleEdge(org.jfree.ui.RectangleEdge) StandardXYZToolTipGenerator(org.jfree.chart.labels.StandardXYZToolTipGenerator)

Example 3 with XYZDataset

use of org.jfree.data.xy.XYZDataset in project tdq-studio-se by Talend.

the class TopChartFactory method createBubbleChart.

/**
 * Method "createBubbleChart".
 *
 * @param indic the indicator
 * @param numericColumn the analyzed numeric column
 * @return the bubble chart
 */
public static JFreeChart createBubbleChart(String chartName, Object dataset, Map<String, ValueAggregator> xyzDatasets) {
    final Map<String, ValueAggregator> xyzDatasetsFinal = xyzDatasets;
    JFreeChart chart = TopChartFactory.createBubbleChart(chartName, // $NON-NLS-1$ //$NON-NLS-2$
    Messages.getString("TopChartFactory.average"), // $NON-NLS-1$ //$NON-NLS-2$
    Messages.getString("TopChartFactory.count"), // $NON-NLS-1$ //$NON-NLS-2$
    (DefaultXYZDataset) dataset, // $NON-NLS-1$ //$NON-NLS-2$
    PlotOrientation.HORIZONTAL, true, true, true);
    final XYPlot plot = (XYPlot) chart.getPlot();
    final XYItemRenderer renderer = plot.getRenderer();
    renderer.setBaseToolTipGenerator(new StandardXYZToolTipGenerator() {

        private static final long serialVersionUID = 1L;

        /*
             * (non-Javadoc)
             * 
             * @see org.jfree.chart.labels.StandardXYZToolTipGenerator#createItemArray(org.jfree.data.xy.XYZDataset,
             * int, int)
             */
        @Override
        protected Object[] createItemArray(XYZDataset dset, int series, int item) {
            final Comparable<?> seriesKey = dset.getSeriesKey(series);
            final String seriesK = String.valueOf(seriesKey);
            String label = seriesK;
            if (xyzDatasetsFinal != null) {
                ValueAggregator valueAggregator = xyzDatasetsFinal.get(seriesKey);
                label = valueAggregator.getLabels(seriesK).get(item);
            }
            final Object[] itemArray = super.createItemArray(dset, series, item);
            // label;
            itemArray[0] = label;
            // $NON-NLS-1$
            itemArray[1] = "avg=" + itemArray[1];
            // $NON-NLS-1$
            itemArray[2] = "record count=" + itemArray[2];
            // $NON-NLS-1$
            itemArray[3] = "null count=" + itemArray[3];
            return itemArray;
        }
    });
    return chart;
}
Also used : XYPlot(org.jfree.chart.plot.XYPlot) ValueAggregator(org.talend.dataprofiler.service.utils.ValueAggregator) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) JFreeChart(org.jfree.chart.JFreeChart) DefaultXYZDataset(org.jfree.data.xy.DefaultXYZDataset) XYZDataset(org.jfree.data.xy.XYZDataset) Paint(java.awt.Paint) StandardXYZToolTipGenerator(org.jfree.chart.labels.StandardXYZToolTipGenerator)

Aggregations

XYZDataset (org.jfree.data.xy.XYZDataset)3 Paint (java.awt.Paint)2 Ellipse2D (java.awt.geom.Ellipse2D)2 JFreeChart (org.jfree.chart.JFreeChart)2 EntityCollection (org.jfree.chart.entity.EntityCollection)2 StandardXYZToolTipGenerator (org.jfree.chart.labels.StandardXYZToolTipGenerator)2 PlotOrientation (org.jfree.chart.plot.PlotOrientation)2 XYPlot (org.jfree.chart.plot.XYPlot)2 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)2 DefaultXYZDataset (org.jfree.data.xy.DefaultXYZDataset)2 RectangleEdge (org.jfree.ui.RectangleEdge)2 Graphics2D (java.awt.Graphics2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 XYItemEntity (org.jfree.chart.entity.XYItemEntity)1 XYToolTipGenerator (org.jfree.chart.labels.XYToolTipGenerator)1 CrosshairState (org.jfree.chart.plot.CrosshairState)1 PlotRenderingInfo (org.jfree.chart.plot.PlotRenderingInfo)1 XYBubbleRenderer (org.jfree.chart.renderer.xy.XYBubbleRenderer)1