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:biz.ixnay.pivot.charts.skin.jfree.BarChartViewSkin.java

@Override
protected JFreeChart createChart() {
    BarChartView chartView = (BarChartView) getComponent();

    String title = chartView.getTitle();
    String horizontalAxisLabel = chartView.getHorizontalAxisLabel();
    String verticalAxisLabel = chartView.getVerticalAxisLabel();
    boolean showLegend = chartView.getShowLegend();

    String seriesNameKey = chartView.getSeriesNameKey();
    List<?> chartData = chartView.getChartData();

    // TODO Make plot orientation a style property

    JFreeChart chart;//from  w w w. ja  v a 2  s.co  m
    ChartView.CategorySequence categories = chartView.getCategories();
    if (categories.getLength() > 0) {
        CategorySeriesDataset dataset = new CategorySeriesDataset(categories, seriesNameKey, chartData);

        if (stacked && threeDimensional) {
            chart = ChartFactory.createStackedBarChart3D(title, horizontalAxisLabel, verticalAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, false, false);
        } else if (stacked) {
            chart = ChartFactory.createStackedBarChart(title, horizontalAxisLabel, verticalAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, false, false);
        } else if (threeDimensional) {
            chart = ChartFactory.createBarChart3D(title, horizontalAxisLabel, verticalAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, false, false);
        } else {
            chart = ChartFactory.createBarChart(title, horizontalAxisLabel, verticalAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, false, false);
        }

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        CategoryAxis domainAxis = plot.getDomainAxis();
        CategoryLabelPositions categoryLabelPositions = CategoryLabelPositions
                .createUpRotationLabelPositions(categoryLabelRotation);
        domainAxis.setCategoryLabelPositions(categoryLabelPositions);
    } else {
        // TODO Make the dateAxis argument a style property
        chart = ChartFactory.createXYBarChart(title, horizontalAxisLabel, false, verticalAxisLabel,
                new IntervalSeriesDataset(seriesNameKey, chartData), PlotOrientation.VERTICAL, showLegend,
                false, false);
    }

    return chart;
}

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

public JFreeChart computeCategoryChart(final CategoryDataset categoryDataset) {
    final PlotOrientation orientation = computePlotOrientation();

    final JFreeChart chart;
    if (isThreeD()) {
        if (isStacked()) {
            chart = ChartFactory.createStackedBarChart3D(computeTitle(), getCategoryAxisLabel(),
                    getValueAxisLabel(), categoryDataset, orientation, isShowLegend(), false, false);
        } else {/*from   w ww.ja  v a 2s.com*/
            chart = ChartFactory.createBarChart3D(computeTitle(), getCategoryAxisLabel(), getValueAxisLabel(),
                    categoryDataset, orientation, isShowLegend(), false, false);
        }
        chart.getCategoryPlot().setDomainAxis(new FormattedCategoryAxis3D(getCategoryAxisLabel(),
                getCategoricalAxisMessageFormat(), getRuntime().getResourceBundleFactory().getLocale()));
    } else {
        if (isStacked()) {
            chart = ChartFactory.createStackedBarChart(computeTitle(), getCategoryAxisLabel(),
                    getValueAxisLabel(), categoryDataset, orientation, isShowLegend(), false, false);
        } else {
            chart = ChartFactory.createBarChart(computeTitle(), getCategoryAxisLabel(), getValueAxisLabel(),
                    categoryDataset, orientation, isShowLegend(), false, false);
        }
        chart.getCategoryPlot().setDomainAxis(new FormattedCategoryAxis(getCategoryAxisLabel(),
                getCategoricalAxisMessageFormat(), getRuntime().getResourceBundleFactory().getLocale()));
    }

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    configureLogarithmicAxis(plot);

    return chart;
}

From source file:org.matsim.core.utils.charts.StackedBarChart.java

private JFreeChart createChart(final String title, final String categoryAxisLabel, final String valueAxisLabel,
        final CategoryDataset dataset) {
    return ChartFactory.createStackedBarChart(title, categoryAxisLabel, valueAxisLabel, dataset,
            PlotOrientation.VERTICAL, true, // legend?
            false, // tooltips?
            false // URLs?
    );/*  ww  w. j a v a  2  s .  c  o  m*/
}

From source file:com.itemanalysis.jmetrik.graph.barchart.BarChartPanel.java

public void setGraph() throws IllegalArgumentException {
    boolean hasGroupingVariable = false;
    if (command.getFreeOption("groupvar").hasValue()) {
        hasGroupingVariable = true;//from   w ww.  ja  v a2 s  . com
    }

    String name = command.getFreeOption("variable").getString();
    String xLabel = name;

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    String yLabel = "";
    if (command.getSelectOneOption("yaxis").isValueSelected("freq")) {
        yLabel = "Frequency";
    } else {
        yLabel = "Percentage";
    }

    if (command.getSelectOneOption("layout").isValueSelected("stacked")) {
        chart = ChartFactory.createStackedBarChart(command.getFreeOption("title").getString(), xLabel, yLabel,
                dataset, chartOrientation, hasGroupingVariable, //only show legend if has grouping variable
                true, false);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

    } else if (command.getSelectOneOption("view").isValueSelected("3D")) {
        chart = ChartFactory.createBarChart3D(command.getFreeOption("title").getString(), xLabel, yLabel,
                dataset, chartOrientation, hasGroupingVariable, //only show legend if has grouping variable
                true, false);
    } else {
        chart = ChartFactory.createBarChart(command.getFreeOption("title").getString(), xLabel, yLabel, dataset,
                chartOrientation, hasGroupingVariable, //only show legend if has grouping variable
                true, false);

    }

    String sub = "";
    if (command.getFreeOption("subtitle").getString() != null) {
        sub = command.getFreeOption("subtitle").getString();
    }
    TextTitle subtitle1 = new TextTitle(sub);
    chart.addSubtitle(subtitle1);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setDomainGridlinesVisible(true);

    if (command.getSelectOneOption("layout").isValueSelected("layered")) {
        LayeredBarRenderer renderer = new LayeredBarRenderer();
        //            renderer.setDrawBarOutline(false);
        plot.setRenderer(renderer);
        plot.setRowRenderingOrder(SortOrder.DESCENDING);
    }

    chart.setPadding(new RectangleInsets(20.0, 5.0, 20.0, 5.0));

    ChartPanel panel = new ChartPanel(chart);
    panel.getPopupMenu().addSeparator();
    this.addJpgMenuItem(BarChartPanel.this, panel.getPopupMenu());

    panel.setPreferredSize(new Dimension(width, height));

    plot.setBackgroundPaint(Color.WHITE);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    //                plot.setForegroundAlpha(0.80f);
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(true);
    renderer.setShadowVisible(false);

    this.setBackground(Color.WHITE);
    this.add(panel);

}

From source file:edu.ucla.stat.SOCR.chart.demo.StackedBarChartDemo1.java

/**
 * Creates a sample chart.//from  ww  w  .j  ava 2 s .c o  m
 * 
 * @param dataset  the dataset for the chart.
 * 
 * @return a sample chart.
 */
protected JFreeChart createChart(CategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // chart title
            domainLabel, // domain axis label
            rangeLabel, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            !legendPanelOn, // legend
            true, // tooltips
            false // urls
    );
    chart.setBackgroundPaint(Color.white);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);

    StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setBaseItemLabelsVisible(true);
    renderer.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());
    renderer.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator());

    setCategorySummary(dataset);
    return chart;

}

From source file:edu.ucla.stat.SOCR.chart.demo.StackedBarChartDemo3.java

/**
 * Creates a sample chart.//from   w w w.  j  ava2s  .  c  o  m
 * 
 * @param dataset  the dataset for the chart.
 * 
 * @return a sample chart.
 */
protected JFreeChart createChart(CategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // chart title
            domainLabel, // domain axis label
            rangeLabel, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            !legendPanelOn, // legend
            false, // tooltips
            false // urls
    );
    CategoryPlot plot = chart.getCategoryPlot();
    CategoryItemRenderer renderer = new ExtendedStackedBarRenderer();
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    plot.setRenderer(renderer);

    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerMargin(0.15);
    rangeAxis.setUpperMargin(0.15);

    StackedBarRenderer renderer2 = (StackedBarRenderer) plot.getRenderer();
    renderer2.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator());

    setCategorySummary(dataset);
    return chart;

}

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

/**
 * Creates the JFree chart based on the chart document, data provided and
 * type of chart expected./*ww  w.j  av a2  s.co  m*/
 * </p>
 * @param chartDocContext  Chart documument context for the current chart.
 * @param data             Data for the current chart.
 * @param chartType        Bar chart type (interval, stacked, default/basic)
 * @return Returns the JFree chart object (created by the method)
 */
public JFreeChart createChart(final ChartDocumentContext chartDocContext, final ChartTableModel data,
        final String chartType) {
    final ChartDocument chartDocument = chartDocContext.getChartDocument();
    final String title = getTitle(chartDocument);
    final String valueCategoryLabel = getValueCategoryLabel(chartDocument);
    final String valueAxisLabel = getValueAxisLabel(chartDocument);
    final PlotOrientation orientation = getPlotOrientation(chartDocument);
    final boolean legend = getShowLegend(chartDocument);
    final boolean toolTips = getShowToolTips(chartDocument);

    DefaultCategoryDataset categoryDataset = null;
    final DefaultIntervalCategoryDataset intervalCategoryDataset = null;

    if (JFreeBarChartTypes.INTERVAL.equalsIgnoreCase(chartType)) {
        logger.error(Messages.getErrorString("JFreeBarChartGenerator.INFO_INTERVAL_CHART_NOT_SUPPORTED")); //$NON-NLS-1$
    } else {
        categoryDataset = datasetGeneratorFactory.createDefaultCategoryDataset(chartDocContext, data);
        if (categoryDataset == null) {
            logger.error(Messages.getErrorString("JFreeChartFactoryEngine.ERROR_0001_DATASET_IS_NULL")); //$NON-NLS-1$
            return null;
        }
    }

    final JFreeChart chart;
    if (JFreeBarChartTypes.STACKED.equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createStackedBarChart(title, valueCategoryLabel, valueAxisLabel, categoryDataset,
                orientation, legend, toolTips, false);
    } else if (JFreeBarChartTypes.INTERVAL.equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createBarChart(title, valueCategoryLabel, valueAxisLabel, intervalCategoryDataset,
                orientation, legend, toolTips, false);
    } else if (JFreeBarChartTypes.WATERFALL.equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createBarChart(title, valueCategoryLabel, valueAxisLabel, intervalCategoryDataset,
                orientation, legend, toolTips, false);
    } else {
        chart = ChartFactory.createBarChart(title, valueCategoryLabel, valueAxisLabel, categoryDataset,
                orientation, legend, toolTips, false);
    }

    final ChartElement[] seriesElements = chartDocument.getRootElement()
            .findChildrenByName(ChartElement.TAG_NAME_SERIES);
    final CategoryPlot categoryPlot = chart.getCategoryPlot();
    if (seriesElements != null && categoryPlot != null) {
        setSeriesAttributes(seriesElements, data, categoryPlot);
        setAxisMargins(chartDocument, categoryPlot);
    }

    return chart;
}

From source file:com.swordlord.gozer.components.wicket.graph.GWStackedBarChartPanel.java

public GWStackedBarChartPanel(String id, IModel<?> model, GStackedBarChart child) {
    super(id, model);

    gchart = child;/*from w  w w  .j a v  a 2 s  .  c o  m*/
    DataBindingMember dbMemberRowKey = child.getDataBindingMemberRowKey();
    DataBindingMember dbMemberTargetId = child.getDataBindingMemberTargetId();
    DataBindingMember dbMemberColKey = child.getDataBindingMemberColKey();
    DataBindingMember dbMemberValue = child.getDataBindingMemberValue();
    DataBindingManager dbManager = child.getDataBindingManager();

    DefaultCategoryDataset dcd = new DefaultCategoryDataset();

    List<DataRowBase> rows = dbManager.getRows(dbMemberValue);

    // if the graph has some ordering info in the format of "<field> ASCENDING,<field2> DESCENDING"
    if (child.hasOrdering()) {
        List<Ordering> ordering = child.formatOrdering(child.getOrdering());
        OrderingEx.orderList(rows, ordering);
    }

    for (int j = 0; j < rows.size(); j++) {
        DataRowBase row = rows.get(j);

        String strKey = row.getPropertyAsStringForce(dbMemberRowKey.getRelativePathWithField());

        dcd.setValue(row.getPropertyAsInt(dbMemberValue.getRelativePathWithField()), strKey,
                row.getPropertyAsStringForce(dbMemberColKey.getRelativePathWithField()));

        if (dbMemberTargetId != null) {
            _target.put(strKey, row.getPropertyAsStringForce(dbMemberTargetId.getDataBindingFieldName()));
        }
    }

    JFreeChart chart = ChartFactory.createStackedBarChart(child.getTitle(), child.getCategoryAxisLabel(),
            child.getValueAxisLabel(), dcd, child.getOrientation(), child.getLegend(), false, false);

    // Do this in a more static way!
    StackedBarRenderer.setDefaultBarPainter(new StandardBarPainter());

    //chart.setBackgroundPaint(Color.white);
    if (child.getSubTitle() != null) {
        chart.addSubtitle(new TextTitle(child.getSubTitle()));
    }

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDrawingSupplier(child.getDrawingSupplier());

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVisible(child.getCategoryAxisVisible());

    ValueAxis valueAxis = plot.getRangeAxis();
    valueAxis.setVisible(child.getValueAxisVisible());

    /*
    //CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
    for (int j = 0; j < rowKey.length; j++)
    {
       renderer.setSeriesItemLabelGenerator(j, new LabelGenerator(j, rowKey[j]));
       renderer.setSeriesItemLabelsVisible(j, true);
    }
    */
    StackedBarRenderer renderer = new StackedBarRenderer();

    for (int j = 0; j < dcd.getRowCount(); j++) {
        renderer.setSeriesItemLabelGenerator(j, new StandardCategoryItemLabelGenerator());
        renderer.setSeriesItemLabelsVisible(j, true);
    }

    //renderer.setLegendItemLabelGenerator(new LabelGenerator());
    renderer.setShadowVisible(false);

    plot.setRenderer(renderer);

    ChartImage image = new ChartImage("chart", chart, child.getWidth(800), child.getHeight(800));
    String mapName = child.getCaption();
    add(image);

    DynamicImageMap imageMap = constructImageMap(image, mapName);
    if (!child.isClickable()) {
        imageMap.setVisible(false);
    } else {
        image.add(new AttributeModifier("usemap", new Model<String>("#" + mapName)));
    }
    add(imageMap);
}

From source file:org.fhaes.fhrecorder.view.ColorBarGraph.java

/**
 * Creates the chart with the data from the given data set.
 * //from w w  w.j  av  a2  s  .co m
 * @param dataset the data to plot.
 * @return the chart.
 */
private static JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", dataset, PlotOrientation.VERTICAL,
            false, true, false);

    StackedBarRenderer renderer = new StackedBarRenderer(true);
    renderer.setShadowVisible(false);
    renderer.setBarPainter(new StandardBarPainter()); // Remove shine
    renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setRenderer(renderer);

    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(new Color(192, 192, 192));

    plot.getDomainAxis().setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
    plot.getDomainAxis().setVisible(false);
    plot.getRangeAxis().setVisible(false);
    plot.getDomainAxis().setLowerMargin(.025);
    plot.getDomainAxis().setUpperMargin(.025);

    chart.setBackgroundPaint(new Color(214, 217, 233, 30));

    return chart;
}

From source file:fi.smaa.jsmaa.gui.SMAATRIGUIFactory.java

@Override
public ViewBuilder buildView(Object o) {
    if (o instanceof Alternative && smaaModel.getCategories().contains(o)) {
        return new ViewBuilder() {
            @Override//  w  w  w  .j  a  v a  2s  .c  o  m
            public JComponent buildPanel() {
                return new JPanel();
            }
        };
    } else if (o == treeModel.getModelNode()) {
        return new TechnicalParameterView(smaaModel);
    } else if (o == treeModel.getCatAccNode()) {
        final JFreeChart chart = ChartFactory.createStackedBarChart("", "Alternative", "Category Acceptability",
                categoryAcceptabilityDataset, PlotOrientation.VERTICAL, true, true, false);
        chart.getCategoryPlot().getRangeAxis().setUpperBound(1.0);
        ResultsTable table = new ResultsTable(categoryAcceptabilityTM);
        table.setAutoCreateRowSorter(true);
        table.setDefaultRenderer(Object.class, new ResultsCellColorRenderer(1.0));
        return new ViewWithHeader("Category acceptability indices",
                new ResultsView(parent, table, chart, FileNames.ICON_SCRIPT));
    } else if (o == treeModel.getCategoriesNode()) {
        return new ViewWithHeader("Categories (in ascending order, top = worst)",
                new AlternativeInfoView(smaaModel.getCategories()));
    } else {
        return super.buildView(o);
    }
}