2012-04-27 47 views
5

JFreeChart APIを使用してJavaアプリケーションでチャートを生成しています。そして、私が得た対数軸ラベル/ティックカスタマイズ

LogAxis logAxis = new LogAxis("Price($)"); 
logAxis.setMinorTickMarksVisible(true); 
logAxis.setAutoRange(true); 
xyplot.setRangeAxis(logAxis); 

enter image description here

:私のチャートの1で、私は次のコードで私のy軸対数スケールの軸(図中A)を作るためにLogAxisオブジェクトを使用してみてください(図Aのように)10^nのようなティックを持つログスケールのy軸。私はそれをユーザーの直感的なBのようにしたいと思います。そして、図に示すように、それぞれの間隔は2→4,4→8,8→16のように異なる値を表し、間隔は2^n。マイナーなことは、異なる値を表していても、間隔が等しく表示されていることです。 Oは、次のコードでこれを達成しようとすると、しかし、私は何を得る

LogAxis logAxis = new LogAxis("Price($)"); 
logAxis.setBase(2); 
logAxis.setTickUnit(new NumberTickUnit(2)); 
logAxis.setMinorTickMarksVisible(true); 
logAxis.setAutoRange(true); 
xyplot.setRangeAxis(logAxis); 

どのように私はフィギュアBを達成することができます図C.

のようなものでしょうか?私は何が必要だと思う

+0

関連する例がここに表示されています(http://www.jfree.org/forum/viewtopic.php?f=3&t=119630)。 #p182652)。 – trashgod

答えて

3

あなたがLogAxisを使用しているにもかかわらず、あなたは、整数チック単位を指定することができます。

LogAxis

/** @see http://stackoverflow.com/a/10353270/230513 */ 
private static void createFrame() { 
    XYSeries series = new XYSeries("Series"); 
    for (int i = 0; i <= N; i++) { 
     series.add(i, Math.pow(2, i)); 
    } 
    NumberAxis xAxis = new NumberAxis("X"); 
    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
    LogAxis yAxis = new LogAxis("Y"); 
    yAxis.setBase(2); 
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
    XYPlot plot = new XYPlot(new XYSeriesCollection(series), 
     xAxis, yAxis, new XYLineAndShapeRenderer(true, false)); 
    JFreeChart chart = new JFreeChart(
     "Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false); 
    JFrame frame = new JFrame("LogAxis Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setContentPane(new ChartPanel(chart)); 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      createFrame(); 
     } 
    }); 
} 
+0

[初期スレッド](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)も参照してください。 – trashgod

+0

まさに私が欲しいものです。どうもありがとうございます!。 – Biscuitz

5

logAxis.setNumberFormatOverride(NumberFormat format);

EDITされています。さらにヘルプが必要とされているので...これを試してみてください。

logAxis.setBase(10); 
LogFormat format = new LogFormat(logAxis.getBase(), "", "", true); 
logAxis.setNumberFormatOverride(format); 

ここでプレイするために使用することができ、全体の方法です...。以下の@ amaidmentの例のバリエーションに示すよう

public static void main(String[] args) { 
    XYSeries series = new XYSeries(""); 
    series.add(1, 10); 
    series.add(2, 100); 
    series.add(3, 1000); 
    series.add(4, 10000); 
    series.add(5, 100000); 
    series.add(6, 1000000); 

// NumberAxis yAxis = new NumberAxis(""); 
    LogAxis yAxis = new LogAxis(""); 
    yAxis.setBase(10); 
    LogFormat format = new LogFormat(yAxis.getBase(), "", "", true); 
    yAxis.setNumberFormatOverride(format); 
    XYPlot plot = new XYPlot(
     new XYSeriesCollection(series), 
     new NumberAxis(""), 
     yAxis, 
     new XYLineAndShapeRenderer(true, false)); 
    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false); 

    JFrame frame = new JFrame("LogAxis Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setContentPane(new ChartPanel(chart)); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
+0

ありがとうございますが、動作しません。 – Biscuitz

+1

@Biscuitz - あなたはとても大変な努力をしていないと思う...寛大な気持ちがあるので、詳細なコードを追加した。 – amaidment

+0

+1 [sscce](http://sscce.org/)。 – trashgod

0

IMHO、あなたがしたいグラフの種類を作成するためにChartFactoryを使用し、直接はJFreeChartのコンストラクタを作成しないでください。したがって、以前に投稿されたものよりも優れた解決策です。

public class MyLogChart 
{ 
    public static void main(String[] args) 
    { 
     XYSeries series = new XYSeries("First"); 
     series.add(1.0, 1.0); 
     series.add(2.0, 10.0); 
     series.add(3.0, 100.0); 
     series.add(4.0, 1000.0); 
     series.add(5.0, 10000.0); 
     series.add(6.0, 100000.0); 
     series.add(7.0, 1000000.0); 
     series.add(8.0, 10000000.0); 

     XYSeriesCollection dataset = new XYSeriesCollection(); 
     dataset.addSeries(series); 

     JFreeChart chart = ChartFactory.createXYLineChart(
      "XY Chart", 
      "x-axis", 
      "y-axis", 
      dataset, 
      PlotOrientation.VERTICAL, 
      false, 
      false, 
      false 
      ); 

     LogarithmicAxis yAxis = new LogarithmicAxis("Y"); 

     XYPlot plot = chart.getXYPlot(); 
     plot.setRangeAxis(yAxis); 

     XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer(); 
     renderer.setSeriesShapesVisible(0, true); 

     ChartFrame frame = new ChartFrame("My Chart", chart); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
関連する問題