How to create a bar chart?

How can I create a bar-chart with JFreeChart, that shortens too long bars with a visible hint?

  • I want to create a bar-chart, but extraordinary high values should be shortened. An example is this image: I hope it is clear what I want. My question is: How can I do this with http://www.jfree.org/jfreechart/. If it isn't possible with JFreeChart you can possibly recommend alternative open-source Java-libraries to produce such an output.

  • Answer:

    You could do it with a CombinedDomainCategoryPlot or CombinedDomainXYPlot. Set the range axis of the first plot to your cut off value and then do something similar with the second plot. Then add them to a combined plot. import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.CombinedDomainCategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; public class PlayChart { public static void main(String[] args) { DefaultCategoryDataset ds = new DefaultCategoryDataset(); ds.addValue(100, "A", "A"); ds.addValue(200, "A", "B"); ds.addValue(400, "A", "C"); ds.addValue(500, "A", "D"); ds.addValue(2000, "A", "E"); JFreeChart bc = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts", ds, PlotOrientation.VERTICAL, true, false, false); JFreeChart bcTop = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts", ds, PlotOrientation.VERTICAL, true, false, false); CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot(); CategoryPlot topPlot = bcTop.getCategoryPlot(); NumberAxis topAxis = (NumberAxis) topPlot.getRangeAxis(); topAxis.setLowerBound(1500); topAxis.setUpperBound(2000); combinedPlot.add(topPlot, 1); CategoryPlot mainPlot = bc.getCategoryPlot(); combinedPlot.add(mainPlot, 5); NumberAxis mainAxis = (NumberAxis) mainPlot.getRangeAxis();; mainAxis.setLowerBound(0); mainAxis.setUpperBound(600); JFreeChart combinedChart = new JFreeChart("Test", combinedPlot); ChartFrame cf = new ChartFrame("Test", combinedChart); cf.setSize(800, 600); cf.setVisible(true); } } The plots will share the same X-axis. You'll need to play with the renderers to set colours and labels.

Mnementh at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I'm not sure you can do that in JFreeChart. A solution (which is not nice) is to render the chart to an image and then manipulate it (chop it up etc.) as an image, using http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/RenderedImage.html, and not as a JFreeChart. Unfortunately that's going to be a bit of a pain since you'd probably want to chop at a particular place on the y-axis etc.

Brian Agnew

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.