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.BarChartDemo4.java

/**
 * Creates a sample chart./*from  ww  w. j  av a  2  s . c  o m*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips?
            false // URLs?
    );

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

    // set the background color for the chart...
    chart.setBackgroundPaint(new Color(0xBBBBDD));

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

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    final BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setMaximumBarWidth(0.10);

    // set up gradient paints for series...
    final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray);
    final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray);
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);

    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}

From source file:ANNFileDetect.EncogTestClass.java

private void drawchart(TreeMap<Double, Integer> ht, String file, int minima) throws IOException {
    DefaultCategoryDataset ds = new DefaultCategoryDataset();
    //XYDataset xy = new XYDataset();
    /*/*from w  ww. j a  v a 2 s  . c  om*/
       * Enumeration<Double> e = ht.keys(); while (e.hasMoreElements()) {
       * Double tmp = e.nextElement(); //ds.addValue(ht.get(tmp), "Times",
       * tmp); if (ht.get(tmp) > 100) ds.addValue(ht.get(tmp), "Times", tmp);
       * }
       */
    for (Map.Entry<Double, Integer> entry : ht.entrySet()) {
        //ds.addValue(ht.get(tmp), "Times", tmp); 
        if (entry.getValue() > minima) {
            ds.addValue(entry.getValue(), "Times", entry.getKey());
        }
    }
    /*
     * while (e.hasMoreElements()) { Double tmp = e.nextElement();
     * //ds.addValue(ht.get(tmp), "Times", tmp); if (ht.get(tmp) > 100)
     * ds.addValue(ht.get(tmp), "Times", tmp); }
     */
    JFreeChart chart = ChartFactory.createBarChart(file, "quantity", "value", ds, PlotOrientation.VERTICAL,
            true, true, false);
    //JFreeChart chart = ChartFactory.createScatterPlot(file, "quantity", "value",ds, PlotOrientation.VERTICAL, true, true, false);
    String dlt = "/";
    String[] tmpfl = file.split(dlt);
    String crp = tmpfl[tmpfl.length - 1];
    String[] actfl = crp.split("\\.");
    ChartUtilities.saveChartAsJPEG(new File("/tmp/charts/" + actfl[1].toUpperCase() + actfl[0]), chart, 6000,
            1200);
    files++;
}

From source file:Reportes.BarChart.java

public ChartPanel reporteVehiculosAgregados(DefaultCategoryDataset data) {
    JFreeChart chart = ChartFactory.createBarChart("Reporte de cantidad de vehiculos aadidos por sede",
            "Sedes", "Cantidad", data, PlotOrientation.VERTICAL, true, true, true);

    CategoryPlot categoryP = chart.getCategoryPlot();
    BarRenderer renderer = (BarRenderer) categoryP.getRenderer();
    renderer.setMaximumBarWidth(0.35);//from ww  w.  j  a v  a 2  s .c o m
    Color color = new Color(67, 165, 208);
    renderer.setSeriesPaint(0, color);

    ChartPanel panel = new ChartPanel(chart, true, true, true, false, false);
    panel.setSize(ancho, alto);

    return panel;
}

From source file:com.bdb.weather.display.day.DayRainPane.java

/**
 * Constructor./*  w  ww .j a v  a 2  s  . co m*/
 */
public DayRainPane() {
    setPrefSize(400, 300);
    chart = ChartFactory.createBarChart("Water Cycle", "Hour", "", null, PlotOrientation.VERTICAL, true, true,
            false);

    chartViewer = new ChartViewer(chart);

    rainPlot = (CategoryPlot) chart.getPlot();
    rainPlot.setNoDataMessage("There is no data for the specified day");

    BarRenderer renderer = (BarRenderer) rainPlot.getRenderer();
    renderer.setBasePaint(Color.BLUE);
    renderer.setSeriesPaint(0, Color.BLUE);
    renderer.setSeriesPaint(1, Color.RED);
    renderer.setSeriesItemLabelGenerator(0, new RainItemLabelGenerator(
            StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, Depth.getDefaultFormatter()));
    StandardCategoryToolTipGenerator ttgen = new StandardCategoryToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, Depth.getDefaultFormatter());
    rainPlot.getRenderer().setSeriesToolTipGenerator(0, ttgen);

    NumberFormat etFormatter = (NumberFormat) Depth.getDefaultFormatter().clone();
    etFormatter.setMaximumFractionDigits(etFormatter.getMaximumFractionDigits() + 1);
    renderer.setSeriesItemLabelGenerator(1, new RainItemLabelGenerator(
            StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, etFormatter));
    ttgen = new StandardCategoryToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
            etFormatter);
    rainPlot.getRenderer().setSeriesToolTipGenerator(1, ttgen);

    rainPlot.setRangeAxis(valueAxis);
    rainPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);

    dataTable = new TableView();

    TableColumn<RainItem, Integer> hourColumn = new TableColumn<>(HOUR_ROW_KEY);
    hourColumn.setCellValueFactory(new PropertyValueFactory(HOUR_ROW_KEY));
    dataTable.getColumns().add(hourColumn);

    TableColumn<RainItem, Depth> rainfallColumn = new TableColumn<>("Rainfall");
    rainfallColumn.setCellValueFactory(new PropertyValueFactory(RAIN_ROW_KEY));
    dataTable.getColumns().add(rainfallColumn);

    TableColumn<RainItem, Depth> etColumn = new TableColumn<>(ET_ROW_KEY);
    etColumn.setCellValueFactory(new PropertyValueFactory(ET_ROW_KEY));
    dataTable.getColumns().add(etColumn);

    this.setTabContents(chartViewer, dataTable);
}

From source file:web.diva.server.model.SparkLineGenerator.java

/**
 * Creates a new JSparklinesBarChartTableCellRenderer. Use this constructor
 * when positive and negative values are to be plotted. Note that to use the
 * significance color coding the object in the table cell has to be of type
 * XYDataPoint.//ww w .j a v a 2 s .  c o m
 *
 * @param plotOrientation the orientation of the plot
 * @param minValue the minium value to be plotted, used to make sure that
 * all plots in the same column has the same minimum value and are thus
 * comparable
 * @param maxValue the maximum value to be plotted, used to make sure that
 * all plots in the same column has the same maximum value and are thus
 * comparable
 * @param negativeValuesColor the color to use for the negative values if
 * two sided data is shown
 * @param positiveValuesColor the color to use for the positive values if
 * two sided data is shown, and the color used for one sided data
 * @param nonSignificantColor the color to use for the non-significant
 * values
 * @param significanceLevel the upper level for when to use the significant
 * values color
 * @throws IllegalArgumentException if minValue &gt; maxValue
 */
public SparkLineGenerator(PlotOrientation plotOrientation, double minValue, double maxValue,
        Color negativeValuesColor, Color positiveValuesColor, Color nonSignificantColor,
        double significanceLevel) {
    //         System.out.println("the 2 line mode ");

    this.negativeValuesColor = negativeValuesColor;
    this.positiveValuesColor = positiveValuesColor;
    rangeFactor = Math.max(Math.abs(maxValue), Math.abs(minValue));
    this.maxValue = maxValue / rangeFactor;
    this.minValue = minValue / rangeFactor;

    this.nonSignificantColor = nonSignificantColor;
    this.significanceLevel = significanceLevel;

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    chart = ChartFactory.createBarChart(null, null, null, dataset, plotOrientation, false, false, false);
    //        this.chartPanel = new ChartPanel(chart);
    plot = chart.getCategoryPlot();

    plot.getRangeAxis().setRange(this.minValue, this.maxValue);

    // hide unwanted chart details
    plot.setOutlineVisible(false);
    plot.getRangeAxis().setVisible(false);
    plot.getDomainAxis().setVisible(false);
    plot.setRangeGridlinesVisible(false);

    plot.setBackgroundAlpha(0.0f);
    plot.setBackgroundImageAlpha(0.0f);
    plot.setBackgroundPaint(plotBackgroundColor);

    chart.setBackgroundImageAlpha(0.0f);
    chart.setBackgroundPaint(plotBackgroundColor);

    if (minValue > maxValue) {
        throw new IllegalArgumentException(
                "minValue has to be smaller than maxValue! Current values: minValue: " + minValue
                        + ", maxValue: " + maxValue + ".");
    }
}

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

/**
 * Creates a sample chart.// w w w .  j a  v a  2  s.co m
 * 
 * @param dataset  the dataset.
 * 
 * @return a sample chart.
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo 3", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            false, // include legend
            true, false);

    chart.setBackgroundPaint(Color.lightGray);

    // get a reference to the plot for further customisation...
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setNoDataMessage("NO DATA!");

    final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green,
            Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue });
    //        renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    renderer.setItemLabelsVisible(true);
    final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER,
            TextAnchor.CENTER, 45.0);
    renderer.setPositiveItemLabelPosition(p);
    plot.setRenderer(renderer);

    // change the margin at the top of the range axis...
    final ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerMargin(0.15);
    rangeAxis.setUpperMargin(0.15);

    return chart;

}

From source file:barChart1.BarChartDemo.java

/**
 * Creates a sample chart./* w  w  w . ja  v a  2 s  .c  o m*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private static JFreeChart createChart(CategoryDataset dataset) {

    // create the chart...
    JFreeChart chart = ChartFactory.createBarChart("Belgium vs Italy vs Ireland ", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );

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

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

    // get a reference to the plot for further customisation...
    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(0, 0, 64));
    GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 64, 0));
    GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(64, 0, 0));

    GradientPaint gp3 = new GradientPaint(0.0f, 0.0f, Color.yellow, 0.0f, 0.0f, new Color(64, 0, 0));
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    renderer.setSeriesPaint(2, gp2);
    renderer.setSeriesPaint(3, gp2);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}

From source file:dumbara.view.Chart1.java

public static void byEmployee() {
    DefaultCategoryDataset objDataset = new DefaultCategoryDataset();
    try {/*from  w w w  .j  ava  2 s  . co  m*/
        dumbara.model.Chart1[] chart1 = LoanController.getLoanAmounts();
        for (dumbara.model.Chart1 chart11 : chart1) {
            objDataset.setValue(chart11.getLoanamount(), "Loan amounts", chart11.getEmpID());
        }
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(Chart1.class.getName()).log(Level.SEVERE, null, ex);
    }
    //
    JFreeChart objChart = ChartFactory.createBarChart("Total Loan Amounts of Employees", "Employee ID",
            "Loan Amount (Rs.)", objDataset, PlotOrientation.VERTICAL, true, true, false);
    ChartFrame frame = new ChartFrame("Data Analysis Wizard - v2.1.4", objChart);
    frame.pack();
    frame.setSize(1000, 600);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}

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

/**
 * Creates a sample chart.//from w w w .j  a v a2  s. co  m
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo 8", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips?
            false // URLs?
    );

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

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

    // get a reference to the plot for further customisation...
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setUpperMargin(0.15);

    // disable bar outlines...
    final CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);

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

    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}

From source file:grafici.StatisticheBarChart.java

/**
 * Creates a sample chart.//from w w w  .ja  v a2s  . co m
 * 
 * @param dataset
 *            the dataset.
 * 
 * @return The chart.
 */
private static JFreeChart createChart(CategoryDataset dataset, Table results, int variabile, int valore) {

    // create the chart...
    JFreeChart chart = ChartFactory.createBarChart(titolo, // chart
            // title
            "", // domain axis label
            results.getColumn(valore).getText().toUpperCase(), // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );

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

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

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

    // ******************************************************************
    // More than 150 demo applications are included with the JFreeChart
    // Developer Guide...for more information, see:
    //
    // > http://www.object-refinery.com/jfreechart/guide.html
    //
    // ******************************************************************

    // set the range axis to display integers only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64));
    GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0));
    GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(64, 0, 0));
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    renderer.setSeriesPaint(2, gp2);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 12.0));
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}