Example usage for org.jfree.chart LegendItemCollection add

List of usage examples for org.jfree.chart LegendItemCollection add

Introduction

In this page you can find the example usage for org.jfree.chart LegendItemCollection add.

Prototype

public void add(LegendItem item) 

Source Link

Document

Adds a legend item to the collection.

Usage

From source file:org.logisticPlanning.utils.graphics.chart.impl.jfree._JFCLineChart2D.java

/** process the lines */
private final void __processLines() {
    final LegendItemCollection legendCollection;
    final boolean addLegend;
    final ChartPalette palette;
    Stroke legendStroke;/*from   www.ja  v a  2  s . c  o m*/
    Color legendColor;
    int index;
    Line2D line;
    List<Line2D> lines;
    Iterator<Point2D> it;
    Point2D p;
    boolean foreground;
    XYSeries ser;
    String name;
    LegendItem item;

    lines = this.getBackgroundLines();
    ((_JFCXYLineAndShapeRenderer) (this.m_plot.getDrawingSupplier())).m_backgroundCount = lines.size();
    foreground = false;

    legendCollection = new LegendItemCollection();
    addLegend = (this.getLegendType() != ELegendType.NO_LEGEND);
    palette = this.getDriver().getPalette();

    // first add all foreground lines, than all background lines
    outer: for (;;) {
        index = 0;

        while (lines.size() > 0) {
            line = lines.remove(0);
            it = this.iterateLinePoints(line);

            // // are there any points to plot?
            // if (it.hasNext()) {
            // // then let's make a series and plot them!
            name = line.name();
            ser = new XYSeries(((name != null) ? name : EmptyUtils.EMPTY_STRING), false, true);

            while (it.hasNext()) {
                p = it.next();
                ser.add(_JFCLineChart2D.__format(p.getX()), _JFCLineChart2D.__format(p.getY()));
            }

            this.m_dataset.addSeries(ser);

            if (addLegend && (name != null) && (name.length() > 0)) {

                legendColor = (foreground ? //
                        palette.getForegroundDataColor(index)//
                        : palette.getBackgroundDataColor(index));
                legendStroke = (foreground ? //
                        palette.getForegroundDataStroke(index)//
                        : palette.getBackgroundDataStroke(index));

                item = new LegendItem(//
                        name, null, null, null, // text
                        false, _JFCLineChart2D.EMPTY_SHAPE, false, // shape
                        legendColor, //
                        false, //
                        legendColor, //
                        legendStroke, //
                        false, _JFCLineChart2D.EMPTY_SHAPE, // line
                        legendStroke, //
                        legendColor//
                );
                item.setLabelFont(palette.getLegendFont());
                item.setLabelPaint(legendColor);
                legendCollection.add(item);
            }
            index++;
            // }
        }

        if (foreground) {
            break outer;
        }
        foreground = true;
        lines = this.getLines();
    }

    this.m_plot.setFixedLegendItems(legendCollection);
}

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Returns the legend items for the plot.  Each legend item is generated by
 * the plot's renderer, since the renderer is responsible for the visual
 * representation of the data./*  w w  w  .j a va  2s. c o  m*/
 *
 * @return The legend items.
 */
public LegendItemCollection getLegendItems() {
    if (this.fixedLegendItems != null) {
        return this.fixedLegendItems;
    }
    LegendItemCollection result = new LegendItemCollection();
    int count = this.datasets.size();
    for (int datasetIndex = 0; datasetIndex < count; datasetIndex++) {
        XYDataset dataset = getDataset(datasetIndex);
        if (dataset != null) {
            XYItemRenderer renderer = getRenderer(datasetIndex);
            if (renderer == null) {
                renderer = getRenderer(0);
            }
            if (renderer != null) {
                int seriesCount = dataset.getSeriesCount();
                for (int i = 0; i < seriesCount; i++) {
                    if (renderer.isSeriesVisible(i) && renderer.isSeriesVisibleInLegend(i)) {
                        LegendItem item = renderer.getLegendItem(datasetIndex, i);
                        if (item != null) {
                            result.add(item);
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java

private void createCategoricalLegendItems(PlotInstance plotInstance, Set<PlotDimension> dimensionSet,
        LegendItemCollection legendItemCollection, Iterable<Double> values) {
    createDimensionTitleLegendItem(plotInstance, dimensionSet, legendItemCollection);

    PlotConfiguration plotConfig = plotInstance.getCurrentPlotConfigurationClone();

    Shape defaultShape = new Ellipse2D.Float(-5f, -5f, 10f, 10f);
    Color defaultOutlineColor = PlotConfiguration.DEFAULT_OUTLINE_COLOR;
    ColorProvider colorProvider = null;/*from  www  .  ja va  2  s  . c  o m*/
    ShapeProvider shapeProvider = null;
    SizeProvider sizeProvider = null;

    DefaultDimensionConfig dimensionConfig = (DefaultDimensionConfig) plotConfig
            .getDimensionConfig(dimensionSet.iterator().next());
    DimensionConfigData dimensionConfigData = plotInstance.getPlotData()
            .getDimensionConfigData(dimensionConfig);
    for (PlotDimension dimension : dimensionSet) {
        if (dimension == PlotDimension.COLOR) {
            colorProvider = dimensionConfigData.getColorProvider();
        } else if (dimension == PlotDimension.SHAPE) {
            shapeProvider = dimensionConfigData.getShapeProvider();
        } else if (dimension == PlotDimension.SIZE) {
            sizeProvider = dimensionConfigData.getSizeProvider();
        }
    }

    // initialize size scale for legend
    ContinuousSizeProvider legendSizeProvider = null;
    if (sizeProvider != null) {
        double minScalingFactor = sizeProvider.getMinScalingFactor();
        double maxScalingFactor = sizeProvider.getMaxScalingFactor();
        double minLegendScalingFactor = MIN_LEGEND_ITEM_SCALING_FACTOR;
        double maxLegendScalingFactor = MAX_LEGEND_ITEM_SCALING_FACTOR;
        if (minScalingFactor > maxScalingFactor) {
            double tmp = minScalingFactor;
            minScalingFactor = maxScalingFactor;
            maxScalingFactor = tmp;
            minLegendScalingFactor = MAX_LEGEND_ITEM_SCALING_FACTOR;
            maxLegendScalingFactor = MIN_LEGEND_ITEM_SCALING_FACTOR;
        }
        legendSizeProvider = new ContinuousSizeProvider(minScalingFactor, maxScalingFactor,
                minLegendScalingFactor, maxLegendScalingFactor, false);
    }

    for (Double value : values) {
        // configure shape and stroke
        Shape shape = defaultShape;
        BasicStroke outlineStroke;
        Color outlineColor = new Color(0, 0, 0, 0);
        if (shapeProvider != null) {
            shape = shapeProvider.getShapeForCategory(value);
            outlineStroke = DEFAULT_OUTLINE_STROKE;
            outlineColor = defaultOutlineColor;
        } else {
            outlineStroke = new BasicStroke();
            if (colorProvider != null) {
                shape = UNDEFINED_SHAPE;
            } else {
                shape = UNDEFINED_SHAPE_AND_COLOR;
            }
        }

        // configure fill paint
        Paint paint = UNDEFINED_COLOR_PAINT;
        if (colorProvider != null) {
            paint = colorProvider.getColorForValue(value);
        }

        double scalingFactor = 1;
        if (sizeProvider != null) {
            // scale shape according to sizeProvider
            scalingFactor = sizeProvider.getScalingFactorForValue(value);
            // scale shape to fit into legend
            scalingFactor = legendSizeProvider.getScalingFactorForValue(scalingFactor);
            AffineTransform transformation = new AffineTransform();
            transformation.scale(scalingFactor, scalingFactor);
            shape = transformation.createTransformedShape(shape);
        }

        String label = dimensionConfigData.getStringForValue(value);
        if (label == null) {
            label = "";
        }

        CustomLegendItem legendItem = new CustomLegendItem(label, null, null, null, shape, paint, outlineStroke,
                outlineColor);
        legendItemCollection.add(legendItem);
    }
}

From source file:edu.jhuapl.graphs.controller.GraphController.java

/**
 * The useItemColor is still under development to allow each bar to be a different color.
 *//*from www.  ja va 2  s  .  c  o  m*/
private double setDataSeries(GraphDataInterface graphData, String displayKey, boolean useNoDataColor,
        List<DataSeries> dataSeries, LegendItemCollection legendItems, boolean useItemColor) {
    double[][] counts = graphData.getCounts();
    int[][] colors = graphData.getColors();
    String[][] altTexts = graphData.getAltTexts();
    String[][] lineSetURLs = graphData.getLineSetURLs();
    String[] xLabels = graphData.getXLabels();
    String[] lineSetLabels = graphData.getLineSetLabels();
    boolean[] displayAlerts = graphData.displayAlerts();
    boolean[] displaySeverityAlerts = graphData.displaySeverityAlerts();
    double[] lineSymbolSizes = graphData.getLineSymbolSizes();
    Color[] graphBaseColors = graphData.getGraphBaseColors();
    int displayKeyIndex = 0;
    double maxCount = 0;
    boolean singleAlertLegend = graphData.getShowSingleAlertLegend();
    boolean singleSeverityLegend = graphData.getShowSingleSeverityLegend();

    for (int i = 0; i < counts.length; i++) {
        String lineSetLabel = "series" + (i + 1);
        boolean displayAlert = false;
        boolean displaySeverityAlert = false;
        double lineSymbolSize = 7.0;
        try {
            lineSetLabel = lineSetLabels[i];
        } catch (Exception e) {
        }
        try {
            displayAlert = displayAlerts[i];
        } catch (Exception e) {
        }
        try {
            displaySeverityAlert = displaySeverityAlerts[i];
        } catch (Exception e) {
        }
        try {
            lineSymbolSize = lineSymbolSizes[i];
        } catch (Exception e) {
        }

        double xy = lineSymbolSize / 2.0 * -1;
        Color seriesColor = graphBaseColors[i % graphBaseColors.length];
        boolean displayNormalData = displaySeries(displayKey, displayKeyIndex++) ? true : false;
        boolean displayWarningData = displayAlert && displaySeries(displayKey, displayKeyIndex++) ? true
                : false;
        boolean displayAlertData = displayAlert && displaySeries(displayKey, displayKeyIndex++) ? true : false;
        boolean displaySevereData = displaySeverityAlert && displaySeries(displayKey, displayKeyIndex++) ? true
                : false;
        List<DataPoint> points = new ArrayList<DataPoint>();

        /** get graph data */

        for (int j = 0; j < counts[i].length; j++) {
            boolean alertDataExists = false;
            String altText = null;
            String lineSetURL = null;
            int color = 1;
            try {
                altText = altTexts[i][j];
            } catch (Exception e) {
            }
            try {
                lineSetURL = lineSetURLs[i][j];
            } catch (Exception e) {
            }
            try {
                color = colors[i][j];
            } catch (Exception e) {
            }

            Map<String, Object> pointMetaData = new HashMap<String, Object>();
            pointMetaData.put(GraphSource.ITEM_TOOL_TIP, altText);
            pointMetaData.put(GraphSource.ITEM_URL, lineSetURL);
            pointMetaData.put(GraphSource.ITEM_SHAPE,
                    new Ellipse2D.Double(xy, xy, lineSymbolSize, lineSymbolSize));

            pointMetaData.put(GraphSource.ITEM_COLOR, seriesColor);

            // color 0 = GRAY (no data), color 2 = YELLOW (warning), 3 = RED (alert),
            // 4 = PURPLE (severe)
            if (useNoDataColor && color == 0) {
                pointMetaData.put(GraphSource.ITEM_COLOR, noDataColor);
            } else if (displayWarningData && color == 2) {
                alertDataExists = true;
                pointMetaData.put(GraphSource.ITEM_COLOR, warningDataColor);
            } else if (displayAlertData && color == 3) {
                alertDataExists = true;
                pointMetaData.put(GraphSource.ITEM_COLOR, alertDataColor);
            } else if (displaySevereData && color == 4) {
                alertDataExists = true;
                pointMetaData.put(GraphSource.ITEM_COLOR, severeDataColor);
            }

            if (useItemColor) {
                seriesColor = graphBaseColors[j % graphBaseColors.length];
                pointMetaData.put(GraphSource.ITEM_COLOR, seriesColor);
            }

            if (displayNormalData || alertDataExists) {
                // only update the maxCount if this data point is visible
                if (counts[i][j] > maxCount) {
                    maxCount = counts[i][j];
                }
            } else {
                // if normal data is supposed to be hidden and no alert data exists, then hide this
                // data point
                pointMetaData.put(GraphSource.ITEM_VISIBLE, false);
            }

            // if the data is set to the Double.MIN_VALUE, then add it as a null.
            if (counts[i][j] != Double.MIN_VALUE) {
                points.add(new DataPoint(counts[i][j], xLabels[j], pointMetaData));
            } else {
                points.add(new DataPoint(null, xLabels[j], pointMetaData));
            }

        }

        /** add the series */

        // series properties
        Map<String, Object> dataSeriesMetaData = new HashMap<String, Object>();
        dataSeriesMetaData.put(GraphSource.SERIES_TITLE, lineSetLabel);
        dataSeriesMetaData.put(GraphSource.SERIES_COLOR, seriesColor);
        // if normal data is hidden for this series, hide the series connector line
        dataSeriesMetaData.put(GraphSource.SERIES_LINES_VISIBLE, displayNormalData);
        dataSeries.add(new DataSeries(points, dataSeriesMetaData));

        if (legendItems != null) {
            if (displayNormalData && legendItems.getItemCount() < maxLegendItems) {
                if (displayAlert && !singleAlertLegend) {
                    legendItems
                            .add(new LegendItem(lineSetLabel + ": " + getTranslation("Normal"), seriesColor));
                } else {
                    legendItems.add(new LegendItem(lineSetLabel, seriesColor));
                }
            }
            if (!singleAlertLegend && displayWarningData && legendItems.getItemCount() < maxLegendItems) {
                legendItems
                        .add(new LegendItem(lineSetLabel + ": " + getTranslation("Warning"), warningDataColor));
            }
            if (!singleAlertLegend && displayAlertData && legendItems.getItemCount() < maxLegendItems) {
                legendItems.add(new LegendItem(lineSetLabel + ": " + getTranslation("Alert"), alertDataColor));
            }
            if (!singleSeverityLegend && displaySevereData && legendItems.getItemCount() < maxLegendItems) {
                legendItems
                        .add(new LegendItem(lineSetLabel + ": " + getTranslation("Severe"), severeDataColor));
            }
        }
    }

    if (singleAlertLegend && legendItems.getItemCount() < maxLegendItems) {
        legendItems.add(new LegendItem(getTranslation("Warning"), warningDataColor));
        legendItems.add(new LegendItem(getTranslation("Alert"), alertDataColor));
    }
    if (singleSeverityLegend && legendItems.getItemCount() < maxLegendItems) {
        legendItems.add(new LegendItem(getTranslation("Severe"), severeDataColor));
    }
    return maxCount;
}