2009-09-07 34 views

回答

11

你可以用CombinedDomainCategoryPlotCombinedDomainXYPlot来做到这一点。将第一个绘图的范围轴设置为您的截止值,然后对第二个绘图执行类似操作。然后将它们添加到组合情节。

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的链接

+0

大,看起来完全一样希望,感谢快速帮助。 – Mnementh 2009-09-07 12:58:01

+0

我知道这是很久以前,但这是一个非常彻底的答案。 – 2011-02-07 09:28:49

0

我不确定你可以在JFreeChart中做到这一点。

解决方案(并不好)是将图表渲染为图像,然后使用RenderedImage而不是JFreeChart作为图像对图像进行处理(将其砍掉等)。不幸的是,这可能会有点痛,因为你可能想在y轴上的某个特定位置进行切割等。