我想創建一個條形圖,但應該縮短非常高的值。一個例子是這樣的形象:如何使用JFreeChart創建條形圖,縮短帶有可見提示的太長的條形圖?
shortened graph http://www.epa.gov/recovery/images/bar-chart.gif
我希望這是清楚我想要什麼。
我的問題是:我如何與JFreeChart做到這一點。如果使用JFreeChart無法實現,則可以推薦其他開源Java庫來生成這樣的輸出。
我想創建一個條形圖,但應該縮短非常高的值。一個例子是這樣的形象:如何使用JFreeChart創建條形圖,縮短帶有可見提示的太長的條形圖?
shortened graph http://www.epa.gov/recovery/images/bar-chart.gif
我希望這是清楚我想要什麼。
我的問題是:我如何與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軸上的某個特定位置進行切割等。
大,看起來完全一樣希望,感謝快速幫助。 – Mnementh 2009-09-07 12:58:01
我知道這是很久以前,但這是一個非常徹底的答案。 – 2011-02-07 09:28:49