JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 請問一下 jFreechart DomainAxis及RangeAxis的Label如何置於座標軸上
dreamltf

wicket



發文: 649
積分: 5
於 2009-05-27 19:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
想請教大家一下,下面這張圖 jfreechart 有 api 可以用嗎,還是要自己覆寫@@?
主要是想讓 X軸 及 Y軸 上的 Label title 能夠擺在座標軸的邊邊上,不知道有沒有辦法

這是我現在不能達成結果的 code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private JFreeChart createChart(XYDataset dataset) {
    JFreeChart chart = ChartFactory.createXYLineChart(null, "年度", "經費(億元)", dataset,
        PlotOrientation.VERTICAL, false, false, false);
    chart.setBackgroundPaint(Color.white);
    chart.setTextAntiAlias(false);
 
    XYPlot plot = chart.getXYPlot();
 
    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setAutoTickUnitSelection(false);
    domainAxis.getLabelInsets().calculateTopInset(0);
 
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.getLabelInsets().calculateTopInset(0);
 
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    plot.setRenderer(renderer);
 
    return chart;
  }



dreamltf edited on 2009-05-27 23:21
reply to postreply to post
作者 Re:請問一下 jFreechart DomainAxis及RangeAxis的Label如何置於座標軸上 [Re:dreamltf]
dreamltf

wicket



發文: 649
積分: 5
於 2009-05-29 16:18 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有點硬湊出來的code...
因為左側 Label 及下方 Label 的區域還是在,只是把 drawLabel 的座標換了。
如果把 Label 的 LabelAngle 改變角度,則會多出大片空白區域。

還在研究這是哪個 method 在負責算的...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  private JFreeChart createChart(XYDataset dataset) {
    JFreeChart chart = ChartFactory.createXYLineChart(null, "年度", "經費(億元)",
        dataset, PlotOrientation.VERTICAL, false, false, false);
    chart.setBackgroundPaint(Color.white);
    chart.setTextAntiAlias(false);
 
    XYPlot plot = chart.getXYPlot();
    plot.setAxisOffset(new RectangleInsets(30, 10, 10, 30));
 
    ValueAxis domainAxis = new LabelAxis(plot.getDomainAxis().getLabel());
    plot.setDomainAxis(domainAxis);
    domainAxis.setAutoTickUnitSelection(false);
 
    ValueAxis rangeAxis = new LabelAxis(plot.getRangeAxis().getLabel());
    plot.setRangeAxis(rangeAxis);
 
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    plot.setRenderer(renderer);
 
    return chart;
  }

LabelAxis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
 
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
 
/**
 * @author dreamltf
 *
 */
public class LabelAxis extends NumberAxis {
 
  private static final long serialVersionUID = 1;
 
  public LabelAxis() {
    super();
  }
 
  public LabelAxis(String label) {
    super(label);
  }
 
  @Override
  protected AxisState drawLabel(String label, Graphics2D g2,
      Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge,
      AxisState state) {
 
    // it is unlikely that 'state' will be null, but check anyway...
    if (state == null) {
      throw new IllegalArgumentException("Null 'state' argument.");
    }
 
    if ((label == null) || (label.equals(""))) {
      return state;
    }
 
    Font font = getLabelFont();
    RectangleInsets insets = getLabelInsets();
    g2.setFont(font);
    g2.setPaint(getLabelPaint());
    FontMetrics fm = g2.getFontMetrics();
    Rectangle2D labelBounds = TextUtilities.getTextBounds(label, g2, fm);
 
    if (edge == RectangleEdge.LEFT) {
      AffineTransform t = AffineTransform.getRotateInstance(
          getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(),
          labelBounds.getCenterY());
      Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
      labelBounds = rotatedLabelBounds.getBounds2D();
      double labelx = dataArea.getMinX();
      double labely = dataArea.getMinY() - labelBounds.getWidth() / 2.0;
      TextUtilities.drawRotatedString(label, g2, (float) labelx,
          (float) labely, TextAnchor.CENTER, getLabelAngle(),
          TextAnchor.CENTER);
      state.cursorLeft(insets.getLeft() + labelBounds.getWidth()
          + insets.getRight());
    } else if (edge == RectangleEdge.BOTTOM) {
      AffineTransform t = AffineTransform.getRotateInstance(
          getLabelAngle(), labelBounds.getCenterX(), labelBounds
              .getCenterY());
      Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
      labelBounds = rotatedLabelBounds.getBounds2D();
      double labelx = dataArea.getMaxX() + labelBounds.getWidth() / 2.0;
      double labely = dataArea.getMaxY() + labelBounds.getHeight();
      TextUtilities.drawRotatedString(label, g2, (float) labelx,
          (float) labely, TextAnchor.CENTER, getLabelAngle(),
          TextAnchor.CENTER);
      state.cursorDown(insets.getTop() + labelBounds.getHeight()
          + insets.getBottom());
    }
    return state;
  }
 
}

參考資料: http://www.javaworld.com.tw/jute/post/view?bid=11&id=159684&sty=3
thanks


dreamltf edited on 2009-11-15 12:09
reply to postreply to post
» JWorld@TW »  Java Tools

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8