Example usage for org.jfree.chart.title LegendTitle setItemPaint

List of usage examples for org.jfree.chart.title LegendTitle setItemPaint

Introduction

In this page you can find the example usage for org.jfree.chart.title LegendTitle setItemPaint.

Prototype

public void setItemPaint(Paint paint) 

Source Link

Document

Sets the item paint.

Usage

From source file:net.sf.jasperreports.components.spiderchart.SpiderChartDesignEvaluator.java

/**
 * //from w w w  .j a  v  a  2  s  . c  o m
 */
public static JRRenderable evaluateRenderer(ReportConverter reportConverter, JRComponentElement element) {
    SpiderChartComponent chartComponent = (SpiderChartComponent) element.getComponent();
    ChartSettings chartSettings = chartComponent.getChartSettings();
    SpiderPlot plot = chartComponent.getPlot();

    SpiderWebPlot spiderWebPlot = new SpiderWebPlot(getSampleDataset());

    if (plot.getAxisLineColor() != null) {
        spiderWebPlot.setAxisLinePaint(plot.getAxisLineColor());
    }
    if (plot.getAxisLineWidth() != null) {
        spiderWebPlot.setAxisLineStroke(new BasicStroke(plot.getAxisLineWidth()));
    }
    if (plot.getBackcolor() != null) {
        spiderWebPlot.setBackgroundPaint(plot.getBackcolor());
    }
    if (plot.getBackgroundAlpha() != null) {
        spiderWebPlot.setBackgroundAlpha(plot.getBackgroundAlpha());
    }
    if (plot.getForegroundAlpha() != null) {
        spiderWebPlot.setForegroundAlpha(plot.getForegroundAlpha());
    }
    if (plot.getHeadPercent() != null) {
        spiderWebPlot.setHeadPercent(plot.getHeadPercent());
    }
    if (plot.getInteriorGap() != null) {
        spiderWebPlot.setInteriorGap(plot.getInteriorGap());
    }
    if (plot.getLabelColor() != null) {
        spiderWebPlot.setLabelPaint(plot.getLabelColor());
    }
    if (plot.getLabelFont() != null) {
        spiderWebPlot.setLabelFont(JRFontUtil.getAwtFont(plot.getLabelFont(), Locale.getDefault()));
    }
    if (plot.getLabelGap() != null) {
        spiderWebPlot.setAxisLabelGap(plot.getLabelGap());
    }

    spiderWebPlot.setMaxValue(MAX_VALUE);

    if (plot.getRotation() != null) {
        spiderWebPlot.setDirection(plot.getRotation().getRotation());
    }
    if (plot.getStartAngle() != null) {
        spiderWebPlot.setStartAngle(plot.getStartAngle());
    }
    if (plot.getTableOrder() != null) {
        spiderWebPlot.setDataExtractOrder(plot.getTableOrder().getOrder());
    }
    if (plot.getWebFilled() != null) {
        spiderWebPlot.setWebFilled(plot.getWebFilled());
    }

    spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
    spiderWebPlot.setLabelGenerator(new StandardCategoryItemLabelGenerator());

    String titleText = JRExpressionUtil.getExpressionText(chartSettings.getTitleExpression());

    Font titleFont = chartSettings.getTitleFont() != null
            ? JRFontUtil.getAwtFont(chartSettings.getTitleFont(), Locale.getDefault())
            : TextTitle.DEFAULT_FONT;

    JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true);

    if (chartSettings.getBackcolor() != null) {
        jfreechart.setBackgroundPaint(chartSettings.getBackcolor());
    }

    RectangleEdge titleEdge = getEdge(chartSettings.getTitlePosition(), RectangleEdge.TOP);

    if (titleText != null) {
        TextTitle title = jfreechart.getTitle();
        title.setText(titleText);
        if (chartSettings.getTitleColor() != null) {
            title.setPaint(chartSettings.getTitleColor());
        }

        title.setFont(titleFont);
        title.setPosition(titleEdge);
        jfreechart.setTitle(title);
    }

    String subtitleText = JRExpressionUtil.getExpressionText(chartSettings.getSubtitleExpression());
    if (subtitleText != null) {
        TextTitle subtitle = new TextTitle(subtitleText);
        subtitle.setText(subtitleText);
        if (chartSettings.getSubtitleColor() != null) {
            subtitle.setPaint(chartSettings.getSubtitleColor());
        }

        if (chartSettings.getSubtitleColor() != null) {
            Font subtitleFont = chartSettings.getSubtitleFont() != null
                    ? JRFontUtil.getAwtFont(chartSettings.getSubtitleFont(), Locale.getDefault())
                    : TextTitle.DEFAULT_FONT;
            subtitle.setFont(subtitleFont);
        }

        subtitle.setPosition(titleEdge);

        jfreechart.addSubtitle(subtitle);
    }

    // Apply all of the legend formatting options
    LegendTitle legend = jfreechart.getLegend();
    if (legend != null) {
        legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend()));
        if (legend.isVisible()) {
            if (chartSettings.getLegendColor() != null) {
                legend.setItemPaint(chartSettings.getLegendColor());
            }
            if (chartSettings.getLegendBackgroundColor() != null) {
                legend.setBackgroundPaint(chartSettings.getLegendBackgroundColor());
            }

            if (chartSettings.getLegendFont() != null) {
                legend.setItemFont(JRFontUtil.getAwtFont(chartSettings.getLegendFont(), Locale.getDefault()));
            }
            legend.setPosition(getEdge(chartSettings.getLegendPosition(), RectangleEdge.BOTTOM));
        }
    }

    Rectangle2D rectangle = new Rectangle2D.Double(0, 0, element.getWidth(), element.getHeight());

    String renderType = chartSettings.getRenderType();
    if (renderType == null) {
        renderType = JRProperties.getProperty(reportConverter.getReport(), JRChart.PROPERTY_CHART_RENDER_TYPE);
    }

    return ChartUtil.getChartRendererFactory(renderType).getRenderer(jfreechart, null, rectangle);
}

From source file:net.sf.jasperreports.components.spiderchart.SpiderChartRendererEvaluator.java

/**
 * /*w  w  w  .j a va2s .  com*/
 */
public static Renderable evaluateRenderable(JasperReportsContext jasperReportsContext,
        JRComponentElement element, SpiderChartSharedBean spiderchartBean, ChartCustomizer chartCustomizer,
        String defaultRenderType, String datasetType) {
    SpiderChartComponent chartComponent = (SpiderChartComponent) element.getComponent();
    ChartSettings chartSettings = chartComponent.getChartSettings();
    SpiderPlot plot = (SpiderPlot) chartComponent.getPlot();

    DefaultCategoryDataset dataset = null;
    StandardCategoryItemLabelGenerator labelGenerator = null;

    if (FILL_DATASET.equals(datasetType)) {
        dataset = ((FillSpiderDataset) spiderchartBean.getDataset()).getCustomDataset();
        labelGenerator = ((FillSpiderDataset) spiderchartBean.getDataset()).getLabelGenerator();
    } else {
        dataset = getSampleDataset();
        labelGenerator = new StandardCategoryItemLabelGenerator();
    }

    SpiderWebPlot spiderWebPlot = new SpiderWebPlot(dataset);

    if (plot.getAxisLineColor() != null) {
        spiderWebPlot.setAxisLinePaint(plot.getAxisLineColor());
    }
    if (plot.getAxisLineWidth() != null) {
        spiderWebPlot.setAxisLineStroke(new BasicStroke(plot.getAxisLineWidth()));
    }
    if (plot.getBackcolor() != null) {
        spiderWebPlot.setBackgroundPaint(plot.getBackcolor());
    }
    if (plot.getBackgroundAlpha() != null) {
        spiderWebPlot.setBackgroundAlpha(plot.getBackgroundAlpha());
    }
    if (plot.getForegroundAlpha() != null) {
        spiderWebPlot.setForegroundAlpha(plot.getForegroundAlpha());
    }
    if (plot.getHeadPercent() != null) {
        spiderWebPlot.setHeadPercent(plot.getHeadPercent());
    }
    if (plot.getInteriorGap() != null) {
        spiderWebPlot.setInteriorGap(plot.getInteriorGap());
    }
    if (plot.getLabelColor() != null) {
        spiderWebPlot.setLabelPaint(plot.getLabelColor());
    }
    if (plot.getLabelFont() != null) {
        spiderWebPlot.setLabelFont(FontUtil.getInstance(jasperReportsContext).getAwtFont(plot.getLabelFont(),
                Locale.getDefault()));
    }
    if (plot.getLabelGap() != null) {
        spiderWebPlot.setAxisLabelGap(plot.getLabelGap());
    }
    if (spiderchartBean.getMaxValue() != null) {
        spiderWebPlot.setMaxValue(spiderchartBean.getMaxValue());
    }
    if (plot.getRotation() != null) {
        spiderWebPlot.setDirection(plot.getRotation().getRotation());
    }
    if (plot.getStartAngle() != null) {
        spiderWebPlot.setStartAngle(plot.getStartAngle());
    }
    if (plot.getTableOrder() != null) {
        spiderWebPlot.setDataExtractOrder(plot.getTableOrder().getOrder());
    }
    if (plot.getWebFilled() != null) {
        spiderWebPlot.setWebFilled(plot.getWebFilled());
    }

    spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
    spiderWebPlot.setLabelGenerator(labelGenerator);

    Font titleFont = chartSettings.getTitleFont() != null ? FontUtil.getInstance(jasperReportsContext)
            .getAwtFont(chartSettings.getTitleFont(), Locale.getDefault()) : TextTitle.DEFAULT_FONT;

    String titleText = spiderchartBean.getTitleText();

    JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true);

    Color backcolor = chartSettings.getBackcolor() != null ? chartSettings.getBackcolor()
            : element.getBackcolor();
    if (backcolor != null) {
        jfreechart.setBackgroundPaint(backcolor);
    }

    RectangleEdge titleEdge = getEdge(chartSettings.getTitlePosition(), RectangleEdge.TOP);

    if (titleText != null) {
        TextTitle title = jfreechart.getTitle();
        title.setText(titleText);
        if (chartSettings.getTitleColor() != null) {
            title.setPaint(chartSettings.getTitleColor());
        }

        title.setFont(titleFont);
        title.setPosition(titleEdge);
        jfreechart.setTitle(title);
    }

    String subtitleText = spiderchartBean.getSubtitleText();
    if (subtitleText != null) {
        TextTitle subtitle = new TextTitle(subtitleText);
        subtitle.setText(subtitleText);
        if (chartSettings.getSubtitleColor() != null) {
            subtitle.setPaint(chartSettings.getSubtitleColor());
        }

        if (chartSettings.getSubtitleColor() != null) {
            Font subtitleFont = chartSettings.getSubtitleFont() != null
                    ? FontUtil.getInstance(jasperReportsContext).getAwtFont(chartSettings.getSubtitleFont(),
                            Locale.getDefault())
                    : TextTitle.DEFAULT_FONT;
            subtitle.setFont(subtitleFont);
        }

        subtitle.setPosition(titleEdge);

        jfreechart.addSubtitle(subtitle);
    }

    // Apply all of the legend formatting options
    LegendTitle legend = jfreechart.getLegend();

    if (legend != null) {
        legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend()));
        if (legend.isVisible()) {
            if (chartSettings.getLegendColor() != null) {
                legend.setItemPaint(chartSettings.getLegendColor());
            }
            if (chartSettings.getLegendBackgroundColor() != null) {
                legend.setBackgroundPaint(chartSettings.getLegendBackgroundColor());
            }

            if (chartSettings.getLegendFont() != null) {
                legend.setItemFont(FontUtil.getInstance(jasperReportsContext)
                        .getAwtFont(chartSettings.getLegendFont(), Locale.getDefault()));
            }
            legend.setPosition(getEdge(chartSettings.getLegendPosition(), RectangleEdge.BOTTOM));
        }
    }

    String renderType = chartSettings.getRenderType() == null ? defaultRenderType
            : chartSettings.getRenderType();
    Rectangle2D rectangle = new Rectangle2D.Double(0, 0, element.getWidth(), element.getHeight());

    if (chartCustomizer != null) {
        chartCustomizer.customize(jfreechart, chartComponent);
    }

    return ChartUtil.getInstance(jasperReportsContext).getChartRenderableFactory(renderType)
            .getRenderable(jasperReportsContext, jfreechart, spiderchartBean.getHyperlinkProvider(), rectangle);
}

From source file:org.jreserve.dummy.plot.charts.AbstractChart.java

protected void formatLegend(LegendTitle legend) {
    legend.setBackgroundPaint(format.getBackgroundColor());
    legend.setItemPaint(format.getForeColor());
}

From source file:org.jreserve.gui.plot.charts.AbstractChart.java

protected void formatLegend(LegendTitle legend) {
    legend.setBackgroundPaint(format.getBackgroundColor());
    legend.setItemPaint(format.getForeColor());
    legend.setPosition(format.getLegendPosition());
}

From source file:org.sonar.server.charts.deprecated.BaseChart.java

protected void configureChart(JFreeChart chart, RectangleEdge legendPosition) {
    chart.setBackgroundPaint(new Color(255, 255, 255, 0));
    chart.setBackgroundImageAlpha(0.0f);
    chart.setBorderVisible(false);/*w w w . j a  v a2 s . com*/
    chart.setAntiAlias(true);
    chart.setTextAntiAlias(true);

    chart.removeLegend();
    if (legendPosition != null) {
        LegendTitle legend = new LegendTitle(chart.getPlot());
        legend.setPosition(legendPosition);
        legend.setItemPaint(BASE_COLOR);
        chart.addSubtitle(legend);
    }
}

From source file:com.romraider.logger.ecu.ui.handler.graph.GraphUpdateHandler.java

private synchronized void addToCombined(LoggerData loggerData) {
    if (combinedChartPanel == null) {
        combinedChartPanel = new ChartPanel(createXYLineChart(loggerData, null, true), false, true, true, true,
                true);//from  w  w w  . jav  a2  s.c  om
        LegendTitle legendTitle = new LegendTitle(combinedChartPanel.getChart().getXYPlot());
        legendTitle.setItemPaint(WHITE);
        combinedChartPanel.getChart().addLegend(legendTitle);
        combinedChartPanel.setMinimumSize(new Dimension(500, 400));
        combinedChartPanel.setPreferredSize(new Dimension(500, 400));
        graphPanel.add(combinedChartPanel);
    }
    XYPlot plot = combinedChartPanel.getChart().getXYPlot();
    plot.setDataset(counter, new XYSeriesCollection(seriesMap.get(loggerData)));
    plot.setRenderer(counter, new StandardXYItemRenderer());
    datasetIndexes.put(loggerData, counter++);
}

From source file:net.sf.jasperreports.components.spiderchart.FillSpiderChart.java

protected JFreeChart evaluateChart(byte evaluation) throws JRException {
    maxValue = (Double) fillContext.evaluate(getPlot().getMaxValueExpression(), evaluation);
    titleText = (String) fillContext.evaluate(getChartSettings().getTitleExpression(), evaluation);
    subtitleText = (String) fillContext.evaluate(getChartSettings().getSubtitleExpression(), evaluation);
    anchorName = (String) fillContext.evaluate(getChartSettings().getAnchorNameExpression(), evaluation);
    hyperlinkReference = (String) fillContext.evaluate(getChartSettings().getHyperlinkReferenceExpression(),
            evaluation);/*from ww  w  .j ava  2  s  . c  om*/
    hyperlinkAnchor = (String) fillContext.evaluate(getChartSettings().getHyperlinkAnchorExpression(),
            evaluation);
    hyperlinkPage = (Integer) fillContext.evaluate(getChartSettings().getHyperlinkPageExpression(), evaluation);
    hyperlinkTooltip = (String) fillContext.evaluate(getChartSettings().getHyperlinkTooltipExpression(),
            evaluation);
    hyperlinkParameters = JRFillHyperlinkHelper.evaluateHyperlinkParameters(getChartSettings(),
            expressionEvaluator, evaluation);

    dataset.evaluateDatasetRun(evaluation);
    dataset.finishDataset();

    chartHyperlinkProvider = new CategoryChartHyperlinkProvider(dataset.getItemHyperlinks());

    bookmarkLevel = getChartSettings().getBookmarkLevel();

    SpiderWebPlot spiderWebPlot = new SpiderWebPlot((DefaultCategoryDataset) dataset.getCustomDataset());

    if (getPlot().getAxisLineColor() != null) {
        spiderWebPlot.setAxisLinePaint(getPlot().getAxisLineColor());
    }
    if (getPlot().getAxisLineWidth() != null) {
        spiderWebPlot.setAxisLineStroke(new BasicStroke(getPlot().getAxisLineWidth()));
    }
    if (getPlot().getBackcolor() != null) {
        spiderWebPlot.setBackgroundPaint(getPlot().getBackcolor());
    }
    if (getPlot().getBackgroundAlpha() != null) {
        spiderWebPlot.setBackgroundAlpha(getPlot().getBackgroundAlpha());
    }
    if (getPlot().getForegroundAlpha() != null) {
        spiderWebPlot.setForegroundAlpha(getPlot().getForegroundAlpha());
    }
    if (getPlot().getHeadPercent() != null) {
        spiderWebPlot.setHeadPercent(getPlot().getHeadPercent());
    }
    if (getPlot().getInteriorGap() != null) {
        spiderWebPlot.setInteriorGap(getPlot().getInteriorGap());
    }
    if (getPlot().getLabelColor() != null) {
        spiderWebPlot.setLabelPaint(getPlot().getLabelColor());
    }
    if (getPlot().getLabelFont() != null) {
        spiderWebPlot.setLabelFont(JRFontUtil.getAwtFont(getPlot().getLabelFont(), Locale.getDefault()));
    }
    if (getPlot().getLabelGap() != null) {
        spiderWebPlot.setAxisLabelGap(getPlot().getLabelGap());
    }
    if (maxValue != null) {
        spiderWebPlot.setMaxValue(maxValue);
    }
    if (getPlot().getRotation() != null) {
        spiderWebPlot.setDirection(getPlot().getRotation().getRotation());
    }
    if (getPlot().getStartAngle() != null) {
        spiderWebPlot.setStartAngle(getPlot().getStartAngle());
    }
    if (getPlot().getTableOrder() != null) {
        spiderWebPlot.setDataExtractOrder(getPlot().getTableOrder().getOrder());
    }
    if (getPlot().getWebFilled() != null) {
        spiderWebPlot.setWebFilled(getPlot().getWebFilled());
    }

    spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
    spiderWebPlot.setLabelGenerator(new StandardCategoryItemLabelGenerator());

    Font titleFont = getChartSettings().getTitleFont() != null
            ? JRFontUtil.getAwtFont(getChartSettings().getTitleFont(), Locale.getDefault())
            : TextTitle.DEFAULT_FONT;

    JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true);

    if (chartSettings.getBackcolor() != null) {
        jfreechart.setBackgroundPaint(chartSettings.getBackcolor());
    }

    RectangleEdge titleEdge = getEdge(getChartSettings().getTitlePosition(), RectangleEdge.TOP);

    if (titleText != null) {
        TextTitle title = jfreechart.getTitle();
        title.setText(titleText);
        if (getChartSettings().getTitleColor() != null) {
            title.setPaint(getChartSettings().getTitleColor());
        }

        title.setFont(titleFont);
        title.setPosition(titleEdge);
        jfreechart.setTitle(title);
    }

    if (subtitleText != null) {
        TextTitle subtitle = new TextTitle(subtitleText);
        subtitle.setText(subtitleText);
        if (getChartSettings().getSubtitleColor() != null) {
            subtitle.setPaint(getChartSettings().getSubtitleColor());
        }

        if (getChartSettings().getSubtitleColor() != null) {
            Font subtitleFont = getChartSettings().getSubtitleFont() != null
                    ? JRFontUtil.getAwtFont(getChartSettings().getSubtitleFont(), Locale.getDefault())
                    : TextTitle.DEFAULT_FONT;
            subtitle.setFont(subtitleFont);
        }

        subtitle.setPosition(titleEdge);

        jfreechart.addSubtitle(subtitle);
    }

    // Apply all of the legend formatting options
    LegendTitle legend = jfreechart.getLegend();

    if (legend != null) {
        legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend()));
        if (legend.isVisible()) {
            if (getChartSettings().getLegendColor() != null) {
                legend.setItemPaint(getChartSettings().getLegendColor());
            }
            if (getChartSettings().getLegendBackgroundColor() != null) {
                legend.setBackgroundPaint(getChartSettings().getLegendBackgroundColor());
            }

            if (getChartSettings().getLegendFont() != null) {
                legend.setItemFont(
                        JRFontUtil.getAwtFont(getChartSettings().getLegendFont(), Locale.getDefault()));
            }
            legend.setPosition(getEdge(getChartSettings().getLegendPosition(), RectangleEdge.BOTTOM));
        }
    }
    return jfreechart;

}

From source file:org.pentaho.plugin.jfreereport.reportcharts.MultiPieChartExpression.java

protected void configureSubChart(final JFreeChart chart) {
    final TextTitle chartTitle = chart.getTitle();
    if (chartTitle != null) {
        if (getPieTitleFont() != null) {
            chartTitle.setFont(getPieTitleFont());
        } else {/*from  w  w w.j  a v a  2 s  .  co m*/
            final Font titleFont = Font.decode(getTitleFont());
            chartTitle.setFont(titleFont);
        }
    }

    if (isAntiAlias() == false) {
        chart.setAntiAlias(false);
    }

    final LegendTitle chLegend = chart.getLegend();
    if (chLegend != null) {
        final RectangleEdge loc = translateEdge(getLegendLocation().toLowerCase());
        if (loc != null) {
            chLegend.setPosition(loc);
        }
        if (getLegendFont() != null) {
            chLegend.setItemFont(Font.decode(getLegendFont()));
        }
        if (!isDrawLegendBorder()) {
            chLegend.setBorder(BlockBorder.NONE);
        }
        if (getLegendBackgroundColor() != null) {
            chLegend.setBackgroundPaint(getLegendBackgroundColor());
        }
        if (getLegendTextColor() != null) {
            chLegend.setItemPaint(getLegendTextColor());
        }
    }

    final Plot plot = chart.getPlot();
    plot.setNoDataMessageFont(Font.decode(getLabelFont()));

    final String pieNoData = getPieNoDataMessage();
    if (pieNoData != null) {
        plot.setNoDataMessage(pieNoData);
    } else {
        final String message = getNoDataMessage();
        if (message != null) {
            plot.setNoDataMessage(message);
        }
    }
}

From source file:org.jfree.eastwood.ChartEngine.java

/**
 * Creates and returns a new <code>JFreeChart</code> instance that
 * reflects the specified parameters (which should be equivalent to
 * a parameter map returned by HttpServletRequest.getParameterMap() for
 * a valid URI for the Google Chart API.
 *
 * @param params  the parameters (<code>null</code> not permitted).
 * @param font    the font to use to draw titles, labels and legends.
 *
 * @return A chart corresponding to the specification in the supplied
 *         parameters./*from ww  w  .j a  v  a2s  . c o m*/
 */
public static JFreeChart buildChart(Map params, Font font) {
    if (params == null) {
        throw new IllegalArgumentException("Null 'params' argument.");
    }

    JFreeChart chart = null;

    // *** CHART TYPE **
    String[] chartType = (String[]) params.get("cht");
    int dataType = -1; // 0 = PieDataset; 1 = XYDataset; 2 = special

    // pie charts: 'p' and 'p3'
    if (chartType[0].equals("p")) {
        chart = createPieChart();
        dataType = 0;
    } else if (chartType[0].equals("p3")) {
        chart = createPieChart3D();
        dataType = 0;
    }
    // line chart: 'lc'
    else if (chartType[0].equals("lc")) {
        chart = createLineChart();
        dataType = 1;
    }
    // sparkline: 'ls'
    else if (chartType[0].equals("ls")) {
        chart = createSparklineChart();
        dataType = 1;
    }
    // xy chart: 'lxy'
    else if (chartType[0].equals("lxy")) {
        chart = createLineChart();
        dataType = 3;
    }
    // bar charts: 'bhs', 'bhg', 'bhs' and 'bhg'
    else if (chartType[0].equals("bhs")) {
        chart = createStackedBarChart(PlotOrientation.HORIZONTAL);
        dataType = 2;
    } else if (chartType[0].equals("bhg")) {
        chart = createBarChart(PlotOrientation.HORIZONTAL);
        dataType = 2;
    } else if (chartType[0].equals("bvs")) {
        chart = createStackedBarChart(PlotOrientation.VERTICAL);
        dataType = 2;
    } else if (chartType[0].equals("bvg")) {
        chart = createBarChart(PlotOrientation.VERTICAL);
        dataType = 2;
    } else if (chartType[0].equals("bhs3")) {
        chart = createStackedBarChart3D(PlotOrientation.HORIZONTAL);
        dataType = 2;
    } else if (chartType[0].equals("bhg3")) {
        chart = createBarChart3D(PlotOrientation.HORIZONTAL);
        dataType = 2;
    } else if (chartType[0].equals("bvs3")) {
        chart = createStackedBarChart3D(PlotOrientation.VERTICAL);
        dataType = 2;
    } else if (chartType[0].equals("bvg3")) {
        chart = createBarChart3D(PlotOrientation.VERTICAL);
        dataType = 2;
    }
    // scatter chart: 's'
    else if (chartType[0].equals("s")) {
        chart = createScatterChart();
        dataType = 4;
    } else if (chartType[0].equals("v")) {
        throw new RuntimeException("Venn diagrams not implemented.");
        // TODO: fix this.
    } else {
        throw new RuntimeException("Unknown chart type: " + chartType[0]);
    }

    chart.getPlot().setOutlineVisible(false);

    // *** CHART AXES ***
    List axes = new java.util.ArrayList();
    String[] axisStr = (String[]) params.get("chxt");
    if (axisStr != null) {
        if (chart.getPlot() instanceof XYPlot) {
            XYPlot plot = (XYPlot) chart.getPlot();
            processAxisStr(plot, axisStr[0], axes);
        } else if (chart.getPlot() instanceof CategoryPlot) {
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                processAxisStrV(plot, axisStr[0], axes);
            } else {
                processAxisStrH(plot, axisStr[0], axes);
            }
        }
    }

    // *** AXIS RANGES ***
    String[] axisRangeStr = (String[]) params.get("chxr");
    if (axisRangeStr != null) {
        String[] ranges = breakString(axisRangeStr[0], '|');
        for (int i = 0; i < ranges.length; i++) {
            int comma1 = ranges[i].indexOf(',');
            int comma2 = ranges[i].indexOf(',', comma1 + 1);
            int axisIndex = Integer.parseInt(ranges[i].substring(0, comma1));
            float lowerBound = Float.parseFloat(ranges[i].substring(comma1 + 1, comma2));
            float upperBound = Float.parseFloat(ranges[i].substring(comma2 + 1));
            Axis axis = (Axis) axes.get(axisIndex);
            if (axis instanceof GValueAxis) {
                GValueAxis gaxis = (GValueAxis) axis;
                gaxis.setLabelAxisStart(lowerBound);
                gaxis.setLabelAxisEnd(upperBound);
            }
        }
    }

    // *** AXIS LABELS ***
    String[] axisLabelStr = (String[]) params.get("chxl");
    if (axisLabelStr != null) {
        Pattern p = Pattern.compile("\\d+:\\|");
        Matcher m = p.matcher(axisLabelStr[0]);
        if (m.find()) {
            int keyStart = m.start();
            int labelStart = m.end();
            while (m.find(labelStart)) {
                String keyStr = axisLabelStr[0].substring(keyStart, labelStart - 2);
                int axisIndex = Integer.parseInt(keyStr);
                keyStart = m.start();
                String labelStr = axisLabelStr[0].substring(labelStart, keyStart - 1);
                String[] labels = breakString(labelStr, '|');
                GLabelledAxis axis = (GLabelledAxis) axes.get(axisIndex);
                axis.setTickLabels(Arrays.asList(labels));
                labelStart = m.end();
            }
            // process the final item
            String keyStr = axisLabelStr[0].substring(keyStart, labelStart - 2);
            String labelStr = axisLabelStr[0].substring(labelStart);
            int axisIndex = Integer.parseInt(keyStr);
            if (labelStr.endsWith("|")) {
                labelStr = labelStr.substring(0, labelStr.length() - 1);
            }
            String[] labels = breakString(labelStr, '|');
            GLabelledAxis axis = (GLabelledAxis) axes.get(axisIndex);
            axis.setTickLabels(Arrays.asList(labels));

        } else {
            throw new RuntimeException("No matching pattern!");
        }

    }

    // ** EXPLICIT AXIS LABEL POSITIONS
    String[] axisPositionStr = (String[]) params.get("chxp");
    if (axisPositionStr != null) {
        String[] positions = breakString(axisPositionStr[0], '|');
        for (int i = 0; i < positions.length; i++) {
            int c1 = positions[i].indexOf(',');
            int axisIndex = Integer.parseInt(positions[i].substring(0, c1));
            String remainingStr = positions[i].substring(c1 + 1);
            String[] valueStr = breakString(remainingStr, ',');
            List tickValues = new java.util.ArrayList(valueStr.length);
            Axis axis = (Axis) axes.get(axisIndex);
            if (axis instanceof GValueAxis) {
                GValueAxis gaxis = (GValueAxis) axes.get(axisIndex);
                for (int j = 0; j < valueStr.length; j++) {
                    float pos = Float.parseFloat(valueStr[j]);
                    tickValues.add(new Float(pos));
                }
                gaxis.setTickLabelPositions(tickValues);
            }
            // FIXME: what about a CategoryAxis?
        }
    }

    // *** CHART TITLE ***
    String[] titleStr = (String[]) params.get("chtt");
    if (titleStr != null) {
        // process the title
        String[] s = breakString(titleStr[0], '|');
        for (int i = 0; i < s.length; i++) {
            TextTitle t = new TextTitle(s[i].replace('+', ' '));
            t.setPaint(Color.darkGray);
            // Google seems to use 14pt fonts for titles and 12pt fonts for
            // all other text. Make sure this relationship remains.
            t.setFont(font.deriveFont(font.getSize2D() * 14f / 12f));
            chart.addSubtitle(t);
        }
        // and the font and colour
        String[] fontStr = (String[]) params.get("chts");
        if (fontStr != null) {
            int c1 = fontStr[0].indexOf(',');
            String colorStr = null;
            String fontSizeStr = null;
            if (c1 != -1) {
                colorStr = fontStr[0].substring(0, c1);
                fontSizeStr = fontStr[0].substring(c1 + 1);
            } else {
                colorStr = fontStr[0];
            }
            Color color = parseColor(colorStr);
            int size = 12;
            if (fontSizeStr != null) {
                size = Integer.parseInt(fontSizeStr);
            }
            for (int i = 0; i < chart.getSubtitleCount(); i++) {
                Title t = chart.getSubtitle(i);
                if (t instanceof TextTitle) {
                    TextTitle tt = (TextTitle) t;
                    tt.setPaint(color);
                    tt.setFont(font.deriveFont((float) size));
                }
            }
        }
    }

    // *** CHART DATA ***
    String[] dataStr = (String[]) params.get("chd");
    String scalingStr = null;
    if (dataStr.length > 0 && dataStr[0].startsWith("t:")) {
        // Only look at chds when text encoding is used
        String[] chds = (String[]) params.get("chds");
        if (chds != null && chds.length > 0) {
            scalingStr = chds[0];
        }
    }

    // we'll also process an optional second dataset that is provided as
    // an Eastwood extension...this isn't part of the Google Chart API
    String[] d2Str = (String[]) params.get("ewd2");

    // 'p' and 'p3' - create PieDataset
    if (dataType == 0) {
        PieDataset dataset = DataUtilities.parsePieDataset(dataStr[0], scalingStr);
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setDataset(dataset);

        // ignore d2Str as there is currently no need for a second pie
        // dataset.
    }

    // 'lc' - create XYDataset
    else if (dataType == 1) {
        XYPlot plot = (XYPlot) chart.getPlot();
        XYDataset dataset = DataUtilities.parseXYDataset(dataStr[0], scalingStr);
        plot.setDataset(dataset);

        if (d2Str != null) { // an Eastwood extension
            XYDataset d2 = DataUtilities.parseXYDataset(d2Str[0], scalingStr);
            plot.setDataset(1, d2);
        }
    }

    // 'bhs', 'bhg', 'bvs', 'bvg'
    else if (dataType == 2) {
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        CategoryDataset dataset = DataUtilities.parseCategoryDataset(dataStr[0], scalingStr);
        plot.setDataset(dataset);

        if (d2Str != null) { // an Eastwood extension
            CategoryDataset d2 = DataUtilities.parseCategoryDataset(d2Str[0], scalingStr);
            plot.setDataset(1, d2);
        }
    }

    // 'lxy'
    else if (dataType == 3) {
        XYPlot plot = (XYPlot) chart.getPlot();
        XYDataset dataset = DataUtilities.parseXYDataset2(dataStr[0], scalingStr);
        plot.setDataset(dataset);

        if (d2Str != null) { // an Eastwood extension
            XYDataset d2 = DataUtilities.parseXYDataset2(d2Str[0], scalingStr);
            plot.setDataset(1, d2);
        }
    } else if (dataType == 4) {
        XYPlot plot = (XYPlot) chart.getPlot();
        XYSeriesCollection dataset = DataUtilities.parseScatterDataset(dataStr[0], scalingStr);
        if (dataset.getSeriesCount() > 1) {
            dataset.removeSeries(1);
        }
        plot.setDataset(dataset);
        if (d2Str != null) { // an Eastwood extension
            XYDataset d2 = DataUtilities.parseXYDataset2(d2Str[0], scalingStr);
            plot.setDataset(1, d2);
        }
    }

    if (chart.getPlot() instanceof XYPlot) {
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getDomainAxis().setLabelFont(font);
        plot.getDomainAxis().setTickLabelFont(font);
        plot.getRangeAxis().setLabelFont(font);
        plot.getRangeAxis().setTickLabelFont(font);
    } else if (chart.getPlot() instanceof CategoryPlot) {
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.getDomainAxis().setLabelFont(font);
        plot.getDomainAxis().setTickLabelFont(font);
        plot.getRangeAxis().setLabelFont(font);
        plot.getRangeAxis().setTickLabelFont(font);
    }

    // *** CHART COLOURS ***
    String[] colorStr = (String[]) params.get("chco");
    if (colorStr != null) {
        Color[] colors = parseColors(colorStr[0]);
        if (dataType == 0) {
            PiePlot plot = (PiePlot) chart.getPlot();
            applyColorsToPiePlot(plot, colors);
        } else {
            AbstractRenderer renderer = null;
            if (chart.getPlot() instanceof CategoryPlot) {
                CategoryPlot plot = (CategoryPlot) chart.getPlot();
                renderer = (AbstractRenderer) plot.getRenderer();
                renderer.setBasePaint(colors[0]);
            } else if (chart.getPlot() instanceof XYPlot) {
                XYPlot plot = (XYPlot) chart.getPlot();
                renderer = (AbstractRenderer) plot.getRenderer();
                renderer.setBasePaint(colors[colors.length - 1]);
            }
            for (int i = 0; i < colors.length; i++) {
                renderer.setSeriesPaint(i, colors[i]);
            }
        }
    } else {
        Plot plot = chart.getPlot();
        if (plot instanceof PiePlot) {
            applyColorsToPiePlot((PiePlot) chart.getPlot(), new Color[] { new Color(255, 153, 0) });
        }
    }

    // *** CHART LINE STYLES ***
    String[] lineStr = (String[]) params.get("chls");
    if (lineStr != null && chart.getPlot() instanceof XYPlot) {
        Stroke[] strokes = parseLineStyles(lineStr[0]);
        XYPlot plot = (XYPlot) chart.getPlot();
        XYItemRenderer renderer = plot.getRenderer();
        for (int i = 0; i < strokes.length; i++) {
            renderer.setSeriesStroke(i, strokes[i]);
        }
        renderer.setBaseStroke(strokes[strokes.length - 1]);
    }

    // *** CHART GRID LINES
    if (dataType != 0) {
        String[] gridStr = (String[]) params.get("chg");
        if (gridStr != null) {
            processGridLinesSpec(gridStr[0], chart);
        }
    }

    // *** CHART LABELS
    if (dataType == 0) { // pie chart
        String[] labelStr = (String[]) params.get("chl");
        if (labelStr != null) {
            String[] s = breakString(labelStr[0], '|');
            List labels = Arrays.asList(s);
            PiePlot plot = (PiePlot) chart.getPlot();
            if (labels.size() > 0) {
                plot.setLabelGenerator(new GPieSectionLabelGenerator(labels));
                plot.setLabelFont(font);
                plot.setLabelPaint(Color.darkGray);
            }
        }
    }

    String[] legendStr = (String[]) params.get("chdl");
    if (legendStr != null) {
        // process the title
        String[] s = breakString(legendStr[0], '|');
        List labels = Arrays.asList(s);
        if (labels.size() > 0) {
            Plot p = chart.getPlot();
            if (p instanceof CategoryPlot) {
                CategoryPlot plot = (CategoryPlot) chart.getPlot();
                BarRenderer renderer = (BarRenderer) plot.getRenderer();
                renderer.setLegendItemLabelGenerator(new GSeriesLabelGenerator(labels));
                renderer.setBaseSeriesVisibleInLegend(false);
                for (int i = 0; i < labels.size(); i++) {
                    renderer.setSeriesVisibleInLegend(i, Boolean.TRUE);
                }
            } else if (p instanceof XYPlot) {
                XYPlot plot = (XYPlot) chart.getPlot();
                XYItemRenderer renderer = plot.getRenderer();
                renderer.setLegendItemLabelGenerator(new GSeriesLabelGenerator(labels));
                renderer.setBaseSeriesVisibleInLegend(false);
                for (int i = 0; i < labels.size(); i++) {
                    renderer.setSeriesVisibleInLegend(i, Boolean.TRUE);
                }
            } else if (p instanceof PiePlot) {
                PiePlot plot = (PiePlot) chart.getPlot();
                plot.setLegendLabelGenerator(new GPieSectionLabelGenerator(labels));
            }

            LegendTitle legend = new LegendTitle(chart.getPlot());
            RectangleEdge pos = RectangleEdge.RIGHT;
            String[] chdlp = (String[]) params.get("chdlp");
            if (chdlp != null) {
                if ("b".equals(chdlp[0])) {
                    pos = RectangleEdge.BOTTOM;
                } else if ("t".equals(chdlp[0])) {
                    pos = RectangleEdge.TOP;
                } else if ("l".equals(chdlp[0])) {
                    pos = RectangleEdge.LEFT;
                }
            }
            legend.setPosition(pos);
            legend.setItemFont(font);
            legend.setItemPaint(Color.darkGray);
            chart.addSubtitle(legend);
        }
    }

    // *** CHART MARKERS ***
    String[] markerStr = (String[]) params.get("chm");
    if (markerStr != null) {
        String[] markers = breakString(markerStr[0], '|');
        for (int i = 0; i < markers.length; i++) {
            addMarker(markers[i], chart);
        }
    }

    // *** CHART FILL ***/
    String[] fillStr = (String[]) params.get("chf");
    if (fillStr != null) {
        // process the 1 or 2 fill specs
        int i = fillStr[0].indexOf('|');
        if (i == -1) {
            processFillSpec(fillStr[0], chart);
        } else {
            String fs1 = fillStr[0].substring(0, i);
            String fs2 = fillStr[0].substring(i + 1);
            processFillSpec(fs1, chart);
            processFillSpec(fs2, chart);
        }
    }

    // process the 'ewtr' tag, if present
    processEWTR(params, chart);

    return chart;

}

From source file:com.vgi.mafscaling.LogView.java

private void createGraghPanel() {
    JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, null, PlotOrientation.VERTICAL, false,
            true, false);//from w w  w  .j  a  v  a  2 s.  com
    chartPanel = new ChartPanel(chart, true, true, true, true, true);
    chartPanel.setAutoscrolls(true);
    chartPanel.setPopupMenu(null);
    chart.setBackgroundPaint(new Color(60, 60, 65));

    rpmDataset = new XYSeriesCollection();
    rpmPlotRenderer = new XYLineAndShapeRenderer();
    dataset = new XYSeriesCollection();
    plotRenderer = new XYLineAndShapeRenderer();

    NumberAxis xAxis = new NumberAxis();
    xAxis.setTickLabelsVisible(false);
    xAxis.setTickLabelPaint(Color.WHITE);
    xAxis.setAutoRangeIncludesZero(false);
    NumberAxis yAxis = new NumberAxis();
    yAxis.setTickLabelsVisible(false);
    yAxis.setTickLabelPaint(Color.WHITE);
    yAxis.setAutoRangeIncludesZero(false);
    NumberAxis y2Axis = new NumberAxis();
    y2Axis.setTickLabelsVisible(false);
    y2Axis.setTickLabelPaint(Color.WHITE);
    y2Axis.setAutoRangeIncludesZero(false);

    plot = chartPanel.getChart().getXYPlot();
    plot.setRangePannable(true);
    plot.setDomainPannable(true);
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    plot.setBackgroundPaint(new Color(80, 80, 85));

    plot.setDataset(0, rpmDataset);
    plot.setRenderer(0, rpmPlotRenderer);
    plot.setDomainAxis(0, xAxis);
    plot.setRangeAxis(0, yAxis);
    plot.mapDatasetToDomainAxis(0, 0);
    plot.mapDatasetToRangeAxis(0, 0);

    plot.setDataset(1, dataset);
    plot.setRenderer(1, plotRenderer);
    plot.setRangeAxis(1, y2Axis);
    plot.mapDatasetToDomainAxis(1, 0);
    plot.mapDatasetToRangeAxis(1, 1);

    LegendTitle legend = new LegendTitle(plot);
    legend.setItemFont(new Font("Arial", 0, 10));
    legend.setPosition(RectangleEdge.TOP);
    legend.setItemPaint(Color.WHITE);
    chart.addLegend(legend);

    xyMarker = new XYDomainMutilineAnnotation();
    plot.addAnnotation(xyMarker);

    chartMouseListener = new ChartMouseListener() {
        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            try {
                Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
                Point2D p = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
                double x = plot.getDomainAxis().java2DToValue(p.getX(), dataArea, plot.getDomainAxisEdge());
                boolean isLeft = (p.getX() < (dataArea.getMaxX() - dataArea.getMinX()) / 2) ? true : false;
                if (setMarkers(x, isLeft)) {
                    try {
                        int selectedCol = logDataTable.getTable().getSelectedColumn();
                        if (selectedCol < 0)
                            selectedCol = 0;
                        if (logPlayWindow == null || startMarker != null || endMarker != null) {
                            logDataTable.getTable().setRowSelectionInterval((int) x, (int) x);
                            logDataTable.getTable().changeSelection((int) x, selectedCol, false, false);
                        } else {
                            logPlayWindow.setProgressBar((int) x);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            if (logPlayWindow == null)
                return;
            if (xyMarker.count() == 0)
                return;
            Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
            Point2D p = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
            double x = plot.getDomainAxis().java2DToValue(p.getX(), dataArea, plot.getDomainAxisEdge());
            if (x < 0 || (int) x >= logDataTable.getRowCount())
                return;
            if (SwingUtilities.isLeftMouseButton(event.getTrigger())) {
                if (startMarker == null) {
                    startMarker = new ValueMarker(x);
                    startMarker.setPaint(Color.GREEN);
                    startMarker.setStroke(new BasicStroke(1.5f));
                    plot.addDomainMarker(startMarker);
                } else {
                    plot.removeDomainMarker(startMarker);
                    startMarker = null;
                }
            } else if (SwingUtilities.isRightMouseButton(event.getTrigger())) {
                if (endMarker == null) {
                    endMarker = new ValueMarker(x);
                    endMarker.setPaint(Color.GREEN);
                    endMarker.setStroke(new BasicStroke(1.5f));
                    plot.addDomainMarker(endMarker);
                } else {
                    plot.removeDomainMarker(endMarker);
                    endMarker = null;
                }
            }
            chartPanel.repaint();
            logPlayWindow.setStartEndArea(startMarker, endMarker);
        }
    };

    chartPanel.addChartMouseListener(chartMouseListener);
}