2011-08-04 11 views
5

Sto usando JFreeChart per creare grafici candlestick nella mia app Java. Tuttavia, i miei grafici finiscono per guardare in questo modo:Auto-Scale Y-axis in JfreeChart

capturebhx http://imageshack.us/photo/my-images/69/capturebhx.png/

mi piacerebbe avere l'asse Y in scala automaticamente in modo che il grafico sembra più simile a questo:

capture2wl http://imageshack.us/photo/my-images/717/capture2wl.png/

Penso che lo org.jfree.chart.axis.NumberAxis.configure() farà questo, ma non ne sono sicuro. Non riesco a trovare un modo per legare il mio oggetto JFreeChart o l'oggetto ChartPanel a questo oggetto NumberAxis. Per favore aiutami, sono perso e ho cercato a lungo di provare a legare insieme questi oggetti. Oppure, se riesci a trovare un altro modo, sarebbe fantastico!

Alcuni codice:

... 
private DefaultHighLowDataset dataset; 
private JFreeChart chart; 
private ChartPanel chart_panel; 

... 

// creates dataset, then chart from dataset, then chart_panel from chart 
dataset = new DefaultHighLowDataset("", date, high, low, open, close, volume); 
chart = ChartFactory.createCandlestickChart("Blank", "Time", "Price", dataset, false); 
chart_panel = new ChartPanel(chart); // what you see in the images 
... 
+1

Alcuni frammenti di codice possono essere d'aiuto. –

risposta

6

Assicurarsi di setAutoRangeIncludesZero(false) o "il campo dell'asse ... è costretto a includere zero".

Addendum:

io ancora non so come collegare un oggetto NumberAxis a un oggetto o di ChartPanelJFreeChart oggetto.

Si consiglia di esaminare gli esempi in org.jfree.chart.demo e here. Se questo è terra incognita, io raccomanderei The JFreeChart Developer Guide.

Diniego: Non affiliato a Object Refinery Limited; solo un cliente soddisfatto e un contributore molto minore.

+0

Potrebbe farlo anche, ma non so ancora come collegare un oggetto NumberAxis a un oggetto ChartPanel o oggetto JFreeChart. – supercoder

+0

Ho elaborato sopra. Se hai ancora problemi, pubblica [sscce] (http://sscce.org/) utilizzato per creare la tua [immagine] (http://imageshack.us/photo/my-images/69/capturebhx.png /). – trashgod

+0

anche io ho lo stesso problema ma sto usando una libreria afreechart per Android. e questo codice non funziona per me. – arjunkn

4

ho fatto in questo modo:

 final JFreeChart chart = ChartFactory.createCandlestickChart(
      "Candlestick Demo", "Time", "Price", dataset, false); 

    double lowestLow = getLowestLow(dataset); 
    double highestHigh = getHighestHigh(dataset); 

    chart.getXYPlot().getRangeAxis().setRange(lowestLow*0.95, highestHigh*1.05); 

ho calcolare l'alto basso basso e più basso utilizzo di queste funzioni

private double getLowestLow(DefaultHighLowDataset dataset){ 
    double lowest; 
    lowest = dataset.getLowValue(0,0); 
    for(int i=1;i<dataset.getItemCount(0);i++){ 
     if(dataset.getLowValue(0,i) < lowest){ 
      lowest = dataset.getLowValue(0,i); 
     } 
    } 

    return lowest; 
} 


private double getHighestHigh(DefaultHighLowDataset dataset){ 
    double highest; 
    highest = dataset.getHighValue(0,0); 
    for(int i=1;i<dataset.getItemCount(0);i++){ 
     if(dataset.getLowValue(0,i) > highest){ 
      highest = dataset.getHighValue(0,i); 
     } 
    } 

    return highest; 
} 

Questo mi sembra dare una bella grafico a candela che fa bene uso della gamma dell'asse Y. Spero che questo ti aiuti.