English 中文(简体)
Width of the bar in Jfreechart
原标题:

Is there a way to adjust the width of the bars in a Barchart?

I create my chart with the following code.

final JFreeChart chart = ChartFactory.createBarChart("Report", // chart title
                "Date", // domain axis label
                "Number", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
                );  
问题回答

You can also set the maximum width of a bar. This is useful if you have a wide chart but occasionally there are only one or two data points and you don t want a single big fat bar taking up the whole area.

CategoryPlot categoryPlot = chart.getCategoryPlot();
BarRenderer br = (BarRenderer) categoryPlot.getRenderer();
br.setMaximumBarWidth(.35); // set maximum width to 35% of chart

You can t directly specify the width of the bars, but there s a few attributes that can be changed which affect the width. You should take a look at the lowerMargin, upperMargin and categoryMargin attributes defined by the CategoryAxis or the itemMargin attribute in the BarRenderer.

For example:

    BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
    renderer.setItemMargin(.1);

The double specified in setItemMargin(double percent) is the percentage of the overall length of the category axis that will be used for the space between the bars within the same category (default is .2 or 20%). The smaller this value is, the larger the bars will be.

After getting no results using positive margins and zero, I tried using negative numbers for itemMargin to see what would happen. The bars started getting thicker. I don t know if this is recommended, though, since you will get overlap if there is not enough space available.

BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
renderer.setItemMargin(-2);

According to Dave Gilbert (JFreeChart Project Leader)

The bar widths are automatically calculated and will depend on the chart or plot width and the number of items in your dataset.

See the discussion here.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签