001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.io;
017
018import java.util.List;
019import java.awt.Color;
020
021import org.jfree.data.general.Dataset;
022import org.jfree.data.general.PieDataset;
023import org.jfree.data.category.CategoryDataset;
024import org.jfree.data.xy.XYDataset;
025import org.jfree.chart.plot.Plot;
026import org.jfree.chart.plot.MultiplePiePlot;
027import org.jfree.chart.plot.PiePlot;
028import org.jfree.chart.plot.PiePlot3D;
029import org.jfree.chart.plot.RingPlot;
030import org.jfree.chart.plot.SpiderWebPlot;
031import org.jfree.chart.plot.PolarPlot;
032import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
033import org.jfree.chart.labels.StandardPieToolTipGenerator;
034
035/**
036 * ChartPlot_Pie は、ChartPlot インターフェースを継承した実体クラスです。
037 * JFreeChart では、各種オブジェクトの組み合わせで、色々なグラフを作成できます。
038 * チャートタイプが、複数種類存在するため、ここでは、特殊な方法として、各タイプ毎に
039 * オブジェクトを構築しています。(ファクトリメソッド的な処理)
040 *
041 * @version  0.9.0      2007/06/21
042 * @author       Kazuhiko Hasegawa
043 * @since        JDK1.1,
044 */
045public class ChartPlot_Pie implements ChartPlot {
046
047        /**
048         * デフォルトコンストラクター
049         *
050         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
051         */
052        public ChartPlot_Pie() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
053
054        /**
055         * Plot オブジェクトを取得します。
056         *
057         * Plot オブジェクト には、その種類の応じた、データセットやレンデラーを
058         * 設定する必要があります。
059         * また、複数のデータセットや、それに関係する属性情報も、設定する必要が
060         * あります。
061         * Plot は、JFreeChart オブジェクトにつき、一つ用意しなければなりません。
062         * チャート合成時でも、Plot は一つです。
063         *
064         * @og.rev 5.3.0.0 (2010/12/01) 特殊プロットの追加
065         * @og.rev 5.7.8.0 (2014/07/04) MeterPlot 、Compass 、Thermometer の機能追加
066         * @og.rev 6.4.9.0 (2016/07/23) Pie,Pie3D,Ring に、色指定を追加。これらは、同じ設定が使えるので、マージします。
067         *
068         * @param       create  ChartCreateオブジェクト
069         *
070         * @return      Plotオブジェクト
071         */
072        public Plot getPlot( final ChartCreate create ) {
073
074                final List<ChartDataset> datasetList = create.getDatasetList();
075                final ChartDataset chDataset = datasetList.get(0);
076
077                final Dataset dtset = chDataset.getDataset();
078
079                // クリッカブル・マップ
080                final HybsURLGenerator urlGen = create.getURLGenerator();
081                final boolean useToolTip = create.isUseToolTip();               // 4.9.9.9 (2009/08/07) メソッド名変更
082
083                Plot plot = null;
084                final String type = chDataset.getChartType();
085                // 6.4.9.0 (2016/07/23) Pie,Pie3D,Ring は、同じ設定が使えるので、マージします。
086                if( "Pie".equalsIgnoreCase( type ) || "Pie3D".equalsIgnoreCase( type ) || "Ring".equalsIgnoreCase( type ) ) {
087                        final PiePlot pplot ;
088                        switch( type ) {
089                                case "Pie"              : pplot = new PiePlot();        break;
090                                case "Pie3D"    : pplot = new PiePlot3D();      break;
091                                case "Ring"             : pplot = new RingPlot();       break;
092                                default                 : pplot = new PiePlot();        break;
093                        }
094
095                        pplot.setDataset( (PieDataset)dtset );
096                        if( urlGen != null ) {
097                                pplot.setURLGenerator( urlGen );
098                        }
099                        if( useToolTip ){       // 4.3.1.0 (2008/08/09) ツールチップスの利用
100                                pplot.setToolTipGenerator( new StandardPieToolTipGenerator() );
101                        }
102
103                        // 6.4.9.0 (2016/07/23) 色指定の反映。indexが非推奨なので、キー指定に変更。
104                        final Color[] clrs = chDataset.getSeriesColors();
105                        if( clrs != null && clrs.length>0 ) {
106                                final int len = ((PieDataset)dtset).getItemCount();
107                                for( int i=0;i<clrs.length && i<len; i++ ) {
108                                        final Comparable<?> key = ((PieDataset)dtset).getKey(i);
109                                        pplot.setSectionPaint( key,clrs[i] );
110                                }
111                        }
112                        plot = pplot;
113                }
114                else if( "MultiplePie".equalsIgnoreCase( type ) ) {
115                        plot = new MultiplePiePlot();
116                        ((MultiplePiePlot)plot).setDataset( (CategoryDataset)dtset );
117                }
118                else if( "SpiderWeb".equalsIgnoreCase( type ) ) {
119                        plot = new SpiderWebPlot();
120                        ((SpiderWebPlot)plot).setDataset( (CategoryDataset)dtset );
121                        if( urlGen != null ) {
122                                ((SpiderWebPlot)plot).setURLGenerator( urlGen );
123                        }
124                        if( useToolTip ){       // 4.3.1.0 (2008/08/09) ツールチップスの利用
125                                ((SpiderWebPlot)plot).setToolTipGenerator( new StandardCategoryToolTipGenerator() );
126                        }
127
128                        // 6.4.9.0 (2016/07/23) 色指定の反映。
129                        final Color[] clrs = chDataset.getSeriesColors();
130                        if( clrs != null && clrs.length>0 ) {
131                                for( int i=0;i<clrs.length; i++ ) {
132                                        ((SpiderWebPlot)plot).setSeriesPaint( i,clrs[i] );
133                                }
134                        }
135                }
136                // 5.3.0.0 (2010/12/01) 特殊プロットの追加
137                else if( "Polar".equalsIgnoreCase( type ) ) {
138                        plot = new PolarPlot();
139                        ((PolarPlot)plot).setDataset( (XYDataset)dtset );
140                }
141                else if( "Meter".equalsIgnoreCase( type ) ) {
142                        // 5.7.8.0 (2014/07/04) 機能追加
143                        plot = chDataset.makeMeterPlot();
144                }
145                else if( "Thermometer".equalsIgnoreCase( type ) ) {
146                        // 5.7.8.0 (2014/07/04) 機能追加
147                        plot = chDataset.makeThermometerPlot();
148                }
149                else if( "Compass".equalsIgnoreCase( type ) ) {
150                        // 5.7.8.0 (2014/07/04) 機能追加
151                        plot = chDataset.makeCompassPlot();
152                }
153
154                return plot;
155        }
156}