2010年11月26日金曜日

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.Grouped)
bc.setEncoder(EncoderFactory.getEncoder(
EncodingType.TextEncoding))

// 棒グラフの色を追加
bc.addChartColor(
new ChartColor(
new Color(0x77, 0x99, 0xdd)
)
)

// 値は0-100
data = new BarChartDataSerie.BarChartDataSerieBuilder(
[30, 50, 80, 75, 90]).build()
bc.addBarChartDataSerie(data)

// 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 ("barchart3.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 件のコメント:

コメントを投稿