2011-11-04 92 views
3

從我所能告訴的DateTickUnitType是一個枚舉,不能在直接替換之外進行擴展或更改,它僅指定DAY,MONTH,YEAR,MINUTE,HOUR, SECOND和MILLISECOND,但儘管存在Week類型的TimePeriod,但不是WEEK。在JFreeChart中爲一週的單位創建一個自定義的DateTickUnitType

造成這個問題的原因是,當我被迫使用DAY刻度單位繪製一個星期內的單個數據點時,與使用DAY刻度單位或每月繪製每日點相比,數據點在MONTH刻度單位上。

根據我的數據的範圍(我計算編程),我創建了TimeSeriesDataItem最合適TIMEPERIOD:圖表內置

再經過
// create the correct TimeSeriesDataItem based on the gap of this data set 
switch (gap) { 
case WEEK: 
    item = new TimeSeriesDataItem(new Week(targetDate.getTime()), dataPoint); 
    break; 
case DAY: 
    item = new TimeSeriesDataItem(new Day(targetDate.getTime()), dataPoint); 
    break; 
case MONTH: 
default: 
    item = new TimeSeriesDataItem(new Month(targetDate.getTime()), dataPoint); 
    break; 
} 

,我嘗試定製,以使每個數據點儘可能寬。如果數據是天,則時間單位是天。如果數據是幾個月,則刻度單位是幾個月。但是如果數據是數週,那麼我不得不在幾個星期內或者幾個月之後離開,因爲沒有一週的時間單位。

JFreeChart barChart = ChartFactory.createXYBarChart(
    model.getTitle(), 
    model.getDomainTitle(), 
    true, 
    model.getRangeTitle(), 
    dataset, 
    model.getOrientation() == SimpleBarChartModel.Orientation.VERTICAL ? PlotOrientation.VERTICAL : PlotOrientation.HORIZONTAL, 
    model.getShowLegend(), 
    false, 
    false); 

// ... 

DateAxis domainAxis = (DateAxis)plot.getDomainAxis(); 
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
if (model.getDomainLabels() != null && model.getDomainLabels().size() > 1) { 
    // create three different sets of TickUnits for the three ranges (day, week, month) 
    // JFreeChart will select the smallest unit that doesn't overlap labels. 
    TickUnits tickUnits = new TickUnits(); 
    tickUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat("MMM"))); 
    tickUnits.add(new DateTickUnit(DateTickUnitType.DAY, 7, dateFormat)); 
    tickUnits.add(new DateTickUnit(DateTickUnitType.DAY, 1, dateFormat)); 
    domainAxis.setStandardTickUnits(tickUnits); 
} 
domainAxis.setAutoRange(true); 
domainAxis.setVerticalTickLabels(true); 
domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); 

前者創建了非常細的條,因爲它繪製了每個刻度單位7列。

有沒有什麼辦法讓我爲WEEK製作一個自定義DateTickUnitType,讓我每週只繪製一個點而不是7個(因爲我只有一個數據點)?

回答

3

對於DynamicTimeSeriesCollection,您可以使用here顯示的方法爲MILLISECOND添加WEEK。另請參閱此後續文件thread,其中提到了所選期間的倍數。

+0

謝謝@trashgod,但我遇到的問題是使域標籤解析爲1周的頻率。 另一種解決方案(您的後續線程)似乎每200毫秒就會出現一次,但x軸標籤在500ms標記上仍保持均勻間隔。 或者你是否在條形圖上建議他的解決方案會產生在數據點之間沒有間隙的條形(不管x軸標籤)? – BrionS

+0

我太慢了。無法再編輯上述評論。我認爲我的問題既是情節的解決方案(鏈接的帖子可能解決)和刻度標籤單位。 – BrionS

+0

我的目標不是很清楚,繼續並刪除評論(s)並重新措辭。帶有示例數據的[sscce](http://sscce.org/)可能會有所幫助。 – trashgod

相關問題