Groovyでグラフを作成する 4)日付表示を“日-月”から“月/日”に変更する 失敗編
前回で日本語の表示には成功した。
しかし、まだ、日付表示が“1-9”の欧米風の表示になっている。
これを9-1か9/1に変更したい。
日付のデータ形式をorg.jfree.data.time.Day(java.util.Date time) に変更してみる
ソース
import com.thecoderscorner.groovychart.chart.ChartBuilder import groovy.swing.SwingBuilder import java.awt.BorderLayout import java.awt.Dimension import org.jfree.chart.ChartFactory import org.jfree.chart.ChartPanel import org.jfree.data.time.Day import org.jfree.chart.StandardChartTheme ChartBuilder builder = new ChartBuilder(); def chart = builder.timeserieschart(title:'2010年9月の平均気温', timeAxisLabel:'日付', valueAxisLabel:'気温(℃)', legend:true, tooltips:false, urls:false ) { timeSeriesCollection { timeSeries(name:'気温', timePeriodClass:'org.jfree.data.time.Day') { add(period:new Day(new Date("2010/9/1")), value:27.4) add(period:new Day(new Date("2010/9/2")), value:27.5) add(period:new Day(new Date("2010/9/3")), value:29.1) add(period:new Day(new Date("2010/9/4")), value:28.7) add(period:new Day(new Date("2010/9/5")), value:27.9) add(period:new Day(new Date("2010/9/6")), value:29.3) add(period:new Day(new Date("2010/9/7")), value:29.2) add(period:new Day(new Date("2010/9/8")), value:25.3) add(period:new Day(new Date("2010/9/9")), value:24.4) add(period:new Day(new Date("2010/9/10")), value:24.1) add(period:new Day(new Date("2010/9/11")), value:27.7) add(period:new Day(new Date("2010/9/12")), value:27.9) add(period:new Day(new Date("2010/9/13")), value:29.5) add(period:new Day(new Date("2010/9/14")), value:25.1) add(period:new Day(new Date("2010/9/15")), value:22.3) add(period:new Day(new Date("2010/9/16")), value:20.9) add(period:new Day(new Date("2010/9/17")), value:22.7) add(period:new Day(new Date("2010/9/18")), value:24.2) add(period:new Day(new Date("2010/9/19")), value:24.5) add(period:new Day(new Date("2010/9/20")), value:24.8) add(period:new Day(new Date("2010/9/21")), value:25.5) add(period:new Day(new Date("2010/9/22")), value:28.3) add(period:new Day(new Date("2010/9/23")), value:21.2) add(period:new Day(new Date("2010/9/24")), value:18.5) add(period:new Day(new Date("2010/9/25")), value:19.9) add(period:new Day(new Date("2010/9/26")), value:18.9) add(period:new Day(new Date("2010/9/27")), value:18.0) add(period:new Day(new Date("2010/9/28")), value:19.7) add(period:new Day(new Date("2010/9/29")), value:21.7) add(period:new Day(new Date("2010/9/30")), value:19.4) } } } ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()) def chartPanel = new ChartPanel(chart.chart, false); chartPanel.setPreferredSize(new Dimension(0, 40)); chartPanel.setMouseZoomable(true, false); builder.chartAsPNG(new FileOutputStream('.\\chart\\ChartSampleTemperature2.png'), 800, 400); SwingBuilder swing = new SwingBuilder(); def frame = swing.frame( title:'This is a Frame', location:[100,100], size:[800,400], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add( chartPanel, java.awt.BorderLayout.CENTER); frame.setVisible(true) println new Day(new Date("2010/9/1"))
日付のデータ形式をorg.jfree.data.time.Day(java.util.Date time, java.util.TimeZone zone, java.util.Locale locale) に変更してみる
ソース
import com.thecoderscorner.groovychart.chart.ChartBuilder import groovy.swing.SwingBuilder import java.awt.BorderLayout import java.awt.Dimension import org.jfree.chart.ChartFactory import org.jfree.chart.ChartPanel import org.jfree.data.time.Day import org.jfree.chart.StandardChartTheme ChartBuilder builder = new ChartBuilder(); def chart = builder.timeserieschart(title:'2010年9月の平均気温', timeAxisLabel:'日付', valueAxisLabel:'気温(℃)', legend:true, tooltips:false, urls:false ) { timeSeriesCollection { timeSeries(name:'気温', timePeriodClass:'org.jfree.data.time.Day') { add(period:new Day(new Date("2010/9/1"),TimeZone.getDefault(),Locale.JAPANESE), value:27.4) add(period:new Day(new Date("2010/9/2"),TimeZone.getDefault(),Locale.JAPANESE), value:27.5) add(period:new Day(new Date("2010/9/3"),TimeZone.getDefault(),Locale.JAPANESE), value:29.1) add(period:new Day(new Date("2010/9/4"),TimeZone.getDefault(),Locale.JAPANESE), value:28.7) add(period:new Day(new Date("2010/9/5"),TimeZone.getDefault(),Locale.JAPANESE), value:27.9) add(period:new Day(new Date("2010/9/6"),TimeZone.getDefault(),Locale.JAPANESE), value:29.3) add(period:new Day(new Date("2010/9/7"),TimeZone.getDefault(),Locale.JAPANESE), value:29.2) add(period:new Day(new Date("2010/9/8"),TimeZone.getDefault(),Locale.JAPANESE), value:25.3) add(period:new Day(new Date("2010/9/9"),TimeZone.getDefault(),Locale.JAPANESE), value:24.4) add(period:new Day(new Date("2010/9/10"),TimeZone.getDefault(),Locale.JAPANESE), value:24.1) add(period:new Day(new Date("2010/9/11"),TimeZone.getDefault(),Locale.JAPANESE), value:27.7) add(period:new Day(new Date("2010/9/12"),TimeZone.getDefault(),Locale.JAPANESE), value:27.9) add(period:new Day(new Date("2010/9/13"),TimeZone.getDefault(),Locale.JAPANESE), value:29.5) add(period:new Day(new Date("2010/9/14"),TimeZone.getDefault(),Locale.JAPANESE), value:25.1) add(period:new Day(new Date("2010/9/15"),TimeZone.getDefault(),Locale.JAPANESE), value:22.3) add(period:new Day(new Date("2010/9/16"),TimeZone.getDefault(),Locale.JAPANESE), value:20.9) add(period:new Day(new Date("2010/9/17"),TimeZone.getDefault(),Locale.JAPANESE), value:22.7) add(period:new Day(new Date("2010/9/18"),TimeZone.getDefault(),Locale.JAPANESE), value:24.2) add(period:new Day(new Date("2010/9/19"),TimeZone.getDefault(),Locale.JAPANESE), value:24.5) add(period:new Day(new Date("2010/9/20"),TimeZone.getDefault(),Locale.JAPANESE), value:24.8) add(period:new Day(new Date("2010/9/21"),TimeZone.getDefault(),Locale.JAPANESE), value:25.5) add(period:new Day(new Date("2010/9/22"),TimeZone.getDefault(),Locale.JAPANESE), value:28.3) add(period:new Day(new Date("2010/9/23"),TimeZone.getDefault(),Locale.JAPANESE), value:21.2) add(period:new Day(new Date("2010/9/24"),TimeZone.getDefault(),Locale.JAPANESE), value:18.5) add(period:new Day(new Date("2010/9/25"),TimeZone.getDefault(),Locale.JAPANESE), value:19.9) add(period:new Day(new Date("2010/9/26"),TimeZone.getDefault(),Locale.JAPANESE), value:18.9) add(period:new Day(new Date("2010/9/27"),TimeZone.getDefault(),Locale.JAPANESE), value:18.0) add(period:new Day(new Date("2010/9/28"),TimeZone.getDefault(),Locale.JAPANESE), value:19.7) add(period:new Day(new Date("2010/9/29"),TimeZone.getDefault(),Locale.JAPANESE), value:21.7) add(period:new Day(new Date("2010/9/30"),TimeZone.getDefault(),Locale.JAPANESE), value:19.4) } } } ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()) def chartPanel = new ChartPanel(chart.chart, false); chartPanel.setPreferredSize(new Dimension(0, 40)); chartPanel.setMouseZoomable(true, false); builder.chartAsPNG(new FileOutputStream('.\\chart\\ChartSampleTemperature3.png'), 800, 400); SwingBuilder swing = new SwingBuilder(); def frame = swing.frame( title:'This is a Frame', location:[100,100], size:[800,400], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add( chartPanel, java.awt.BorderLayout.CENTER); frame.setVisible(true) println new Day(new Date("2010/9/1"),TimeZone.getDefault(),Locale.JAPANESE)
日付をフォーマットする
日時をフォーマットする(GroovyのDateクラス版)のようにformat()で形式を指定してみる。
add(period:new Day(new Date("2010/9/1").format("MM/dd")), value:27.4)
ソース
import com.thecoderscorner.groovychart.chart.ChartBuilder import groovy.swing.SwingBuilder import java.awt.BorderLayout import java.awt.Dimension import org.jfree.chart.ChartFactory import org.jfree.chart.ChartPanel import org.jfree.data.time.Day import org.jfree.chart.StandardChartTheme ChartBuilder builder = new ChartBuilder(); def chart = builder.timeserieschart(title:'2010年9月の平均気温', timeAxisLabel:'日付', valueAxisLabel:'気温(℃)', legend:true, tooltips:false, urls:false ) { timeSeriesCollection { timeSeries(name:'気温', timePeriodClass:'org.jfree.data.time.Day') { add(period:new Day(new Date("2010/9/1").format("MM/dd")), value:27.4) add(period:new Day(new Date("2010/9/2").format("MM/dd")), value:27.5) add(period:new Day(new Date("2010/9/3").format("MM/dd")), value:29.1) add(period:new Day(new Date("2010/9/4").format("MM/dd")), value:28.7) add(period:new Day(new Date("2010/9/5").format("MM/dd")), value:27.9) add(period:new Day(new Date("2010/9/6").format("MM/dd")), value:29.3) add(period:new Day(new Date("2010/9/7").format("MM/dd")), value:29.2) add(period:new Day(new Date("2010/9/8").format("MM/dd")), value:25.3) add(period:new Day(new Date("2010/9/9").format("MM/dd")), value:24.4) add(period:new Day(new Date("2010/9/10").format("MM/dd")), value:24.1) add(period:new Day(new Date("2010/9/11").format("MM/dd")), value:27.7) add(period:new Day(new Date("2010/9/12").format("MM/dd")), value:27.9) add(period:new Day(new Date("2010/9/13").format("MM/dd")), value:29.5) add(period:new Day(new Date("2010/9/14").format("MM/dd")), value:25.1) add(period:new Day(new Date("2010/9/15").format("MM/dd")), value:22.3) add(period:new Day(new Date("2010/9/16").format("MM/dd")), value:20.9) add(period:new Day(new Date("2010/9/17").format("MM/dd")), value:22.7) add(period:new Day(new Date("2010/9/18").format("MM/dd")), value:24.2) add(period:new Day(new Date("2010/9/19").format("MM/dd")), value:24.5) add(period:new Day(new Date("2010/9/20").format("MM/dd")), value:24.8) add(period:new Day(new Date("2010/9/21").format("MM/dd")), value:25.5) add(period:new Day(new Date("2010/9/22").format("MM/dd")), value:28.3) add(period:new Day(new Date("2010/9/23").format("MM/dd")), value:21.2) add(period:new Day(new Date("2010/9/24").format("MM/dd")), value:18.5) add(period:new Day(new Date("2010/9/25").format("MM/dd")), value:19.9) add(period:new Day(new Date("2010/9/26").format("MM/dd")), value:18.9) add(period:new Day(new Date("2010/9/27").format("MM/dd")), value:18.0) add(period:new Day(new Date("2010/9/28").format("MM/dd")), value:19.7) add(period:new Day(new Date("2010/9/29").format("MM/dd")), value:21.7) add(period:new Day(new Date("2010/9/30").format("MM/dd")), value:19.4) } } } ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()) def chartPanel = new ChartPanel(chart.chart, false); chartPanel.setPreferredSize(new Dimension(0, 40)); chartPanel.setMouseZoomable(true, false); builder.chartAsPNG(new FileOutputStream('.\\chart\\ChartSampleTemperature4.png'), 800, 400); SwingBuilder swing = new SwingBuilder(); def frame = swing.frame( title:'This is a Frame', location:[100,100], size:[800,400], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add( chartPanel, java.awt.BorderLayout.CENTER); frame.setVisible(true) println new Day(new Date("2010/9/1"))
結果
[D:\workspace\groovy_SandBox]groovy ChartSampleTemperature4.groovy Caught: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.jfree.data.time.Day(java.lang.String) at ChartSampleTemperature4$_run_closure1_closure2_closure3.doCall(ChartSampleTemperature4.groovy:23) at ChartSampleTemperature4$_run_closure1_closure2_closure3.doCall(ChartSampleTemperature4.groovy) at ChartSampleTemperature4$_run_closure1_closure2.doCall(ChartSampleTemperature4.groovy:22) at ChartSampleTemperature4$_run_closure1_closure2.doCall(ChartSampleTemperature4.groovy) at ChartSampleTemperature4$_run_closure1.doCall(ChartSampleTemperature4.groovy:21) at ChartSampleTemperature4$_run_closure1.doCall(ChartSampleTemperature4.groovy) at ChartSampleTemperature4.run(ChartSampleTemperature4.groovy:14)
形式指定するとStringになっちまう。。。
だめだこりゃ。
別のアプローチ方法が必要ってことだな。
実行環境
- OS:Windows XP
- Groovy Version: 1.6.5
- JVM: 1.6.0_12
Groovyでグラフを作成する 4)日付表示を“日-月”から“月/日”に変更する
Groovyでグラフを作成する 3)日本語は使えるか?
Groovyでグラフを作成する 2)動作確認編
Groovyでグラフを作成する 1)インストール編
Groovyで文字列を行に分ける
Groovyで文字列を段落に分ける
Groovyの@grabってなんぞや?
GroovyでWindowsのスクリーンセーバーの待ち時間を変更する
Groovyで特定文字(空白類文字)を含まない部分の長さを調べる その3(完結編)
Groovyでdef関数からdef関数が呼び出せないのか?
Groovyの詳細についてはJavadocと以下の書籍を参考にしている。
Groovyイン・アクション
posted with amazlet
Groovyイン・アクションを読むならあった方が便利かな。
薄い本は「ほんたった」で立ててる