2013-04-18 89 views
1

我有一個漂亮迷人的JFreeChart生成折線圖,但有一個例外 - 在我的X軸的最後一個值是越來越截斷,就像這樣:JFreeChart的最後X軸標籤切斷

 

     | 
     | 
     |_________|________|__________|_________|_________| 
    100 Hz 1 kHz 10 kHz 100 kHz 1 MHz 10 MH 

(注意以MHz爲單位的z被截斷)。

我進去看了這裏的解決方案:
JFreeChart tick label cut off

,但因爲我手動指定在LogAxis的範圍內它從設定保證金沒有任何效果的JavaDoc出現。
JavaDoc

這是我使用生成我X軸代碼:

 

     final LogAxis domainAxis = new LogAxis(frequencyAxisLabel); 
     domainAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH)); 
     domainAxis.setRange(100, 10000000); //100Hz to 10MHz 
     domainAxis.setUpperMargin(.05); //Has no effect 
     domainAxis.setNumberFormatOverride(new UnitNumberFormat(UnitValue.HERTZ)); 
     plot.setDomainAxis(domainAxis); 

如果有幫助,我使用下面還寫這一個.svg文件:

 

      // Get a DOMImplementation and create an XML document 
     DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); 
     Document document = domImpl.createDocument(null, "svg", null); 

     // Create an instance of the SVG Generator 
     SVGGraphics2D svgGenerator = new SVGGraphics2D(document); 
     svgGenerator.setSVGCanvasSize(new Dimension(720, 470)); 

     // draw the chart in the SVG generator 
     Rectangle bounds = new Rectangle(10, 10, 700, 450); 
     chart.draw(svgGenerator, bounds); 

     // Write svg file 
     OutputStream outputStream = new FileOutputStream(new File("SomePath)); 
     Writer out = new OutputStreamWriter(outputStream, "UTF-8"); 
     svgGenerator.stream(out, true /* use css */); 
     outputStream.flush(); 
     outputStream.close(); 

鑑於setUpperMargin()方法不會產生效果,我該如何去確保標籤有足夠的空間來完全打印?我打開旋轉刻度標籤,但我還沒有想出如何做到這一點,只有如何旋轉軸標籤。

回答

1

好的,找到了一個有效的解決方案。

我用

chart.setPadding(new RectangleInsets(10, 10, 10, 10)); 

顯然你的里程可能會有所不同,但是這似乎爲我的目的工作。

1

另一種解決方案可以在這裏找到在jfreechart board

總之,添加以下代碼,你產生X軸:

plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(1.0f); 

,或者上面你的代碼;

domainAxis.setMaximumCategoryLabelWidthRatio(1.0f); 

這解決了我使用水平條形圖的問題,其中長域標籤被截尾爲「...」後綴。

0

嘗試這種情況:

//更改邊距在範圍軸上的頂部...

NumberAxis rangeAxis =(NumberAxis)plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

rangeAxis.setUpperMargin(0.15); 

    rangeAxis.setLowerMargin(0.15);