AlejandroVK
AlejandroVK

Reputation: 7605

Only bars in a JfreeChart graph

This might sound silly, but I'd only want to display bars in a JfreeChart and a transparent background. All examples show how to create a PNG with transparent background but this is not what I want, I just want to display it, no need to create it.

Besides, I find no way to "disable" text in horizontal and vertical axis. I just want the lines for the axis and the bars, simple as that. Here's the code:

private static JFreeChart createActivityBarGraph(CategoryDataset dataset) {

    // create the chart...
    JFreeChart chart = ChartFactory.createBarChart(
        null,         // chart title
        null,               // domain axis label
        null,                  // range axis label
        dataset,                  // data
        PlotOrientation.VERTICAL, // orientation
        false,                     // include legend
        false,                     // tooltips?
        false                     // URLs?
    );

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



    // set the background color for the chart to be transparent
    chart.setBackgroundPaint( new Color(255,255,255,0) );
    chart.setBorderVisible(false);        
    CategoryPlot cPlot = chart.getCategoryPlot();
    cPlot.setBackgroundPaint( new Color(255,255,255,0) );
    cPlot.setBackgroundAlpha(0.0f);
    cPlot.setDomainGridlinePaint(Color.white);
    cPlot.setDomainGridlinesVisible(false);
    cPlot.setRangeGridlinePaint(Color.white);
    cPlot.setOutlineVisible(false);


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

    // disable bar outlines...
    BarRenderer renderer = (BarRenderer) cPlot.getRenderer();
    renderer.setDrawBarOutline(true);

    // 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)
    );

    renderer.setSeriesPaint(0, gp0);

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

    return chart;

}

That said, any help is appreciated.

Upvotes: 2

Views: 2039

Answers (2)

Nirmit Dalal
Nirmit Dalal

Reputation: 325

try this

chart.getCategoryPlot().setBackgroundPaint(new Color(0,0,0,0));

Upvotes: 0

Baldrick
Baldrick

Reputation: 24350

You can use setTickLabelsVisible(false) and setTickMarksVisible(false) to hide the text on the axes.

Upvotes: 1

Related Questions