只有JfreeChart图中的条形图

时间:2012-01-13 12:05:57

标签: java jfreechart

这可能听起来很傻,但我只想在JfreeChart和透明背景中显示条形图。所有示例都显示了如何创建具有透明背景的PNG,但这不是我想要的,我只是想显示它,不需要创建它。

此外,我发现无法在水平和垂直轴上“禁用”文本。我只想要轴和条纹的线条,这很简单。这是代码:

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;

}

那就是说,任何帮助都表示赞赏。

2 个答案:

答案 0 :(得分:1)

您可以使用setTickLabelsVisible(false)setTickMarksVisible(false)隐藏轴上的文字。

答案 1 :(得分:0)

试试这个

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