Example usage for org.jfree.chart ChartFactory createStackedBarChart

List of usage examples for org.jfree.chart ChartFactory createStackedBarChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartFactory createStackedBarChart.

Prototype

public static JFreeChart createStackedBarChart(String title, String domainAxisLabel, String rangeAxisLabel,
        CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 

Source Link

Document

Creates a stacked bar chart with default settings.

Usage

From source file:org.eclipse.mylyn.tests.chart.ChartTest.java

private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createStackedBarChart("", // chart title
            "Eclipse Usage", // domain axis label
            "Time (minutes)", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            true, // legend
            true, // tooltips
            false // urls
    );// w  w w .  ja  va  2 s .  co m
    return chart;
}

From source file:CPU.StackedBarChartExample1.java

/**
 * Create chart/*  ww w  . j a  va2s .  c  o m*/
 * @param dataset
 * @return
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart stackedChart = ChartFactory.createStackedBarChart("Stacked Bar Chart", "Category", "Value",
            dataset, PlotOrientation.VERTICAL, true, true, false);

    //create group 
    GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
    KeyToGroupMap map = new KeyToGroupMap("G1");
    map.mapKeyToGroup("Toyota (US)", "G1");
    map.mapKeyToGroup("Toyota (Europe)", "G1");
    map.mapKeyToGroup("Toyota (Asia)", "G1");

    map.mapKeyToGroup("Ford (US)", "G2");
    map.mapKeyToGroup("Ford (Europe)", "G2");
    map.mapKeyToGroup("Ford (Asia)", "G2");

    map.mapKeyToGroup("BMW (US)", "G3");
    map.mapKeyToGroup("BMW (Europe)", "G3");
    map.mapKeyToGroup("BMW (Asia)", "G3");
    renderer.setSeriesToGroupMap(map);
    //margin between bar.
    renderer.setItemMargin(0.03);
    //end

    SubCategoryAxis dom_axis = new SubCategoryAxis("Car Production / Month");
    //Margin between group
    dom_axis.setCategoryMargin(0.06);
    //end

    dom_axis.addSubCategory("Toyota");
    dom_axis.addSubCategory("Ford");
    dom_axis.addSubCategory("BMW");

    CategoryPlot plot = (CategoryPlot) stackedChart.getPlot();
    plot.setDomainAxis(dom_axis);
    plot.setRenderer(renderer);

    return stackedChart;
}

From source file:ch.bfh.lca._15h.server.statisticServices.ChartHandler.java

/**
 * Generates a JFreeChart object, which represents an age-pyramide diagramm.
 * @param arrResults an array of GenericResultRow objects. The following assignement has to be done:
 * Value at Position 0 = Categorie (such as Age 25 to 30)
 * Value at Position 1 = Count of males/*from  www  .ja  va 2 s. co  m*/
 * Value at Position 2 = Count of females
 * @return an JFreeChart Object
 */
public JFreeChart generatesPopulationChart(GenericResultRow[] arrResults) {

    DefaultKeyedValues2DDataset data = new DefaultKeyedValues2DDataset();

    for (GenericResultRow resultRow : arrResults) {
        long maleCount = (long) resultRow.getValueAt(1);
        long femaleCount = (long) resultRow.getValueAt(2);
        String category = (String) resultRow.getValueAt(0);
        // Mnner
        data.addValue(-maleCount, "Mnner", category);
        // Frauem
        data.addValue(femaleCount, "Frauen", category);
    }

    // create the chart...
    JFreeChart chart = ChartFactory.createStackedBarChart("Arzt-Patientenkontakte (APK)", "Alter", // domain axis label
            "Arzt-Patientenkontakte (APK)", // range axis label
            data, // data
            PlotOrientation.HORIZONTAL, true, // include legend
            true, // tooltips
            false);

    return chart;
}

From source file:ch.unibe.iam.scg.archie.ui.charts.PatientsConsHistChart.java

/**
 * @see ch.unibe.iam.scg.archie.ui.charts.AbstractChartComposite#initializeChart()
 *//*from w  w  w.  j a v  a  2  s  .  c o  m*/
@Override
protected JFreeChart initializeChart() {
    JFreeChart chart = ChartFactory.createStackedBarChart(PatientsConsHistChart.CHART_TITLE,
            "Costs of Consultations", // domain axis label
            "Patients", // range axis label
            (DefaultKeyedValues2DDataset) this.creator.getDataset(), // data
            PlotOrientation.HORIZONTAL, true, // include legend
            true, // tooltips
            false // urls
    );

    // set tooltip renderer
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.getRenderer().setBaseToolTipGenerator(new HistogramTooltipGenerator());

    // hide tick labels
    CategoryAxis axis = (CategoryAxis) plot.getDomainAxis();
    axis.setTickLabelsVisible(false);

    // Set chart background color to it's parents background
    chart.setBackgroundPaint(new Color(this.parent.getBackground().getRed(),
            this.parent.getBackground().getGreen(), this.parent.getBackground().getBlue()));

    return chart;
}

From source file:ch.unibe.iam.scg.archie.ui.charts.AgeHistogrammChart.java

/**
 * @see ch.unibe.iam.scg.archie.ui.charts.AbstractChartComposite#initializeChart()
 *///from   w w  w  .ja  v  a  2  s. co m
@Override
protected JFreeChart initializeChart() {
    JFreeChart chart = ChartFactory.createStackedBarChart(AgeHistogrammChart.CHART_TITLE, "Age", // domain
            // axis
            // label
            "Patients", // range axis label
            (DefaultKeyedValues2DDataset) this.creator.getDataset(), // data
            PlotOrientation.HORIZONTAL, true, // include legend
            true, // tooltips
            false // urls
    );

    // set tooltip renderer
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.getRenderer().setBaseToolTipGenerator(new HistogramTooltipGenerator());

    // hide tick labels
    CategoryAxis axis = (CategoryAxis) plot.getDomainAxis();
    axis.setTickLabelsVisible(false);

    // Set chart background color to it's parents background
    chart.setBackgroundPaint(new Color(this.parent.getBackground().getRed(),
            this.parent.getBackground().getGreen(), this.parent.getBackground().getBlue()));

    return chart;
}

From source file:ws.moor.bt.gui.charts.TotalBlocksPerPeer.java

private JFreeChart createChart() {

    JFreeChart chart = ChartFactory.createStackedBarChart("Top 20 Block Peers", "Remote IP", "Blocks",
            createDataset(), PlotOrientation.VERTICAL, true, false, false);
    chart.setBackgroundPaint(Color.white);

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0));

    return chart;
}

From source file:org.codehaus.mojo.dashboard.report.plugin.chart.StackedBarChartRenderer.java

/**
 * // www .  j a  v a 2 s .  c  om
 */
public void createChart() {

    CategoryDataset categorydataset = (CategoryDataset) this.datasetStrategy.getDataset();
    report = ChartFactory.createStackedBarChart(this.datasetStrategy.getTitle(),
            this.datasetStrategy.getYAxisLabel(), this.datasetStrategy.getXAxisLabel(), categorydataset,
            PlotOrientation.HORIZONTAL, true, true, false);
    // report.setBackgroundPaint( Color.lightGray );
    report.setAntiAlias(false);
    report.setPadding(new RectangleInsets(5.0d, 5.0d, 5.0d, 5.0d));
    CategoryPlot categoryplot = (CategoryPlot) report.getPlot();
    categoryplot.setBackgroundPaint(Color.white);
    categoryplot.setRangeGridlinePaint(Color.lightGray);
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    if (datasetStrategy instanceof CloverBarChartStrategy
            || datasetStrategy instanceof MultiCloverBarChartStrategy) {
        numberaxis.setRange(0.0D, StackedBarChartRenderer.NUMBER_AXIS_RANGE);
        numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
    } else {
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    }
    numberaxis.setLowerMargin(0.0D);
    StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer();
    stackedbarrenderer.setDrawBarOutline(false);
    stackedbarrenderer.setBaseItemLabelsVisible(true);
    stackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    stackedbarrenderer.setBaseItemLabelFont(StackedBarRenderer.DEFAULT_VALUE_LABEL_FONT.deriveFont(Font.BOLD));
    int height = (categorydataset.getColumnCount() * ChartUtils.STANDARD_BARCHART_ENTRY_HEIGHT * 2)
            + ChartUtils.STANDARD_BARCHART_ADDITIONAL_HEIGHT + 10;
    if (height > ChartUtils.MINIMUM_HEIGHT) {
        super.setHeight(height);
    } else {
        super.setHeight(ChartUtils.MINIMUM_HEIGHT);
    }
    Paint[] paints = this.datasetStrategy.getPaintColor();

    for (int i = 0; i < categorydataset.getRowCount() && i < paints.length; i++) {
        stackedbarrenderer.setSeriesPaint(i, paints[i]);
    }

}

From source file:org.operamasks.faces.render.graph.BarChartRenderer.java

protected JFreeChart createChart(UIChart comp) {
    Dataset dataset = createDataset(comp);
    JFreeChart chart = null;//from   w  w w .  j  a  va2  s .  c  o  m

    if (dataset instanceof CategoryDataset) {
        if (comp.isStacked()) {
            if (comp.isEffect3D()) {
                chart = ChartFactory.createStackedBarChart3D(null, null, null, (CategoryDataset) dataset,
                        getChartOrientation(comp), false, false, false);
            } else {
                chart = ChartFactory.createStackedBarChart(null, null, null, (CategoryDataset) dataset,
                        getChartOrientation(comp), false, false, false);
            }
        } else {
            if (comp.isEffect3D()) {
                chart = ChartFactory.createBarChart3D(null, null, null, (CategoryDataset) dataset,
                        getChartOrientation(comp), false, false, false);
            } else {
                chart = ChartFactory.createBarChart(null, null, null, (CategoryDataset) dataset,
                        getChartOrientation(comp), false, false, false);
            }
        }
    } else if (dataset instanceof IntervalXYDataset) {
        chart = ChartFactory.createXYBarChart(null, null, (dataset instanceof TimeSeriesCollection), null,
                (IntervalXYDataset) dataset, getChartOrientation(comp), false, false, false);
    }

    return chart;
}

From source file:org.jfree.chart.demo.ItemLabelDemo5.java

private static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createStackedBarChart("Item Label Demo 5", null, null, categorydataset,
            PlotOrientation.VERTICAL, false, true, false);
    jfreechart.setBackgroundPaint(new Color(255, 255, 255));
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    MyStackedBarRenderer mystackedbarrenderer = new MyStackedBarRenderer();
    categoryplot.setRenderer(mystackedbarrenderer);
    ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER,
            TextAnchor.CENTER, 0.0D);// w  ww  . j  av  a 2 s  .c  o m
    mystackedbarrenderer.setPositiveItemLabelPositionFallback(itemlabelposition);
    mystackedbarrenderer.setNegativeItemLabelPositionFallback(itemlabelposition);
    StandardCategoryItemLabelGenerator standardcategoryitemlabelgenerator = new StandardCategoryItemLabelGenerator(
            "{0}", NumberFormat.getInstance());
    mystackedbarrenderer.setBaseItemLabelGenerator(standardcategoryitemlabelgenerator);
    mystackedbarrenderer.setBaseItemLabelsVisible(true);
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setUpperBound(100D);
    return jfreechart;
}

From source file:dk.sdu.mmmi.featureous.views.featurecharacterization.FeatureViewChart.java

public FeatureViewChart(List<TraceModel> ftms, final boolean pkg) {
    this.pkg = pkg;
    data = new DefaultCategoryDataset();
    chart = ChartFactory.createStackedBarChart("Feature characterization", "Feature", "Scattering", data,
            PlotOrientation.VERTICAL, true, false, false);
    plot = (CategoryPlot) chart.getPlot();
    CategoryAxis xAxis = (CategoryAxis) plot.getDomainAxis();
    xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    xAxis.setMaximumCategoryLabelLines(2);
    //        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 v  a2s.  c o m
    //        chart.removeLegend();
    panel = new ChartPanel(chart);
    chart.setBackgroundPaint(Color.white);
    this.ftms = ftms;
    scattering = new ArrayList<Result>(
            new StaticScattering(pkg).calculateAndReturnAll(new HashSet<TraceModel>(ftms), null));
    Result.sortByName(scattering);
    for (Result r : scattering) {
        //            OutputUtil.log(r.name + ";" +r.value);
    }
    panel.getPopupMenu().setEnabled(false);//add(SVGExporter.createExportAction(chart, panel));
    StackedBarRenderer r2 = 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);
            }
        }
    };
    plot.setRenderer(r2, true);
    StackedBarRenderer r = (StackedBarRenderer) plot.getRenderer();
    r.setDrawBarOutline(true);
    plot.getRenderer().setOutlineStroke(new BasicStroke(0.1f));
    Controller.getInstance().getTraceSet().getSelectionManager().addSelectionListener(this);
}