我正在从 JFreeChart 生成集群条形图。我观察到,只有在Y轴值大于0时才会显示这些条。我需要显示Y轴值也为0的条。
我在google中搜索了很多次,但是找不到解决方案。
感谢您能帮助我做到这一点。
我的代码:
public static void generateBarChart() throws IOException, ParseException {
final DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
dataset.addValue( 0 , "Failed" , "Spec 1" );
dataset.addValue( 3 , "Passed" , "Spec 1" );
dataset.addValue( 0 , "Skipped" , "Spec 1" );
dataset.addValue( 2 , "Failed" , "Spec 2" );
dataset.addValue( 0 , "Passed" , "Spec 2" );
dataset.addValue( 1 , "Skipped" , "Spec 2" );
JFreeChart barChart = ChartFactory.createBarChart(
"Bar Chart",
"Count", "Spec Name",
dataset,PlotOrientation.HORIZONTAL,
true, true, false);
CategoryPlot plot = barChart.getCategoryPlot();
plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
plot.getRenderer().setSeriesPaint(0, Color.RED);
plot.getRenderer().setSeriesPaint(1, Color.GREEN);
plot.getRenderer().setSeriesPaint(2, Color.BLUE);
ChartPanel chartPanel = new ChartPanel(barChart, false);
chartPanel.setBackground( Color.WHITE );
barChart.getPlot().setBackgroundPaint( Color.GRAY );
barChart.setBorderVisible(true);
barChart.setBorderPaint(Color.BLACK);
((AbstractRenderer) plot.getRenderer()).setBaseLegendShape(new Rectangle(20,20));
LegendTitle legend = barChart.getLegend();
Font labelFont = new Font("SansSerif", Font.TYPE1_FONT, 14);
legend.setItemFont(labelFont);
Font categoryAxisFont = new Font("SansSerif", Font.TYPE1_FONT, 14);
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setTickLabelFont(categoryAxisFont);
categoryAxis.setLabelFont(new Font("Dialog", Font.TRUETYPE_FONT, 14));
Font rangeAxisFont = new Font("SansSerif", Font.TYPE1_FONT, 16);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setTickLabelFont(rangeAxisFont);
rangeAxis.setLabelFont(new Font("Dialog", Font.TRUETYPE_FONT, 14));
// Value on the bars
plot.getRenderer().setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
plot.getRenderer().setBaseItemLabelsVisible(true);
plot.getRenderer().setItemLabelFont(new Font("Dialog",Font.BOLD,12));
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.INSIDE12,
TextAnchor.TOP_CENTER);
plot.getRenderer().setBasePositiveItemLabelPosition(position);
int width = 600;
int height = 800;
// Save it, if the pi-chart directory is not there create it
File directory = new File("./barchart");
if (! directory.exists()){
directory.mkdirs();
}
File BarChart = new File( "./barchart/barchart.PNG" );
ChartUtilities.saveChartAsPNG( BarChart , barChart , width , height );
}
谢谢。