How to show bars in JFreeChart which has 0 for the Y Axis?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to show bars in JFreeChart which has 0 for the Y Axis?



I am having a clustered bar chart generated from JFreeChart. I have observed that the bars will only appear once the Y Axis value is greater than 0. I need to show the bars which has Y Axis value 0 also.



I have searched many times in google, but I couldn't find a solution for this.



Appreciate if you can help me to do this.



enter image description here



My Code:


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(barChartDir);
if (! directory.exists()){
directory.mkdirs();
}

File BarChart = new File( getSavedBarChartImagePath() );
ChartUtilities.saveChartAsPNG( BarChart , barChart , width , height );
}



Thank You.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived