Java Jfreechart 中条的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1973936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Width of the bar in Jfreechart
提问by Harish
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?
);
回答by nandokakimoto
回答by Hermann Hans
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, upperMarginand categoryMarginattributes defined by the CategoryAxis or the itemMarginattribute in the BarRenderer.
您不能直接指定条形的宽度,但是可以更改一些影响宽度的属性。您应该查看由 CategoryAxis 或BarRenderer中的 itemMargin属性定义的lowerMargin、upperMargin和categoryMargin属性。
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.
中指定的双精度值setItemMargin(double percent)
是类别轴总长度的百分比,它将用于同一类别内的条形之间的空间(默认值为 0.2 或 20%)。该值越小,条形越大。
回答by Jason Messick
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
回答by sjs1984
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.
在使用正边距和零未获得结果后,我尝试使用负数itemMargin
来查看会发生什么。条形开始变粗。不过,我不知道是否建议这样做,因为如果没有足够的可用空间,您会重叠。
BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
renderer.setItemMargin(-2);