Example usage for org.jfree.chart.axis CategoryAxis setCategoryMargin

List of usage examples for org.jfree.chart.axis CategoryAxis setCategoryMargin

Introduction

In this page you can find the example usage for org.jfree.chart.axis CategoryAxis setCategoryMargin.

Prototype

public void setCategoryMargin(double margin) 

Source Link

Document

Sets the category margin and sends an AxisChangeEvent to all registered listeners.

Usage

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

/**
 * Creates a new demo./*from  w  w  w .j av a2s .  c  o m*/
 *
 * @param title  the frame title.
 */
public StatisticalBarChartDemo(final String title) {

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

    final CategoryAxis xAxis = new CategoryAxis("Type");
    xAxis.setLowerMargin(0.01d); // percentage of space before first bar
    xAxis.setUpperMargin(0.01d); // percentage of space after last bar
    xAxis.setCategoryMargin(0.05d); // percentage of space between categories
    final ValueAxis yAxis = new NumberAxis("Value");

    // define the plot
    final CategoryItemRenderer renderer = new StatisticalBarRenderer();
    final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

    final JFreeChart chart = new JFreeChart("Statistical Bar Chart Demo", new Font("Helvetica", Font.BOLD, 14),
            plot, true);
    //chart.setBackgroundPaint(Color.white);
    // 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.pentaho.plugin.jfreereport.reportcharts.AreaChartExpression.java

/**
 * Creates a stacked area chart with default settings.  The chart object returned by this method uses a {@link
 * CategoryPlot} instance as the plot, with a {@link org.jfree.chart.axis.CategoryAxis} for the domain axis, a {@link
 * org.jfree.chart.axis.NumberAxis} as the range axis, and a {@link org.jfree.chart.renderer.category
 * .StackedAreaRenderer}/* w  w w . j  a  va2 s.  c om*/
 * as the renderer.
 *
 * @param title             the chart title (<code>null</code> permitted).
 * @param categoryAxisLabel the label for the category axis (<code>null</code> permitted).
 * @param valueAxisLabel    the label for the value axis (<code>null</code> permitted).
 * @param dataset           the dataset for the chart (<code>null</code> permitted).
 * @param orientation       the plot orientation (horizontal or vertical) (<code>null</code> not permitted).
 * @param legend            a flag specifying whether or not a legend is required.
 * @param tooltips          configure chart to generate tool tips?
 * @param urls              configure chart to generate URLs?
 * @return A stacked area chart.
 */
private JFreeChart createStackedAreaChart(final String title, final String categoryAxisLabel,
        final String valueAxisLabel, final CategoryDataset dataset, final PlotOrientation orientation,
        final boolean legend, final boolean tooltips, final boolean urls) {

    if (orientation == null) {
        throw new IllegalArgumentException("Null 'orientation' argument.");
    }
    final CategoryAxis categoryAxis = new FormattedCategoryAxis(categoryAxisLabel,
            getCategoricalAxisMessageFormat(), getRuntime().getResourceBundleFactory().getLocale());
    categoryAxis.setCategoryMargin(0.0);
    final ValueAxis valueAxis = new NumberAxis(valueAxisLabel);

    final StackedAreaRenderer renderer = new StackedAreaRenderer();
    if (tooltips) {
        renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    }
    if (urls) {
        renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
    }

    final CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
    plot.setOrientation(orientation);
    return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
}

From source file:org.aksw.iguana.reborn.charts.datasets.StatisticalBarChartDemo.java

/**
 * Creates a new demo./*ww w . java2s.  c  o  m*/
 *
 * @param title
 *            the frame title.
 * @throws DocumentException
 * @throws FileNotFoundException
 */
public StatisticalBarChartDemo(final String title) throws FileNotFoundException, DocumentException {

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

    final CategoryAxis xAxis = new CategoryAxis("Type");
    xAxis.setLowerMargin(0.01d); // percentage of space before first bar
    xAxis.setUpperMargin(0.01d); // percentage of space after last bar
    xAxis.setCategoryMargin(0.05d); // percentage of space between
    // categories
    final ValueAxis yAxis = new NumberAxis("Value");

    // define the plot
    final CategoryItemRenderer renderer = new StatisticalBarRenderer();
    final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

    final JFreeChart chart = new JFreeChart("Statistical Bar Chart Demo", new Font("Helvetica", Font.BOLD, 14),
            plot, true);
    ChartUtilities2.saveChartAsPDF(new File("/home/raven/tmp/foo.pdf"), chart, 1000, 300);

    // chart.setBackgroundPaint(Color.white);
    // 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:com.intel.stl.ui.common.view.ComponentFactory.java

@SuppressWarnings("unchecked")
public static JFreeChart createTopNBarChart2(String yAxisLabel, CategoryDataset dataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart(null, null, yAxisLabel, dataset,
            PlotOrientation.HORIZONTAL, false, true, false);
    CategoryPlot categoryplot = jfreechart.getCategoryPlot();
    categoryplot.setBackgroundPaint(null);
    categoryplot.setOutlinePaint(null);//ww  w . j  a  v a2s.  c  o m
    categoryplot.setDomainGridlinesVisible(true);
    categoryplot.setDomainGridlinePosition(CategoryAnchor.END);
    categoryplot.setDomainGridlineStroke(new BasicStroke(0.5F));
    categoryplot.setDomainGridlinePaint(UIConstants.INTEL_BORDER_GRAY);
    categoryplot.setRangeGridlinesVisible(false);
    categoryplot.clearRangeMarkers();
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    categoryaxis.setVisible(false);
    categoryaxis.setCategoryMargin(0.75D);

    NumberAxis axis = (NumberAxis) categoryplot.getRangeAxis();
    axis.setRangeType(RangeType.POSITIVE);
    axis.setVisible(false);

    BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
    barrenderer.setShadowVisible(false);
    barrenderer.setSeriesPaint(0, UIConstants.INTEL_BLUE);
    barrenderer.setDrawBarOutline(false);
    barrenderer.setBaseItemLabelsVisible(true);
    barrenderer.setBaseItemLabelFont(UIConstants.H5_FONT);
    barrenderer.setBarPainter(new StandardBarPainter());

    List<String> names = dataset.getColumnKeys();
    for (String name : names) {
        CategoryTextAnnotation categorytextannotation = new CategoryTextAnnotation(name, name, 0.0D);
        categorytextannotation.setFont(UIConstants.H6_FONT);
        categorytextannotation.setTextAnchor(TextAnchor.BOTTOM_LEFT);
        categorytextannotation.setCategoryAnchor(CategoryAnchor.MIDDLE);
        categoryplot.addAnnotation(categorytextannotation);
    }
    return jfreechart;
}

From source file:org.evosuite.jenkins.plot.Plot.java

@Override
protected JFreeChart createGraph() {
    final JFreeChart chart = ChartFactory.createLineChart(null, "Build Number #", this.yLabel, this.dataset,
            PlotOrientation.VERTICAL, true, true, true);
    chart.setBackgroundPaint(Color.WHITE);

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

    CategoryAxis domainAxis = new CategoryAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    domainAxis.setLowerMargin(0.0);//from www .  j  a  v  a  2 s . co m
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);

    plot.setDomainAxis(domainAxis);
    plot.setBackgroundPaint(Color.WHITE);

    ValueAxis yAxis = plot.getRangeAxis();
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    //yAxis.setRange(0.0, 100.0);

    URLAndTooltipRenderer urlRenderer = new URLAndTooltipRenderer(this.project.getProject());
    ColorPalette.apply(urlRenderer);
    plot.setRenderer(urlRenderer);

    return chart;
}

From source file:com.choicemaker.cm.modelmaker.gui.panels.StatisticsHistogramPanel.java

private void buildPanel() {
    final PlotOrientation orientation = PlotOrientation.VERTICAL;
    data = new HistoCategoryDataset(SERIES, getNumBins());
    histogram = ChartFactory.createBarChart(
            ChoiceMakerCoreMessages.m.formatMessage("train.gui.modelmaker.panel.histogram.cm.accuracy"),
            ChoiceMakerCoreMessages.m.formatMessage("train.gui.modelmaker.panel.histogram.cm.matchprob"),
            ChoiceMakerCoreMessages.m.formatMessage("train.gui.modelmaker.panel.histogram.cm.numpairs"), data,
            orientation, true, true, true);
    histogram.setBackgroundPaint(getBackground());
    CategoryPlot plot = (CategoryPlot) histogram.getPlot();
    plot.setForegroundAlpha(0.9f);//from   w w w.j a  va  2 s . c  o m
    //      HorizontalCategoryAxis axis = (HorizontalCategoryAxis) plot.getDomainAxis();
    CategoryAxis axis = plot.getDomainAxis();
    axis.setLowerMargin(0.02);
    axis.setUpperMargin(0.02);
    axis.setCategoryMargin(0.2);
    //      axis.setVerticalCategoryLabels(true);
    CategoryItemRenderer renderer = plot.getRenderer();
    whSeriesPaint = new Paint[3];
    for (int i = 0; i < whSeriesPaint.length; ++i) {
        //         whSeriesPaint[i] = renderer.getSeriesPaint(0, i);
        whSeriesPaint[i] = renderer.getSeriesPaint(i);
    }
    wohSeriesPaint = new Paint[2];
    wohSeriesPaint[0] = whSeriesPaint[0];
    wohSeriesPaint[1] = whSeriesPaint[2];
    //   plot.setRangeAxis(new VerticalLogarithmicAxis());
    histoPanel = new HistoChartPanel(histogram, false, false, false, true, true, parent.getModelMaker());
    //   histoPanel.setEnabled(false);

    binWidthLabel = new JLabel(
            ChoiceMakerCoreMessages.m.formatMessage("train.gui.modelmaker.panel.histogram.binwidth"));
    binWidthField = new JTextField(Float.toString(binWidth), 4);
    binWidthField.setMinimumSize(new Dimension(50, 20));
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.HierarchicalBarChart.java

private void buildFinalChart(String title, String subTitle, String xAxisLabel, String yAxisLabel, int height,
        int width, String filename, Chart.Scale scaleMode, Chart.LegendFormat legendFormatMode, int alpha) {
    @SuppressWarnings("unchecked")
    Vector<Counter>[] vec = new Vector[this.timers.length];
    boolean exist;

    // create the dataset...
    for (int i = 0; i < this.timers.length; i++) {
        vec[i] = new Vector<Counter>();
        @SuppressWarnings("unchecked")
        Iterator<Element> it = this.timers[i].getDescendants();
        while (it.hasNext()) {
            try {
                Element elt = (Element) it.next();
                String name = elt.getAttributeValue("name");
                double value = Double.valueOf(elt.getAttributeValue("avg"));
                exist = false;//  ww w  .  j a v a  2  s .  c  o  m
                for (int j = 0; j < vec[i].size(); j++) {
                    if (((Counter) vec[i].get(j)).getName().equals(name)) {
                        ((Counter) vec[i].get(j)).addValue(value);
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    vec[i].add(new Counter(name, value));
                }
            } catch (ClassCastException e) {
                continue;
            }
        }
    }

    CategoryDataset dataset = null;
    try {
        dataset = DatasetUtilities.createCategoryDataset(toSeries(vec[0]), this.categories, toDataset(vec));
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new IllegalArgumentException(
                "Benchmark names must have different names. Be sure that your filter contains correct timers names");
    }

    // create the chart...
    final CategoryAxis categoryAxis = new CategoryAxis(xAxisLabel);
    final ValueAxis valueAxis = new NumberAxis(yAxisLabel);

    final CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, new HierarchicalBarRenderer());

    plot.setOrientation(PlotOrientation.VERTICAL);
    final JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    chart.addSubtitle(new TextTitle(subTitle));

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

    final HierarchicalBarRenderer renderer = (HierarchicalBarRenderer) plot.getRenderer();

    renderer.setItemMargin(0.01);
    renderer.setDatasetTree(this.timers);
    renderer.setSeries(toSeries(vec[0]));
    renderer.setAlpha(alpha);

    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryMargin(HierarchicalBarChart.CATEGORY_MARGIN);
    domainAxis.setUpperMargin(0.05);
    domainAxis.setLowerMargin(0.05);

    try {
        if ((filename == null) || "".equals(filename)) {
            throw new RuntimeException(
                    "The output filename for the HierarchicalBarChart cannot be null or empty !");
        }

        ChartUtilities.saveChartAsPNG(XMLHelper.createFileWithDirs(filename + ".png"), chart, width, height);

        Utilities.saveChartAsSVG(chart, new Rectangle(width, height),
                XMLHelper.createFileWithDirs(filename + ".svg"));
    } catch (java.io.IOException e) {
        System.err.println("Error writing chart image to file");
        e.printStackTrace();
    }
}

From source file:com.greenpepper.confluence.macros.historic.AggregationExecutionChartBuilder.java

private void customizeChart(JFreeChart chart) throws IOException {
    chart.setBackgroundPaint(Color.white);
    chart.setBorderVisible(settings.isBorder());

    TextTitle chartTitle = chart.getTitle();
    customizeTitle(chartTitle, DEFAULT_TITLE_FONT);

    addSubTitle(chart, settings.getSubTitle(), DEFAULT_SUBTITLE_FONT);
    addSubTitle(chart, settings.getSubTitle2(), DEFAULT_SUBTITLE2_FONT);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setNoDataMessage(gpUtil.getText("greenpepper.historic.nodata"));

    StackedBarRenderer renderer = new StackedBarRenderer(true);
    plot.setRenderer(renderer);//from ww w  .  ja v a  2  s . c o  m

    int index = 0;
    renderer.setSeriesPaint(index++, GREEN_COLOR);
    if (settings.isShowIgnored())
        renderer.setSeriesPaint(index++, Color.yellow);
    renderer.setSeriesPaint(index, Color.red);

    renderer.setToolTipGenerator(new DefaultTooltipGenerator());
    renderer.setItemURLGenerator(new CategoryURLGenerator() {

        public String generateURL(CategoryDataset data, int series, int category) {
            Comparable valueKey = data.getColumnKey(category);
            ChartLongValue value = (ChartLongValue) valueKey;
            return "javascript:" + settings.getExecutionUID() + "_showHistoricChart('" + value.getId() + "');";
        }
    });

    CategoryAxis domainAxis = plot.getDomainAxis();
    customizeAxis(domainAxis);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    domainAxis.setCategoryMargin(0.01);

    ValueAxis rangeAxis = plot.getRangeAxis();
    customizeAxis(rangeAxis);
    rangeAxis.setLowerBound(0);
    rangeAxis.setUpperBound(1.0);

    if (rangeAxis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) rangeAxis;
        numberAxis.setTickUnit(new NumberTickUnit(.10));
        numberAxis.setNumberFormatOverride(PERCENT_FORMATTER);
    }

    plot.setForegroundAlpha(0.8f);
}

From source file:com.thalesgroup.hudson.plugins.klocwork.graph.KloTrendGraph.java

@Override
protected JFreeChart createGraph() {
    final JFreeChart chart = ChartFactory.createLineChart(null, // chart title
            null, // unused
            yLabel, // range axis label
            categoryDataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );/*from  w w w.ja v  a  2 s  . c o  m*/

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

    final LegendTitle legend = chart.getLegend();
    legend.setPosition(RectangleEdge.RIGHT);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = chart.getCategoryPlot();

    // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
    plot.setBackgroundPaint(Color.WHITE);
    plot.setOutlinePaint(null);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.black);

    CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
    plot.setDomainAxis(domainAxis);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerBound(0);
    rangeAxis.setAutoRange(true);

    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseStroke(new BasicStroke(2.0f));
    ColorPalette.apply(renderer);

    // crop extra space around the graph
    plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0));

    return chart;
}

From source file:cheuk.licenseheaderchecker.resource.DataGraph.java

/**
* Creates a trend graph// w ww.ja  v  a  2s  . c  o  m
*
* @return the JFreeChart graph object
*/
protected JFreeChart createGraph() {

    final JFreeChart chart = ChartFactory.createLineChart(null, // chart title
            null, // unused
            yLabel, // range axis label
            categoryDataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips
            false // urls
    );

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

    //final LegendTitle legend = chart.getLegend();
    //legend.setPosition(RectangleEdge.RIGHT);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = chart.getCategoryPlot();

    // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
    plot.setBackgroundPaint(Color.WHITE);
    plot.setOutlinePaint(null);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.black);

    CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
    plot.setDomainAxis(domainAxis);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerBound(0);
    //        rangeAxis.setAutoRange(true);

    final StackedAreaRenderer renderer = new StackedAreaRenderer2();
    plot.setRenderer(renderer);
    //renderer.setBaseStroke(new BasicStroke(2.0f));
    //ColorPalette.apply(renderer);
    plot.setRenderer(renderer);
    renderer.setSeriesPaint(2, RED);
    renderer.setSeriesPaint(1, GRAY);
    renderer.setSeriesPaint(0, YELLOW);

    // crop extra space around the graph
    plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0));

    return chart;
}