2011-08-15 89 views
7

就像在下面的例子中正在做,我想在圖表上的域記號標記標籤旋轉45度作爲此圖中: http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/如何在JFreeChart中的數軸的域上旋轉刻度標記?

不同的是,我想這樣做在具有數字軸的散點圖上。我無法在NumberAxis類中找到與setCategoryLabelPositions()等效的內容。

+1

我發現這個樣本代碼,做到了這一點:http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=18240&start= 15 – DrunkenPope

回答

8

方法setVerticalTickLabels()可能是一種替代方法。如果沒有,我沒有看到任何選擇,只能覆蓋refreshTicksHorizontal()。另請參閱此example

enter image description here

import java.awt.Color; 
import java.awt.Dimension; 
import java.util.*; 
import org.jfree.chart.*; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 

/** 
* @see https://stackoverflow.com/questions/7208657 
* @see https://stackoverflow.com/questions/7071057 
*/ 
public class ScatterTickLabels extends ApplicationFrame { 

    public ScatterTickLabels(String s) { 
     super(s); 
     final ChartPanel chartPanel = createDemoPanel(); 
     chartPanel.setPreferredSize(new Dimension(640, 480)); 
     this.add(chartPanel); 
    } 

    public static ChartPanel createDemoPanel() { 
     JFreeChart jfreechart = ChartFactory.createScatterPlot(
      "Scatter Plot Demo", "X", "Y", samplexydataset(), 
      PlotOrientation.VERTICAL, true, true, false); 
     XYPlot xyPlot = (XYPlot) jfreechart.getPlot(); 
     xyPlot.setDomainCrosshairVisible(true); 
     xyPlot.setRangeCrosshairVisible(true); 
     XYItemRenderer renderer = xyPlot.getRenderer(); 
     renderer.setSeriesPaint(0, Color.blue); 
     NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis(); 
     domain.setVerticalTickLabels(true); 
     return new ChartPanel(jfreechart); 
    } 

    private static XYDataset samplexydataset() { 
     int cols = 20; 
     int rows = 20; 
     XYSeriesCollection xySeriesCollection = new XYSeriesCollection(); 
     XYSeries series = new XYSeries("Random"); 
     Random rand = new Random(); 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < cols; j++) { 
       double x = rand.nextGaussian(); 
       double y = rand.nextGaussian(); 
       series.add(x, y); 
      } 
     } 
     xySeriesCollection.addSeries(series); 
     return xySeriesCollection; 
    } 

    public static void main(String args[]) { 
     ScatterTickLabels demo = new ScatterTickLabels("Scatter Plot Demo"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 
+0

這非常簡單,足夠滿足我想要的。謝謝。 –

4

你必須看看超級類:Axis.setLabelAngle(rad)

這裏是一個example

編輯:上面沒用,對不起。

我看了org.jfreechart.chart.axis.NumberAxis.refreshTicksHorizo​​ntal的代碼。實際上有一個角度設置爲0.0(所有new NumberTick(...,0.0)構造函數中的最後一個參數)。您可以創建一個NumberAxis的子類,該子類使用不同角度(在您的構造函數中指定)覆蓋方法refreshTicksHorizo​​ntal。

看起來refreshTicks總是在繪製圖形時被調用,所以你不必擔心它沒有被調用。

/** 
* Calculates the positions of the tick labels for the axis, storing the 
* results in the tick label list (ready for drawing). 
* 
* @param g2 the graphics device. 
* @param dataArea the area in which the data should be drawn. 
* @param edge the location of the axis. 
* 
* @return A list of ticks. 
*/ 
protected List refreshTicksHorizontal(Graphics2D g2, 
     Rectangle2D dataArea, RectangleEdge edge) { 

    List result = new java.util.ArrayList(); 

    Font tickLabelFont = getTickLabelFont(); 
    g2.setFont(tickLabelFont); 

    if (isAutoTickUnitSelection()) { 
     selectAutoTickUnit(g2, dataArea, edge); 
    } 

    TickUnit tu = getTickUnit(); 
    double size = tu.getSize(); 
    int count = calculateVisibleTickCount(); 
    double lowestTickValue = calculateLowestVisibleTickValue(); 

    if (count <= ValueAxis.MAXIMUM_TICK_COUNT) { 
     int minorTickSpaces = getMinorTickCount(); 
     if (minorTickSpaces <= 0) { 
      minorTickSpaces = tu.getMinorTickCount(); 
     } 
     for (int minorTick = 1; minorTick < minorTickSpaces; minorTick++) { 
      double minorTickValue = lowestTickValue 
        - size * minorTick/minorTickSpaces; 
      if (getRange().contains(minorTickValue)){ 
       result.add(new NumberTick(TickType.MINOR, minorTickValue, 
         "", TextAnchor.TOP_CENTER, TextAnchor.CENTER, 
         0.0)); 
      } 
     } 
     for (int i = 0; i < count; i++) { 
      double currentTickValue = lowestTickValue + (i * size); 
      String tickLabel; 
      NumberFormat formatter = getNumberFormatOverride(); 
      if (formatter != null) { 
       tickLabel = formatter.format(currentTickValue); 
      } 
      else { 
       tickLabel = getTickUnit().valueToString(currentTickValue); 
      } 
      TextAnchor anchor = null; 
      TextAnchor rotationAnchor = null; 
      double angle = 0.0; 
      if (isVerticalTickLabels()) { 
       anchor = TextAnchor.CENTER_RIGHT; 
       rotationAnchor = TextAnchor.CENTER_RIGHT; 
       if (edge == RectangleEdge.TOP) { 
        angle = Math.PI/2.0; 
       } 
       else { 
        angle = -Math.PI/2.0; 
       } 
      } 
      else { 
       if (edge == RectangleEdge.TOP) { 
        anchor = TextAnchor.BOTTOM_CENTER; 
        rotationAnchor = TextAnchor.BOTTOM_CENTER; 
       } 
       else { 
        anchor = TextAnchor.TOP_CENTER; 
        rotationAnchor = TextAnchor.TOP_CENTER; 
       } 
      } 

      Tick tick = new NumberTick(new Double(currentTickValue), 
        tickLabel, anchor, rotationAnchor, angle); 
      result.add(tick); 
      double nextTickValue = lowestTickValue + ((i + 1)* size); 
      for (int minorTick = 1; minorTick < minorTickSpaces; 
        minorTick++) { 
       double minorTickValue = currentTickValue 
         + (nextTickValue - currentTickValue) 
         * minorTick/minorTickSpaces; 
       if (getRange().contains(minorTickValue)){ 
        result.add(new NumberTick(TickType.MINOR, 
          minorTickValue, "", TextAnchor.TOP_CENTER, 
          TextAnchor.CENTER, 0.0)); 
       } 
      } 
     } 
    } 
    return result; 

} 
+1

這就是我最初的想法,但這不起作用。它改變了軸的標籤,而不是刻度標籤。你引用的代碼也調用上面引用的setCategoryLabelPositions()。 –

+0

@Jay我更新了我的答案。 – toto2

+0

+1如果有一點_ad hoc_,徵用'verticalTickLabels'屬性很簡單。 – trashgod

9

給出的第一個答案爲數字域軸線。如果你有一個類別軸,你想這個代碼:

CategoryAxis domainAxis = plot.getDomainAxis(); 
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);