我想创建一个条形图,但应该缩短非常高的值。一个例子是这个图像:
(来源:epa.gov)
我希望很清楚我想要什么。
我的问题是:我怎么能用JFreeChart做到这一点。如果JFreeChart无法实现,您可以推荐其他开源Java库来生成这样的输出。
你可以用CombinedDomainCategoryPlot
或CombinedDomainXYPlot
来做。将第一个绘图的范围轴设置为截止值,然后使用第二个绘图执行类似的操作。然后将它们添加到组合图中。
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);
}
}
这些图将共享相同的X轴。您需要使用渲染器来设置颜色和标签。
删除了死的ImageShack链接
我不确定你能在JFreeChart中做到这一点。
解决方案(不太好)是将图表渲染为图像,然后使用RenderedImage将其作为图像进行操作(将其切割等),而不是作为JFreeChart。不幸的是,这可能会有点痛苦,因为你可能想要在y轴上的特定位置切割等。