2011年1月10日月曜日

groovyとgooglechartwrapperで積上棒グラフを描画する

groovyとgooglechartwrapperで積上棒グラフを描画するには、以下のコードを実行します。

import java.awt.*
import java.net.*
import javax.imageio.*
import de.toolforge.googlechartwrapper.*
import de.toolforge.googlechartwrapper.data.*
import de.toolforge.googlechartwrapper.label.*
import de.toolforge.googlechartwrapper.util.*
import de.toolforge.googlechartwrapper.style.*
import de.toolforge.googlechartwrapper.coder.*
import de.toolforge.googlechartwrapper.color.*

// 積上棒グラフ
bc = new BarChart(
new Dimension(250, 250),
BarChart.BarChartOrientation.Horizontal,
BarChart.BarChartStyle.Stacked)
bc.setEncoder(EncoderFactory.getEncoder(
EncodingType.TextEncoding))

// 値は0-100
data = new BarChartDataSerie.BarChartDataSerieBuilder(
[35, 52, 88, 75, 80]).build()
bc.addBarChartDataSerie(data)
// 棒グラフの色を追加
bc.addChartColor(
new ChartColor(
new Color(0x77, 0x99, 0xdd)
)
)

// 値は0-100
data2 = new BarChartDataSerie.BarChartDataSerieBuilder(
[10, 12, 18, 15, 17]).build()
bc.addBarChartDataSerie(data2)
// 棒グラフの色を追加
bc.addChartColor(
new ChartColor(
new Color(0xf0, 0xc0, 0x40)
)
)

// X軸
xa = new AxisLabelContainer(AxisType.XAxis)
xa.setAxisRange(new AxisRange(0, 5, 1))
bc.addAxisLabelContainer(xa)

// Y軸
ya = new AxisLabelContainer(AxisType.YAxis)
ya.setAxisRange(new AxisRange(0, 100))
bc.addAxisLabelContainer(ya)

bc.setBarWidthAndSpacing(
BarWidthAndSpacing.newAutomaticallyResize())

// URLを取得
println bc.getUrl()
// 画像として保存
bi = MiscUtils.getBufferedImage(bc)
ImageIO.write(bi, "png", new File ("barchart18.png"))



実行画面


※以下のjarを$GROOVY_HOME/libにコピー
googlechartwrapper_1.1.jar

動作環境
groovy 1.7.5, JDK6 Update22, googlechartwrapper 1.1

関連情報
googlechartwrapperのwebサイト
http://code.google.com/p/googlechartwrapper/

0 件のコメント:

コメントを投稿