Example usage for org.jfree.chart ChartFactory createBarChart

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

Introduction

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

Prototype

public static JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel,
        CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 

Source Link

Document

Creates a bar chart.

Usage

From source file:j2se.jfreechart.barchart.BarChartDemo6.java

/**
 * Creates a new demo.//from   w  w  w.  ja v a 2s .c o  m
 *
 * @param title  the frame title.
 */
public BarChartDemo6(final String title) {

    super(title);

    // create a dataset...
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.addValue(83.0, "First", "Factor 1");

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart(null, // chart title
            "Category", // domain axis label
            "Score (%)", // range axis label
            dataset, // data
            PlotOrientation.HORIZONTAL, false, // include legend
            true, false);

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.yellow); // not seen
    final CategoryPlot plot = chart.getCategoryPlot();
    //        plot.setInsets(new Insets(0, 0, 0, 0));
    plot.setRangeGridlinesVisible(false);
    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLowerMargin(0.20);
    domainAxis.setUpperMargin(0.20);
    domainAxis.setVisible(false);
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setRange(0.0, 100.0);
    rangeAxis.setVisible(false);
    // OPTIONAL CUSTOMISATION COMPLETED.

    // 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:userInterface.ManufacturerRole.ViewInventoryJPanel.java

private void printBarChart() {
    DefaultCategoryDataset barChartData = new DefaultCategoryDataset();
    for (Map.Entry<String, Integer> entry : vaccineNameVsNumber.entrySet()) {
        System.out.println("< " + entry.getKey() + " " + entry.getValue() + " >");

        barChartData.addValue(entry.getValue(), "Vaccine", entry.getKey());

    }/*from ww w  . ja va2  s  . c  o  m*/
    JFreeChart barChart = ChartFactory.createBarChart("Vaccine Inventory", "Vaccines", "Qty", barChartData,
            PlotOrientation.VERTICAL, false, true, false);

    CategoryPlot barChrt = barChart.getCategoryPlot();
    barChrt.setRangeGridlinePaint(Color.BLUE);

    ChartPanel panel = new ChartPanel(barChart);
    PanelChart.removeAll();
    PanelChart.add(panel, BorderLayout.CENTER);
    PanelChart.validate();

}

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

/**
 * Default constructor.//  w  ww . ja  v  a 2s . c  o m
 *
 * @param title  the frame title.
 */
public XMLBarChartDemo(final String title) {

    super(title);

    // create a dataset...
    CategoryDataset dataset = null;
    final URL url = getClass().getResource("/org/jfree/chart/demo/categorydata.xml");

    try {
        final InputStream in = url.openStream();
        dataset = DatasetReader.readCategoryDatasetFromXML(in);
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("Bar Chart", // chart title
            "Domain", "Range", dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, false);

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

    // 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:Graphic.camion.java

private void initialiser() {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    int k = 0;//from  ww  w. j  a  v  a 2s . c om
    for (int j = 0; j < categories.size(); j++) {
        for (int i = 0; i < series.size(); i++) {
            dataset.addValue(valeurs.get(k), series.get(i), categories.get(j));
            k++;
        }

    }
    JFreeChart chart = ChartFactory.createBarChart(titre, // chart title
            abscisse, // domain axis label
            ordonnee, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            legende, // include legend
            true, // tooltips
            false // URL
    );

    // definition de la couleur de fond
    //chart.setBackgroundPaint(couleurFond);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();

    //valeur comprise entre 0 et 1 transparence de la zone graphique
    plot.setBackgroundAlpha(0.9f);

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

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // pour la couleur des barres pour chaque serie
    for (int s = 0; s < series.size(); s++) {
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, couleursBarres[s], 0.0f, 0.0f, new Color(0, 40, 70));
        renderer.setSeriesPaint(s, gp0);

    }

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setFillZoomRectangle(true);
    chartPanel.setMouseWheelEnabled(true);

    add(chartPanel);
}

From source file:lu.lippmann.cdb.weka.SilhouetteUtil.java

/**
 * /*  ww w. jav a  2  s . c om*/
 * @param sils
 * @return
 */
public static JPanel buildSilhouettePanel(final Instances ds, final WekaClusteringResult result) {

    final Map<Integer, List<Double>> sils = computeSilhouette(ds, result);

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    final JFreeChart chart = ChartFactory.createBarChart("Silhouette", "Category", "Value", dataset,
            PlotOrientation.HORIZONTAL, true, true, false);

    int nbClass = sils.keySet().size();

    int id = 0;
    double minValue = 0;

    int counter[][] = new int[nbClass][4];
    for (int i = 0; i < nbClass; i++) {

        final double[] tree = ArrayUtils.toPrimitive(sils.get(i).toArray(new Double[0]));

        for (double val : tree) {
            if (val > 0.75) {
                dataset.addValue(val, "Cluster " + i + " ++", "" + id);
                counter[i][0]++;
            } else if (val > 0.50) {
                dataset.addValue(val, "Cluster " + i + " +", "" + id);
                counter[i][1]++;
            } else if (val > 0.25) {
                dataset.addValue(val, "Cluster " + i + " =", "" + id);
                counter[i][2]++;
            } else {
                dataset.addValue(val, "Cluster " + i + " -", "" + id);
                counter[i][3]++;
            }
            if (val < minValue) {
                minValue = val;
            }
            id++;
        }

    }

    final CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setBackgroundPaint(Color.WHITE);
    categoryplot.getDomainAxis().setVisible(false);
    categoryplot.setDomainGridlinesVisible(false);
    categoryplot.setRangeGridlinesVisible(false);
    categoryplot.getRangeAxis().setRange(minValue, 1.0);

    //Add line markers
    ValueMarker target = new ValueMarker(0.75);
    target.setPaint(Color.BLACK);
    target.setLabel("  ++");
    target.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    target.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    categoryplot.addRangeMarker(target);

    target = new ValueMarker(0.5);
    target.setPaint(Color.BLACK);
    target.setLabel("  +");
    target.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    target.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    categoryplot.addRangeMarker(target);

    target = new ValueMarker(0.25);
    target.setPaint(Color.BLACK);
    target.setLabel("  =");
    target.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    target.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    categoryplot.addRangeMarker(target);

    target = new ValueMarker(0);
    target.setPaint(Color.BLACK);
    target.setLabel("  -");
    target.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    target.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    categoryplot.addRangeMarker(target);

    //Remove visual effects on bar
    final BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
    barrenderer.setBarPainter(new StandardBarPainter());

    //set bar colors
    int p = 0;
    final int max = ColorHelper.COLORBREWER_SEQUENTIAL_PALETTES.size();

    for (int i = 0; i < nbClass; i++) {
        final Color[] color = new ArrayList<Color[]>(ColorHelper.COLORBREWER_SEQUENTIAL_PALETTES.values())
                .get((max - i) % max);
        final int nbColors = color.length;
        for (int k = 0; k < counter[i].length; k++) {
            if (counter[i][k] > 0)
                barrenderer.setSeriesPaint(p++, color[(nbColors - k - 3) % nbColors]);
        }
    }

    //remove blank line between bars
    barrenderer.setItemMargin(-dataset.getRowCount());

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setMouseWheelEnabled(true);
    chartPanel.setPreferredSize(new Dimension(1200, 900));
    chartPanel.setBorder(new TitledBorder("Silhouette plot"));
    chartPanel.setBackground(Color.WHITE);
    chart.setTitle("");
    return chartPanel;

}

From source file:org.openmrs.module.usagestatistics.web.view.chart.TimeChartView.java

@Override
protected JFreeChart createChart(Map<String, Object> model, HttpServletRequest request) {
    // Get the time function
    String func = ServletRequestUtils.getStringParameter(request, "func", "hour");

    UsageStatisticsService svc = Context.getService(UsageStatisticsService.class);

    int[][] timeStats = new int[0][0];
    String xAxisLabel = null;//ww  w  .  j  a  va 2  s.  com
    String[] categories = null;
    if (func.equals("hour")) {
        timeStats = svc.getHourStats(getFromDate(), getUntilInclusiveDate(), getLocation());
        xAxisLabel = ContextProvider.getMessage("usagestatistics.chart.hour");
    } else if (func.equals("dayofweek")) {
        timeStats = svc.getDayOfWeekStats(getFromDate(), getUntilInclusiveDate(), getLocation());
        xAxisLabel = ContextProvider.getMessage("usagestatistics.chart.day");
        categories = ContextProvider.getMessage("usagestatistics.chart.dayNames").split("\\|");
    }

    String yAxisLabel = ContextProvider.getMessage("usagestatistics.chart.records");
    String seriesView = ContextProvider.getMessage("usagestatistics.results.views");
    String seriesUpdates = ContextProvider.getMessage("usagestatistics.results.updates");

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int c = 0; c < timeStats.length; c++) {
        dataset.addValue(timeStats[c][0], seriesView, (categories != null) ? categories[c] : (c + ""));
        dataset.addValue(timeStats[c][1], seriesUpdates, (categories != null) ? categories[c] : (c + ""));
    }

    JFreeChart chart = ChartFactory.createBarChart(null, // Chart title
            xAxisLabel, // Domain axis label
            yAxisLabel, // Range axis label
            dataset, // Data
            PlotOrientation.VERTICAL, // Orientation
            true, // Include legend
            false, // Tooltips?
            false // URLs?
    );

    return chart;
}

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

/**
 * Creates a new demo instance./* ww w  . j  a  va  2s.c  o  m*/
 *
 * @param title  the frame title.
 */
public DualAxisDemo3(final String title) {

    super(title);

    final CategoryDataset dataset1 = createDataset1();

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("Dual Axis Chart", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset1, // dataset
            PlotOrientation.HORIZONTAL, // orientation
            true, // include legend
            true, false);

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

    // set the background color for the chart...
    chart.setBackgroundPaint(new Color(0xCC, 0xFF, 0xCC));
    //      chart.getLegend().setAnchor(Legend.WEST);

    // get a reference to the plot for further customisation...
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    //    plot.getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f);
    final CategoryDataset dataset2 = createDataset2();
    final ValueAxis axis2 = new NumberAxis("Secondary");
    plot.setRangeAxis(1, axis2);
    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    final CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    plot.setRenderer(1, renderer2);

    // OPTIONAL CUSTOMISATION COMPLETED.

    // 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:org.uncommons.watchmaker.swing.evolutionmonitor.IslandsView.java

IslandsView() {
    super(new BorderLayout());
    chart = ChartFactory.createBarChart("Island Population Fitness", "Island No.", "Candidate Fitness",
            bestDataSet, PlotOrientation.VERTICAL, true, // Legend
            false, // Tooltips
            false); // URLs
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.getDomainAxis().setLowerMargin(0.02);
    plot.getDomainAxis().setUpperMargin(0.02);
    ((BarRenderer) plot.getRenderer()).setShadowVisible(false);
    plot.getRangeAxis().setAutoRange(false);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    meanRenderer.setBaseLinesVisible(false);
    ChartPanel chartPanel = new ChartPanel(chart, ChartPanel.DEFAULT_WIDTH, ChartPanel.DEFAULT_HEIGHT,
            ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT,
            ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT, false, // Buffered
            false, // Properties
            true, // Save
            true, // Print
            false, // Zoom
            false); // Tooltips
    add(chartPanel, BorderLayout.CENTER);
    add(createControls(), BorderLayout.SOUTH);
}

From source file:herramientas.Ecualizacion_histograma.java

public void histograma() {
    JFrame vHist = new JFrame("Histograma");

    JFreeChart graf_hist = null;/*ww w . j av a  2 s .  co m*/

    DefaultCategoryDataset data = new DefaultCategoryDataset();

    for (int i = 0; i < 256; i++) {
        //            if(datos[i] != 0) //Mostrando solo los que son distintos de 0.
        data.addValue(datos[i], "Histograma frec. abs.", Integer.toString(i));
    }

    graf_hist = ChartFactory.createBarChart("Histograma de frecuencias absolutas", "Nivel RGB",
            "Nmero de pxeles", data, PlotOrientation.VERTICAL, false, false, false);

    ChartPanel panel = new ChartPanel(graf_hist);
    vHist.getContentPane().add(panel);
    vHist.pack();
    vHist.setVisible(true);

}

From source file:org.posterita.core.BarChart.java

public JFreeChart createChart() throws OperationException {
    if (dataset == null) {
        throw new OperationException("Cannot create Bar chart: cause -> dataset empty!");
    }//from   ww  w . j  ava2 s.  c  o m

    switch (type) {
    case BARCHART_FLAT:
        chart = ChartFactory.createBarChart(title, xLabel, yLabel, dataset, orientation, showLegend,
                showTooltip, true);
        break;

    case BARCHART_3D:
        chart = ChartFactory.createBarChart3D(title, xLabel, yLabel, dataset, orientation, showLegend,
                showTooltip, true);
        break;

    default:
        throw new OperationException(
                "Invalid barchart type! Can only be BarChart.BARCHART_FLAT or BarChart.BARCHART_3D");
    }

    //setting subtitle
    if (subtitle != null) {
        TextTitle title = new TextTitle(subtitle);
        chart.addSubtitle(title);
    }

    CategoryPlot plot = chart.getCategoryPlot();
    NumberAxis axis = (NumberAxis) plot.getRangeAxis();

    //setting tickUnits
    if (integerTickUnits)
        axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    BarRenderer barRender = (BarRenderer) plot.getRenderer();
    barRender.setMaximumBarWidth(maximumBarWidth);

    //displaying labels
    if (showLabels) {
        CategoryItemRenderer itemRender = plot.getRenderer();
        itemRender.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        itemRender.setItemLabelsVisible(true);
    }

    return chart;
}