2012-01-13 85 views
2

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

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

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; 

} 

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

+1

如果有人跑进类似的东西,我做了什么让背景透明/剧情在这里是非常描述:http://marxsoftware.blogspot.com/2007/11/transparent-情节背景,in.html。此外,我创建了一个.png,然后将其加载到标签中,然后删除png文件。不是一个最佳的解决方案,但我似乎无法找到另一种方式来获得透明的背景,而不写入文件第一:( – AlejandroVK 2012-01-14 18:41:53

回答

1

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

+0

谢谢Baldrick,但我应该如何得到轴?图有getCategoriesForAxis(CategoryAxis轴),它返回一个列表,但它不是很清楚如何使用它... – AlejandroVK 2012-01-13 12:53:39

+0

你已经有轴:它是'rangeAxis'和'domainAxis'在你发布的代码中 – Baldrick 2012-01-13 13:09:47

+0

true!现在我只需要透明度:D – AlejandroVK 2012-01-13 13:18:21

0

试试这个

chart.getCategoryPlot().setBackgroundPaint(new Color(0,0,0,0)); 
相关问题