Example usage for org.jfree.chart.axis CategoryAxis setUpperMargin

List of usage examples for org.jfree.chart.axis CategoryAxis setUpperMargin

Introduction

In this page you can find the example usage for org.jfree.chart.axis CategoryAxis setUpperMargin.

Prototype

public void setUpperMargin(double margin) 

Source Link

Document

Sets the upper margin for the axis and sends an AxisChangeEvent to all registered listeners.

Usage

From source file:gov.nih.nci.cma.web.graphing.GEPlot.java

public String generateGeometricMeanIntensityChart(String xAxisLabel, String yAxisLabel, HttpSession session,
        PrintWriter pw) {//w  ww . ja  v  a 2  s. c  o m
    String gmfilename = "";

    JFreeChart gmChart = null;
    try {

        gmChart = ChartFactory.createBarChart(null, xAxisLabel, // domain axis label
                yAxisLabel, gmDataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
        );
        gmChart.setBackgroundPaint(java.awt.Color.white);
        // lets start some customization to retro fit w/jcharts lookand feel
        CategoryPlot plot = gmChart.getCategoryPlot();
        CategoryAxis axis = plot.getDomainAxis();
        axis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        axis.setLowerMargin(0.02); // two percent
        axis.setCategoryMargin(0.20); // 20 percent
        axis.setUpperMargin(0.02); // two percent
        //StatisticalBarRenderer renderer = new StatisticalBarRenderer();
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        // BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setItemMargin(0.01); // one percent
        renderer.setDrawBarOutline(true);
        renderer.setOutlinePaint(Color.BLACK);
        renderer.setBaseToolTipGenerator(new CategoryToolTipGenerator() {

            public String generateToolTip(CategoryDataset dataset, int series, int item) {
                String stdDev = (String) stdDevMap
                        .get(dataset.getRowKey(series) + "::" + dataset.getColumnKey(item));

                return "Probeset : " + dataset.getRowKey(series) + "<br/>Intensity : "
                        + new DecimalFormat("0.0000").format(dataset.getValue(series, item)) + "<br/>"
                        + "<br/>Std. Dev.: " + stdDev + "<br/>";
            }

        });

        plot.setRenderer(renderer);

        gmChart.removeLegend();
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        //gmfilename = ServletUtilities.saveChartAsPNG(gmChart, imgW, 400, info, session);
        gmfilename = ServletUtilities.saveChartAsPNG(gmChart, imgW, 400, info, session);
        ChartUtilities.writeImageMap(pw, gmfilename, info, new CustomOverlibToolTipTagFragmentGenerator(),
                new StandardURLTagFragmentGenerator());

    } catch (Exception e) {
        System.out.println("Exception - " + e.toString());
        e.printStackTrace(System.out);
    }
    return gmfilename;
}

From source file:gov.nih.nci.cma.web.graphing.GEPlot.java

public String generateLog2Chart(String xAxisLabel, String yAxisLabel, HttpSession session, PrintWriter pw) {
    String log2Filename = "";

    JFreeChart log2Chart = null;// ww  w . j ava  2s . co  m
    try {

        log2Chart = ChartFactory.createBarChart(null, xAxisLabel, // domain axis label
                yAxisLabel, log2Dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
        );
        log2Chart.setBackgroundPaint(java.awt.Color.white);
        // lets start some customization to retro fit w/jcharts lookand feel
        CategoryPlot plot = log2Chart.getCategoryPlot();
        CategoryAxis axis = plot.getDomainAxis();
        axis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        axis.setLowerMargin(0.02); // two percent
        axis.setCategoryMargin(0.20); // 20 percent
        axis.setUpperMargin(0.02); // two percent
        StatisticalBarRenderer renderer = new StatisticalBarRenderer();

        // BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setItemMargin(0.01); // one percent
        renderer.setDrawBarOutline(true);
        renderer.setOutlinePaint(Color.BLACK);
        renderer.setBaseToolTipGenerator(new CategoryToolTipGenerator() {

            public String generateToolTip(CategoryDataset dataset, int series, int item) {
                String stdDev = (String) stdDevMap
                        .get(dataset.getRowKey(series) + "::" + dataset.getColumnKey(item));

                return "Probeset : " + dataset.getRowKey(series) + "<br/>Intensity : "
                        + new DecimalFormat("0.0000").format(dataset.getValue(series, item)) + "<br/>"
                        + "<br/>Std. Dev.: " + stdDev + "<br/>";
            }

        });

        // LegendTitle lg = chart.getLegend();
        plot.setRenderer(renderer);

        // lets generate a custom legend - assumes theres only one source?
        //LegendItemCollection lic = log2Chart.getLegend().getSources()[0].getLegendItems();
        //legendHtml = LegendCreator.buildLegend(lic, "Probesets");
        log2Chart.removeLegend();
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        log2Filename = ServletUtilities.saveChartAsPNG(log2Chart, imgW, 400, info, session);
        ChartUtilities.writeImageMap(pw, log2Filename, info, new CustomOverlibToolTipTagFragmentGenerator(),
                new StandardURLTagFragmentGenerator());

    } catch (Exception e) {
        System.out.println("Exception - " + e.toString());
        e.printStackTrace(System.out);
    }
    return log2Filename;
}

From source file:dk.sdu.mmmi.featureous.views.codecharacterization.TanglingViewChart.java

public TanglingViewChart(boolean pkg, boolean sortByValue) {
    this.sortByValue = sortByValue;
    this.pkg = pkg;
    data = new DefaultKeyedValues2DDataset();
    String title = "Computational unit characterization";
    jchart = ChartFactory.createStackedBarChart(title, (pkg) ? "Package" : "Class", "Tangling", data,
            PlotOrientation.VERTICAL, true, false, false);
    CategoryPlot plot = (CategoryPlot) jchart.getPlot();
    //        chart.getLegend().setPosition(RectangleEdge.RIGHT);
    //        chart.getLegend().setVerticalAlignment(VerticalAlignment.TOP);
    //        chart.getLegend().setHorizontalAlignment(HorizontalAlignment.LEFT);
    LegendItemCollection lic = new LegendItemCollection();
    //        lic.add(new LegendItem("Infrastructural unit", "", "", "", new Rectangle(10, 10), Color.GREEN));
    //        lic.add(new LegendItem("Group-feature unit", "", "", "", new Rectangle(10, 10), Color.BLUE));
    //        lic.add(new LegendItem("Single-feature unit", "", "", "", new Rectangle(10, 10), Color.RED));
    plot.setFixedLegendItems(lic);/*  w w  w.  j a  va2s  .  co  m*/
    //        chart.removeLegend();
    plot.setDomainAxis(new SparselyLabeledCategoryAxis(20));
    CategoryAxis xAxis = (CategoryAxis) plot.getDomainAxis();
    xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    xAxis.setLabel((pkg) ? "Package" : "Class");
    //        xAxis.setTickLabelInsets(new RectangleInsets(0, 0, 5, 5));
    //        xAxis.setMaximumCategoryLabelLines(1);
    xAxis.setLowerMargin(0);
    xAxis.setCategoryMargin(0);
    xAxis.setUpperMargin(0);
    //        xAxis.setMaximumCategoryLabelWidthRatio(20f);
    jchart.setBackgroundPaint(Color.white);

    StackedBarRenderer renderer = new StackedBarRenderer() {

        @Override
        public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea,
                CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset,
                int row, int column, int pass) {
            super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column, pass);
            double start = plot.getDomainAxis().getCategoryStart(column, getColumnCount(), dataArea,
                    plot.getDomainAxisEdge());
            double end = plot.getDomainAxis().getCategoryEnd(column, getColumnCount(), dataArea,
                    plot.getDomainAxisEdge());

            String compUnit = data.getRowKey(row).toString();

            // Calculate y coeffs
            double posBase = getBase();
            //                for(int i = 0; i<row; i++){
            //                    Number val = dataset.getValue(i, column);
            //                    if(val!=null){
            //                        posBase = posBase + val.doubleValue();
            //                    }
            //                }

            Number value = dataset.getValue(row, column);
            if (value == null) {
                return;
            }
            double val = value.doubleValue();

            double translatedBase = plot.getRangeAxis().valueToJava2D(posBase, dataArea,
                    plot.getRangeAxisEdge());
            double translatedValue = plot.getRangeAxis().valueToJava2D(posBase + val, dataArea,
                    plot.getRangeAxisEdge());

            if (Controller.getInstance().getTraceSet().getSelectionManager().getSelectedClasses()
                    .contains(compUnit)
                    || Controller.getInstance().getTraceSet().getSelectionManager().getSelectedPkgs()
                            .contains(compUnit)) {
                g2.setPaint(UIUtils.SELECTION_COLOR);
                g2.setStroke(new BasicStroke(3f));
                Line2D l2d = new Line2D.Double(start, translatedBase, start, translatedValue);
                g2.draw(l2d);
                l2d = new Line2D.Double(end, translatedBase, end, translatedValue);
                g2.draw(l2d);
                l2d = new Line2D.Double(start, translatedBase, end, translatedBase);
                g2.draw(l2d);
                l2d = new Line2D.Double(start, translatedValue, end, translatedValue);
                g2.draw(l2d);
            }
        }
    };

    renderer.setToolTipGenerator(new CategoryToolTipGenerator() {

        public String generateToolTip(CategoryDataset cd, int i, int i1) {
            String key = data.getRowKey(i).toString();
            //                key = key.substring(0, key.length()-1);
            return "<html>" + i + " - " + key + "<br>" + Double.toString(cd.getValue(i, i1).doubleValue())
                    + "</hmtl>";
        }
    });

    plot.setRenderer(renderer);

    panel = new ChartPanel(jchart);

    panel.getPopupMenu().setEnabled(false);//add(SVGExporter.createExportAction(chart, panel));

    createView();
    Controller.getInstance().getTraceSet().getSelectionManager().addSelectionListener(this);
}

From source file:com.rapidminer.gui.plotter.charts.ParetoChartPlotter.java

private JFreeChart createChart() {
    if (data.getItemCount() > 0) {
        // get cumulative percentages
        KeyedValues cumulative = DataUtilities.getCumulativePercentages(data);

        CategoryDataset categoryDataset = DatasetUtilities.createCategoryDataset(
                "Count for " + this.dataTable.getColumnName(this.countColumn) + " = " + countValue, data);

        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(null, // chart title
                this.dataTable.getColumnName(this.groupByColumn), // domain axis label
                "Count", // range axis label
                categoryDataset, // data
                PlotOrientation.VERTICAL, true, // include legend
                true, false);//from   ww  w  .  j a v a2 s  . c om

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.WHITE);

        // get a reference to the plot for further customization...
        CategoryPlot plot = chart.getCategoryPlot();

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLowerMargin(0.02);
        domainAxis.setUpperMargin(0.02);
        domainAxis.setLabelFont(LABEL_FONT_BOLD);
        domainAxis.setTickLabelFont(LABEL_FONT);

        // set the range axis to display integers only...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits(Locale.US));
        rangeAxis.setLabelFont(LABEL_FONT_BOLD);
        rangeAxis.setTickLabelFont(LABEL_FONT);

        // second data set (cumulative percentages)
        CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("Cumulative (Percent)", cumulative);

        LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
        renderer2.setSeriesPaint(0, SwingTools.VERY_DARK_BLUE.darker());

        NumberAxis axis2 = new NumberAxis("Percent of " + countValue);
        axis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
        axis2.setLabelFont(LABEL_FONT_BOLD);
        axis2.setTickLabelFont(LABEL_FONT);

        plot.setRangeAxis(1, axis2);
        plot.setDataset(1, dataset2);
        plot.setRenderer(1, renderer2);
        plot.mapDatasetToRangeAxis(1, 1);

        axis2.setTickUnit(new NumberTickUnit(0.1));

        // show grid lines
        plot.setRangeGridlinesVisible(true);

        // bring cumulative line to front
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

        if (isLabelRotating()) {
            domainAxis.setTickLabelsVisible(true);
            domainAxis.setCategoryLabelPositions(
                    CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0d));
        }

        return chart;
    } else {
        return null;
    }
}

From source file:org.pentaho.chart.plugin.jfreechart.chart.bar.JFreeBarChartGenerator.java

/**
 * This method allows to manipulate bar width indirectly by sepecifying percentages for lower margin,
 * upper margin, category margin and item margin. Definitions of these margins and how they effect bar width
 * are available in the JFreeChart documentation.
 *
 * @param chartDocument Current chart defintion
 * @param categoryPlot  The plot object for the current chart
 *//*w w w  .j a  v a 2  s .co m*/
private void setAxisMargins(final ChartDocument chartDocument, final CategoryPlot categoryPlot) {
    final ArrayList<ChartElement> axisElementsList = chartDocument.getAxisSeriesLinkInfo()
            .getDomainAxisElements();
    if (axisElementsList != null) {
        for (final ChartElement axisElement : axisElementsList) {
            if (axisElement != null) {
                final String axisType = (String) axisElement.getAttribute("type");//$NON-NLS-1$
                if (axisType != null && DOMAIN_AXIS.equalsIgnoreCase(axisType)) {
                    final LayoutStyle layoutStyle = axisElement.getLayoutStyle();
                    final CSSValue lowerMarginValue = layoutStyle.getValue(ChartStyleKeys.MARGIN_LOWER);
                    final CSSValue upperMarginValue = layoutStyle.getValue(ChartStyleKeys.MARGIN_UPPER);
                    final CSSValue itemMarginValue = layoutStyle.getValue(ChartStyleKeys.MARGIN_ITEM);
                    final CSSValue categoryMarginValue = layoutStyle.getValue(ChartStyleKeys.MARGIN_CATEGORY);

                    // The lower, upper and category margins can be controlled through category axis
                    final CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
                    if (lowerMarginValue != null) {
                        final double lowerMargin = ((CSSNumericValue) lowerMarginValue).getValue() / 100;
                        categoryAxis.setLowerMargin(lowerMargin);
                    }
                    if (upperMarginValue != null) {
                        final double upperMargin = ((CSSNumericValue) upperMarginValue).getValue() / 100;
                        categoryAxis.setUpperMargin(upperMargin);
                    }
                    if (categoryMarginValue != null) {
                        final double categoryMargin = ((CSSNumericValue) categoryMarginValue).getValue() / 100;
                        categoryAxis.setCategoryMargin(categoryMargin);
                    }
                    if (itemMarginValue != null) {
                        final double itemMargin = ((CSSNumericValue) itemMarginValue).getValue() / 100;
                        final int datasetCount = categoryPlot.getDatasetCount();
                        for (int i = 0; i < datasetCount; i++) {
                            if (categoryPlot.getRenderer() instanceof BarRenderer) {
                                final BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer(i);
                                barRenderer.setItemMargin(itemMargin);
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:hudson.tasks.test.AbstractTestResultAction.java

private JFreeChart createChart(StaplerRequest req, CategoryDataset dataset) {

    final String relPath = getRelPath(req);

    final JFreeChart chart = ChartFactory.createStackedAreaChart(null, // chart title
            null, // unused
            "count", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips
            false // urls
    );/*from w  w w .  jav a2 s .  c  om*/

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...

    //        final StandardLegend legend = (StandardLegend) chart.getLegend();
    //        legend.setAnchor(StandardLegend.SOUTH);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = chart.getCategoryPlot();

    // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
    plot.setBackgroundPaint(Color.WHITE);
    plot.setOutlinePaint(null);
    plot.setForegroundAlpha(0.8f);
    //        plot.setDomainGridlinesVisible(true);
    //        plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.black);

    CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
    plot.setDomainAxis(domainAxis);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    StackedAreaRenderer ar = new StackedAreaRenderer2() {
        @Override
        public String generateURL(CategoryDataset dataset, int row, int column) {
            NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
            return relPath + label.getRun().getNumber() + "/testReport/";
        }

        @Override
        public String generateToolTip(CategoryDataset dataset, int row, int column) {
            NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
            AbstractTestResultAction a = label.getRun().getAction(AbstractTestResultAction.class);
            switch (row) {
            case 0:
                return String.valueOf(Messages.AbstractTestResultAction_fail(label.getRun().getDisplayName(),
                        a.getFailCount()));
            case 1:
                return String.valueOf(Messages.AbstractTestResultAction_skip(label.getRun().getDisplayName(),
                        a.getSkipCount()));
            default:
                return String.valueOf(Messages.AbstractTestResultAction_test(label.getRun().getDisplayName(),
                        a.getTotalCount()));
            }
        }
    };
    plot.setRenderer(ar);
    ar.setSeriesPaint(0, ColorPalette.RED); // Failures.
    ar.setSeriesPaint(1, ColorPalette.YELLOW); // Skips.
    ar.setSeriesPaint(2, ColorPalette.BLUE); // Total.

    // crop extra space around the graph
    plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));

    return chart;
}

From source file:com.googlecode.logVisualizer.chart.HorizontalBarChartBuilder.java

private JFreeChart createChart(final CategoryDataset dataset) {
    final JFreeChart chart = ChartFactory.createBarChart(getTitle(), xLable, yLable, dataset,
            PlotOrientation.HORIZONTAL, isIncludeLegend(), true, false);
    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    final BarRenderer renderer = (BarRenderer) plot.getRenderer();
    final CategoryAxis categoryAxis = plot.getDomainAxis();
    final NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

    plot.setNoDataMessage("No data available");
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.black);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    setBarShadowVisible(chart, false);/* ww w.j a  v a 2  s.  c om*/

    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseToolTipGenerator(
            new StandardCategoryToolTipGenerator("{1}, {2}", NumberFormat.getInstance()));

    categoryAxis.setCategoryMargin(0.02);
    categoryAxis.setUpperMargin(0.02);
    categoryAxis.setLowerMargin(0.02);
    numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    numberAxis.setUpperMargin(0.1);

    return chart;
}

From source file:com.googlecode.logVisualizer.chart.HorizontalStackedBarChartBuilder.java

private JFreeChart createChart(final CategoryDataset dataset) {
    final JFreeChart chart = ChartFactory.createStackedBarChart(getTitle(), xLable, yLable, dataset,
            PlotOrientation.HORIZONTAL, isIncludeLegend(), true, false);
    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    final StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
    final CategoryAxis categoryAxis = plot.getDomainAxis();
    final NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

    plot.setNoDataMessage("No data available");
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.black);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    setBarShadowVisible(chart, false);//from  w w  w . j ava  2  s.  c o  m

    renderer.setDrawBarOutline(false);
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseToolTipGenerator(
            new StandardCategoryToolTipGenerator("{1}, {2}", NumberFormat.getInstance()));

    categoryAxis.setCategoryMargin(0.02);
    categoryAxis.setUpperMargin(0.02);
    categoryAxis.setLowerMargin(0.02);
    numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    numberAxis.setUpperMargin(0.1);

    return chart;
}

From source file:com.googlecode.logVisualizer.chart.perDayConsumption.ConsumptionBarChartBuilder.java

private JFreeChart createChart(final ConsumptionDataset dataset) {
    final JFreeChart chart = ChartFactory.createStackedBarChart(getTitle(), xLable, yLable, dataset,
            PlotOrientation.VERTICAL, isIncludeLegend(), true, false);
    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    final StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
    final CategoryAxis categoryAxis = plot.getDomainAxis();
    final NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

    plot.setNoDataMessage("No data available");
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.black);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    setBarShadowVisible(chart, false);/*from w  ww.  j a va 2  s. com*/
    setStackColors(dataset, renderer);

    renderer.setDrawBarOutline(false);
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new ConsumptionLableGenerator());
    renderer.setBaseToolTipGenerator(new ConsumptionToolTipGenerator());

    categoryAxis.setCategoryMargin(0.07);
    categoryAxis.setUpperMargin(0.01);
    categoryAxis.setLowerMargin(0.01);
    numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    numberAxis.setUpperMargin(0.1);

    return chart;
}

From source file:hr.restart.util.chart.ChartXY.java

/**
 * Creates a BAR CHART/* w ww. j a v  a  2 s  .co m*/
 * @param dataset The org.jfree.data.CategoryDataset
 * @param title The title
 * @return org.jfree.chart.JFreeChart
 */
private JFreeChart createBarChart(final CategoryDataset dataset, String title, PlotOrientation orientation) {

    final JFreeChart chart = ChartFactory.createBarChart(title, // chart title
            "", // domain axis label
            "", // range axis label
            dataset, // data
            orientation, // the plot orientation
            false, // include legend
            true, false);

    chart.setBackgroundPaint(Color.white);

    // the subtitle from the combobox        
    if (jcb != null)
        chart.addSubtitle(new TextTitle(jcb.getSelectedItem().toString()));

    //subtitles setted by the user.
    if (getSubtitles() != null)
        for (int i = 0; i < getSubtitles().size(); i++) {
            chart.addSubtitle(new TextTitle(getSubtitles().get(i).toString()));
        }

    final Plot plot = chart.getPlot();

    // get a reference to the plot for further customisation...
    final CategoryPlot categoryPlot = (CategoryPlot) plot;

    categoryPlot.setNoDataMessage("NO DATA!");

    categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green,
            Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue });
    categoryPlot.setRenderer(renderer);

    renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    renderer.setItemLabelsVisible(true);

    // inside
    //renderer.setBaseItemLabelPaint(Color.white);
    Font font = new Font("SansSerif", Font.PLAIN, 7);
    Font derive = font.deriveFont(Font.BOLD);
    renderer.setBaseItemLabelFont(derive);

    // margin
    final CategoryAxis domainAxis = categoryPlot.getDomainAxis();
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    //domainAxis.setBottomCategoryLabelPosition(new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER));
    domainAxis.setCategoryLabelPositions(new CategoryLabelPositions(
            new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_CENTER), // TOP
            new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER), // BOTTOM
            new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
                    CategoryLabelWidthType.RANGE, 0.30f), // LEFT
            new CategoryLabelPosition(RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT,
                    CategoryLabelWidthType.RANGE, 0.30f) // RIGHT) 
    ));

    final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER,
            TextAnchor.CENTER, 0.0);
    renderer.setPositiveItemLabelPosition(p);

    if (comboBoxOrientation != null) {
        if (comboBoxOrientation.getSelectedItem() == "Vertikalni") {

            domainAxis.setCategoryLabelPositions(
                    CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
        }
    }

    return chart;

}