- Home ›
- JFreeChartを使ったグラフ作成 ›
- 描画領域の設定 ›
- HERE
背景画像の透明度を設定(setBackgroundImageAlpha)
広告
背景画像の透明度を設定する方法を確認します。Plotクラスで用意されている「setBackgroundImageAlpha」メソッドを使います。
setBackgroundImageAlpha public void setBackgroundImageAlpha(float alpha)
Sets the alpha transparency used when drawing the background image. Parameters: alpha - the alpha transparency (in the range 0.0f to 1.0f, where 0.0f is fully transparent, and 1.0f is fully opaque). Throws: java.lang.IllegalArgumentException - if alpha is not within the specified range.
引数には透明度を表すfloat型の数値を指定します。数値は0.0fから1.0fの間で指定し、0.0fを指定すると完全な透明となり1.0fを指定すると非透明となります。デフォルトでは0.5fが設定されています。
例えば次のように記述します。
JFreeChart chart = ChartFactory.createPieChart(...); /* 引数は省略 */ Plot plot = chart.getPlot(); BufferedImage image = null; try { image = ImageIO.read(new File("./hana.png")); } catch (Exception e) { e.printStackTrace(); image = null; } plot.setBackgroundImage(image); plot.setBackgroundImageAlpha(0.8f);
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import org.jfree.chart.JFreeChart; import org.jfree.chart.ChartFactory; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.chart.plot.PlotOrientation; import javax.swing.JFrame; import java.awt.BorderLayout; import org.jfree.chart.ChartPanel; import org.jfree.chart.plot.Plot; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; import org.jfree.ui.Align; public class Test10_1 extends JFrame{ public static void main(String[] args) { Test10_1 frame = new Test10_1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 500, 500); frame.setTitle("グラフサンプル"); frame.setVisible(true); } Test10_1(){ JFreeChart chart = ChartFactory.createBarChart("月別売上", "月", "売上", createData(), PlotOrientation.VERTICAL, true, false, false); Plot plot = chart.getPlot(); BufferedImage image = null; try { image = ImageIO.read(new File("./hana.png")); } catch (Exception e) { e.printStackTrace(); image = null; } plot.setBackgroundPaint(new Color(255, 250, 205)); plot.setBackgroundImage(image); plot.setBackgroundImageAlignment(Align.LEFT); plot.setBackgroundImageAlpha(0.9f); ChartPanel cpanel = new ChartPanel(chart); getContentPane().add(cpanel, BorderLayout.CENTER); } private DefaultCategoryDataset createData(){ DefaultCategoryDataset data = new DefaultCategoryDataset(); String[] series = {"本社", "大阪支社", "名古屋支社"}; String[] category = {"4月", "5月", "6月"}; data.addValue(800, series[0], category[0]); data.addValue(600, series[0], category[1]); data.addValue(900, series[0], category[2]); data.addValue(500, series[1], category[0]); data.addValue(300, series[1], category[1]); data.addValue(200, series[1], category[2]); data.addValue(300, series[2], category[0]); data.addValue(900, series[2], category[1]); data.addValue(600, series[2], category[2]); return data; } }
上記をコンパイルした後で実行すると次のようにJavaアプリケーションが起動します。
上記は 0.9f を設定した場合です。いくつか他の値を指定した場合の実行結果を下記に表示します。
0.5f : (デフォルト)
0.1f :
( Written by Tatsuo Ikura )