2017-06-25 2 views
1

私は、同じ緯度と経線の両方を組み合わせた燭台チャートと線チャートを作成する問題があります。私はOHLCDatasetを使い、 "移動平均"の線図にはTimeSeriesを使います。しかし、線図は、軸に沿った間違った時点で描画されます。私は間違った時刻や日付を設定していないが、プリント時には日付時刻を正確に表示するために、すべてのDateTime要素を印刷しました。チャートでは、しかし、彼らはあまりにも早く6時間を開始します。私は最初にこれがタイムゾーンの問題であると思ったが、私は両方ともESTにタイムゾーンを設定している。 (メソッドを取得JFreeChart - TimeSeriesとOHLCDatasetの組み合わせ - 2番目のデータセットがシフトされました

public OHLCDataset getOHLCDataset(){ 
    OHLCSeries ohlcSeries = new OHLCSeries("Candlesticks"); 
    for(int i=0; i<close.length; i++){ 
     ohlcSeries.add(RegularTimePeriod.createInstance(Minute.class, time[i], TimeZone.getTimeZone("EST")), open[i], max[i], min[i], close[i]); 
    } 
    OHLCSeriesCollection ohlcCollection = new OHLCSeriesCollection(); 
    ohlcCollection.addSeries(ohlcSeries); 
    return ohlcCollection; 
} 

た時系列:ここ

データセットを作成し、XYPlotに

OHLCDataset検索方法(時間[i]が日付オブジェクト)を割り当てコードスニペットであります時間[i]は日付オブジェクトである - 上記と同じ):

public XYDataset getAverageXYDataset(int periods, int frame){ 
    TimeSeries x = new TimeSeries("moving average " + periods + " periods"); 
    if(frame>60){ 
     for(int i=periods-1; i<close.length; i++){ 
      double sum = 0; 
      for(int j=i; j>i-periods; j--){ 
       sum += close[j]; 
      } 
      x.add(RegularTimePeriod.createInstance(Hour.class, time[i], TimeZone.getTimeZone("EST")), sum/periods); 
     } 
    }else{ 
     for(int i=periods-1; i<close.length; i++){ 
      double sum = 0; 
      for(int j=i; j>i-periods; j--){ 
       sum += close[j]; 
      } 
      x.add(RegularTimePeriod.createInstance(Minute.class, time[i], TimeZone.getTimeZone("EST")), sum/periods); 
     } 
    } 
    return new TimeSeriesCollection(x); 
} 

プロットにデータセットを追加するコード:

ここ
OHLCDataset dataset1 = dataset.getOHLCDataset(); 
XYDataset smallAverageDataset = dataset.getAverageXYDataset(20, period); 
XYDataset bigAverageDataset = dataset.getAverageXYDataset(50, period); 

// create the jfreechart - add candlestickdataset first 
String title2 = dataset.getTime()[0] + " - " + dataset.getTime()[dataset.getTime().length-1]; 
JFreeChart chart = createChart(dataset1, title2); 

// get the xyplot and set other datasets 
chart.getXYPlot().setDataset(1, smallAverageDataset); 
chart.getXYPlot().setDataset(2, bigAverageDataset); 

方法createChartです:

private static JFreeChart createChart(final OHLCDataset dataset, String title) { 
    DateAxis xAxis = createXAxis(); 
    NumberAxis yAxis = createYAxis(); 
    MyCandlestickRenderer candlestickRenderer = createCandlestickRenderer(); 
    plot = new XYPlot(dataset, xAxis, yAxis, candlestickRenderer); 
    JFreeChart chart = new JFreeChart(
      title, 
      new Font("SansSerif", Font.BOLD, 24), 
      plot, 
      false 
    ); 
    return chart;   
} 

そして、ここではcreateXAxis方法である:

private static DateAxis createXAxis(){ 
    DateAxis domainAxis = new DateAxis(); 
    domainAxis.setAutoRange(true); 
    domainAxis.setTickLabelsVisible(true); 
    domainAxis.setAutoTickUnitSelection(true); 
    return domainAxis; 
} 

なlinechartsではなく、あなたが見るとそこにオフセットされた理由を私は理解できません私はすべてのデータセットに対して同じタイムゾーンを設定しました。

ご協力いただきありがとうございます。

+0

[example](https://stackoverflow.com/a/38242696/230513)の問題を示す代表的なデータを含む[mcve]を含むように質問を編集してください。 – trashgod

+0

私は時間があるとすぐにそれを行います。ありがとうございました。 – ulanBator

答えて

0

JFreeChartのTimeSeriesクラスでは、x値は特定の時点ではなく期間です。 TimeSeriesCollectionクラスはこのデータをXYDatasetインターフェイス経由で提示し、各データアイテムに特定のx値を選択する必要があります。 setXPositionメソッド(TimeSeriesCollection)は、期間内のどの点が使用されているか(始点、中点、または終点)を決定するフラグを設定します。

関連する問題