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.jfree.chart.demo.PopulationChartDemo.java

/**
 * Creates a new demo.//from   w w  w  .j  a  v  a 2  s.  co  m
 *
 * @param title  the frame title.
 */
public PopulationChartDemo(final String title) {

    super(title);
    final CategoryDataset dataset = createDataset();

    // create the chart...
    final JFreeChart chart = ChartFactory.createStackedBarChart("Population Chart Demo", "Age Group", // domain axis label
            "Population (millions)", // range axis label
            dataset, // data
            PlotOrientation.HORIZONTAL, true, // include legend
            true, // tooltips
            false // urls
    );

    // add the chart to a panel...
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);

}

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);/*from ww  w. j a v a  2  s .com*/
    //        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:net.pickapack.chart.BarPlotFrame.java

/**
 * Create a bar plot frame.//  w  w  w.j ava  2  s. c  om
 *
 * @param barPlot the bar plot
 * @param domainAxisLabel the domain axis label
 * @param width the width
 * @param height the height
 */
public BarPlotFrame(BarPlot<ItemT> barPlot, String domainAxisLabel, int width, int height) {
    super(barPlot.getTitle());

    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();

    for (ItemT item : barPlot.getItems()) {
        if (barPlot.getPredicate().apply(item)) {
            for (SubBarPlot<ItemT> subBarPlot : barPlot.getSubBarPlots()) {
                dataSet.addValue(subBarPlot.getGetValueCallback().apply(item), subBarPlot.getTitle(),
                        subBarPlot.getGetTitleCallback().apply(item));
            }
        }
    }

    JFreeChart chart = barPlot.isStacked()
            ? ChartFactory.createStackedBarChart(barPlot.getTitle(), domainAxisLabel, barPlot.getTitleY(),
                    dataSet, PlotOrientation.VERTICAL, true, true, false)
            : ChartFactory.createBarChart(barPlot.getTitle(), domainAxisLabel, barPlot.getTitleY(), dataSet,
                    PlotOrientation.VERTICAL, true, true, false);

    CategoryPlot plot = chart.getCategoryPlot();

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

    ChartPanel chartPanel = new ChartPanel(chart, false);
    chartPanel.setPreferredSize(new Dimension(width, height));
    chartPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    setContentPane(chartPanel);
}

From source file:net.sqs2.omr.result.chart.StackedBarChart.java

public static void write(OutputStream outputStream, int width, int height, LegendHandler legendHandler,
        StatisticsContentsWriter statisticsContentsFactory) {
    // (1)create dataset
    List<Legend> legendList = legendHandler.getLegendList();
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    Legend legend0 = legendList.get(0);/*from ww w  . j  a  v a2 s.c  o m*/
    Legend legend1 = legendList.get(1);
    for (FormArea formArea0 : legend0.formAreaList) {
        int itemIndex0 = formArea0.getItemIndex();
        for (FormArea formArea1 : legend1.formAreaList) {
            int itemIndex1 = formArea1.getItemIndex();
            String key = createKey(legendHandler.getAxis(), itemIndex0, itemIndex1);
            int count = statisticsContentsFactory.getCount(key);
            dataset.addValue(count, formArea1.getItemLabel(), formArea0.getItemLabel());
        }
        if (legend0.primaryFormArea.isSelectSingle()) {
            String key = createKey(legendHandler.getAxis(), itemIndex0, -1);
            int count = statisticsContentsFactory.getCount(key);
            dataset.addValue(count, ChartConstants.NO_ANSWER, formArea0.getItemLabel());
        }
    }
    if (legend1.primaryFormArea.isSelectSingle()) {
        for (FormArea formArea1 : legend1.formAreaList) {
            int itemIndex1 = formArea1.getItemIndex();
            String key = createKey(legendHandler.getAxis(), -1, itemIndex1);
            int count = statisticsContentsFactory.getCount(key);
            dataset.addValue(count, formArea1.getItemLabel(), ChartConstants.NO_ANSWER);
        }
    }
    if (legend1.primaryFormArea.isSelectSingle()) {
        String key = createKey(legendHandler.getAxis(), -1, -1);
        int count = statisticsContentsFactory.getCount(key);
        dataset.addValue(count, ChartConstants.NO_ANSWER, ChartConstants.NO_ANSWER);
    }

    // String title = StringUtil.join(formAreaList.get(0).getHints(), "\n");
    String title = "";
    // (2)create JFreeChart instance
    // JFreeChart chart =
    // ChartFactory.createBarChart(StringUtil.join(formAreaList.get(0).getHints(),
    // "\n"),
    JFreeChart chart = ChartFactory.createStackedBarChart(title, legend0.primaryFormArea.getLabel(),
            legend1.primaryFormArea.getLabel(), dataset, PlotOrientation.HORIZONTAL, true, false, false);

    // (3)create chart image
    try {
        ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
    }
}

From source file:edu.cuny.cat.ui.ScorePlotPanel.java

public ScorePlotPanel() {

    registry = GameController.getInstance().getRegistry();

    setTitledBorder("Scores");

    dataset = new DefaultCategoryDataset();

    chart = ChartFactory.createStackedBarChart("", "", "", dataset, PlotOrientation.HORIZONTAL, true, true,
            false);//from ww  w.  j  a v  a2s.co  m
    // chart.setAntiAlias(false);
    chart.setBackgroundPaint(getBackground());
    final CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setBackgroundPaint(Color.lightGray);
    categoryplot.setRangeGridlinePaint(Color.white);
    final StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer();
    UIUtils.setDefaultBarRendererStyle(stackedbarrenderer);
    stackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    stackedbarrenderer.setBaseItemLabelsVisible(true);
    final ChartPanel chartPanel = new ChartPanel(chart);
    add(chartPanel, BorderLayout.CENTER);
}

From source file:GraphingProject.TheJApplet.java

private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createStackedBarChart("Stacked Bar Chart Demo 1", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            true, // legend
            true, // tooltips
            false // urls
    );/*from ww  w .  j av a2 s. com*/
    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 .  c om*/
    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: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);/*  w w w .ja va2 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:playground.yu.utils.charts.StackedBarChart.java

private JFreeChart createChart(String title, String axisLabel, String axisLabel2,
        DefaultCategoryDataset dataset, PlotOrientation plotOrientation) {
    JFreeChart chart = ChartFactory.createStackedBarChart(title, axisLabel, axisLabel2, dataset,
            plotOrientation, true, true, false);
    return chart;
}

From source file:edu.cuny.cat.ui.ProfitPlotPanel.java

public ProfitPlotPanel() {

    shoutSet = Collections.synchronizedSet(new HashSet<Shout>());

    registry = GameController.getInstance().getRegistry();

    setTitledBorder("Income and Expenses");

    dataset = new DefaultCategoryDataset();

    final JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", dataset, PlotOrientation.HORIZONTAL,
            true, true, false);//from w w w  .j  a v  a  2 s. com
    // chart.setAntiAlias(false);
    chart.setBackgroundPaint(getBackground());
    final CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setBackgroundPaint(Color.lightGray);
    categoryplot.setRangeGridlinePaint(Color.white);
    final StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer();
    UIUtils.setDefaultBarRendererStyle(stackedbarrenderer);
    stackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    stackedbarrenderer.setBaseItemLabelsVisible(true);
    final ChartPanel chartPanel = new ChartPanel(chart);
    add(chartPanel, BorderLayout.CENTER);
}