目前我正在使用JFreeChart绘制双轴条形图。间隔的条形彼此非常接近。因此,图表中未显示间隔标签。如何增加栏之间的空间?
CategoryAxis domainAxis = new CategoryAxis("Value");
domainAxis.setAxisLinePaint(Color.blue);
domainAxis.setLowerMargin(0.05);
domainAxis.setUpperMargin(0.05);
NumberAxis rangeAxis = new NumberAxis("Category 2");
BarRenderer renderer1 = new BarRenderer();
renderer1.setMaximumBarWidth(.50);
renderer1.setSeriesPaint(0, Color.blue);
renderer1.setShadowVisible(false);
CategoryPlot plot = new CategoryPlot(dataset1, domainAxis, rangeAxis, renderer1);
JFreeChart chart = new JFreeChart("", plot);
chart.setBackgroundPaint(Color.WHITE);
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setDataset(1, dataset2);
plot.mapDatasetToRangeAxis(1, 1);
ValueAxis axis2 = new NumberAxis("Category 1");
plot.setRangeAxis(1, axis2);
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
BarRenderer renderer2 = new BarRenderer();
renderer2.setSeriesPaint(0, Color.green);
renderer2.setMaximumBarWidth(.100);
renderer2.setShadowVisible(false);
plot.setRenderer(1, renderer2);
return chart;
完整的代码:
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class BarChartDemo7 extends ApplicationFrame {
static class LabelGenerator extends StandardCategoryItemLabelGenerator {
public String generateLabel(CategoryDataset dataset, int series,
int category) {
return dataset.getRowKey(series).toString();
}
}
public BarChartDemo7(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
private static CategoryDataset createDataset() {
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i=0;i<120;i++){
dataset.addValue(i, series1, Integer.toString(i));
}
for(int i=0;i<120;i++){
dataset.addValue(i, series2, Integer.toString(i));
}
for(int i=0;i<120;i++){
dataset.addValue(i, series3, Integer.toString(i));
}
return dataset;
}
private static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo 7",
"Category",
"Value",
dataset,
PlotOrientation.HORIZONTAL,
false,
true,
false
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangePannable(true);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setItemMargin(0.10);
return chart;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
public static void main(String[] args) {
BarChartDemo7 demo = new BarChartDemo7(
"");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
我想增加间隔之间的间距,使它们不重叠。