Example usage for org.jfree.chart.renderer CategoryItemRenderer setLabelGenerator

List of usage examples for org.jfree.chart.renderer CategoryItemRenderer setLabelGenerator

Introduction

In this page you can find the example usage for org.jfree.chart.renderer CategoryItemRenderer setLabelGenerator.

Prototype

public void setLabelGenerator(CategoryLabelGenerator generator);

Source Link

Document

Sets the item label generator for ALL series and sends a RendererChangeEvent to all registered listeners.

Usage

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

/**
 * Creates a sample chart./*w  w w.j  a  v  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("Item Label Demo 1", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips?
            false // URLs?
    );

    chart.setBackgroundPaint(Color.white);

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

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setUpperMargin(0.15);

    final CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setLabelGenerator(new LabelGenerator(50.0));
    renderer.setItemLabelsVisible(true);

    return chart;

}

From source file:gui.BarChart.java

/**
 * Zusammenbauen des Diagrammes./*ww  w  .j a  v  a  2s .c  o  m*/
 * 
 * @return diagramm
 */
private JFreeChart createChart() {

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("", // berschrift
            label_x_axis, // x label
            label_y_axis, // y label
            dataset, // datenstze
            PlotOrientation.VERTICAL, // vertikale balken
            true, // mit legende
            true, // mit tooltips
            false // URLs???
    );

    // Layoutanpassungen im Folgende:

    // allg. Hintergrundfarbe
    chart.setBackgroundPaint(Color.white);

    // Referenz auf Zeichnung:
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setNoDataMessage("NO DATA!");

    // aussehen des eigentlichen diagrammes:
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(true);

    // skaleneinteilung:
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setUpperMargin(0.1);

    // kategorien renderer einstellungen verndern:                
    final CategoryItemRenderer categories = plot.getRenderer();
    // casten in bar renderer:
    final BarRenderer bars = (BarRenderer) categories;

    // werte im diagramm anzeigen:
    categories.setLabelGenerator(new StandardCategoryLabelGenerator());
    categories.setItemLabelsVisible(true);
    final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER,
            TextAnchor.CENTER, -Math.PI / 6);

    categories.setPositiveItemLabelPosition(p);
    categories.setNegativeItemLabelPosition(p);

    plot.setRenderer(categories);

    // farbverlauf der serien 1 und 2
    final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, new Color(127, 127, 255), 0.0f, 0.0f,
            new Color(127, 127, 127));
    final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, new Color(255, 127, 127), 0.0f, 0.0f,
            new Color(127, 127, 127));
    bars.setSeriesPaint(0, gp0);
    bars.setSeriesPaint(1, gp1);
    // keine umrandung der balken
    bars.setDrawBarOutline(false);

    // abstand der zahlen vom balken
    bars.setItemLabelAnchorOffset(13);

    // x achsenbeschriftung
    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));

    return chart;

}

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

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

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

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setUpperMargin(0.25);

    final CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setItemLabelsVisible(true);

    // use one or the other of the following lines to see the different modes for
    // the label generator...
    renderer.setLabelGenerator(new LabelGenerator(null));
    // renderer.setLabelGenerator(new LabelGenerator(0));

    return chart;

}

From source file:hr.restart.util.chart.ChartXY.java

/**
 * Creates a BAR CHART//from   w w  w  . j  a  va2  s  . c om
 * @param dataset The org.jfree.data.CategoryDataset
 * @param title The title
 * @return org.jfree.chart.JFreeChart
 */
private JFreeChart createBarChart(final CategoryDataset dataset, String title, PlotOrientation orientation) {

    final JFreeChart chart = ChartFactory.createBarChart(title, // chart title
            "", // domain axis label
            "", // range axis label
            dataset, // data
            orientation, // the plot orientation
            false, // include legend
            true, false);

    chart.setBackgroundPaint(Color.white);

    // the subtitle from the combobox        
    if (jcb != null)
        chart.addSubtitle(new TextTitle(jcb.getSelectedItem().toString()));

    //subtitles setted by the user.
    if (getSubtitles() != null)
        for (int i = 0; i < getSubtitles().size(); i++) {
            chart.addSubtitle(new TextTitle(getSubtitles().get(i).toString()));
        }

    final Plot plot = chart.getPlot();

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

    categoryPlot.setNoDataMessage("NO DATA!");

    categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green,
            Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue });
    categoryPlot.setRenderer(renderer);

    renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    renderer.setItemLabelsVisible(true);

    // inside
    //renderer.setBaseItemLabelPaint(Color.white);
    Font font = new Font("SansSerif", Font.PLAIN, 7);
    Font derive = font.deriveFont(Font.BOLD);
    renderer.setBaseItemLabelFont(derive);

    // margin
    final CategoryAxis domainAxis = categoryPlot.getDomainAxis();
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    //domainAxis.setBottomCategoryLabelPosition(new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER));
    domainAxis.setCategoryLabelPositions(new CategoryLabelPositions(
            new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_CENTER), // TOP
            new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER), // BOTTOM
            new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
                    CategoryLabelWidthType.RANGE, 0.30f), // LEFT
            new CategoryLabelPosition(RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT,
                    CategoryLabelWidthType.RANGE, 0.30f) // RIGHT) 
    ));

    final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER,
            TextAnchor.CENTER, 0.0);
    renderer.setPositiveItemLabelPosition(p);

    if (comboBoxOrientation != null) {
        if (comboBoxOrientation.getSelectedItem() == "Vertikalni") {

            domainAxis.setCategoryLabelPositions(
                    CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
        }
    }

    return chart;

}