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.awt.GradientPaint;
019import java.awt.Graphics2D;
020import java.awt.Paint;
021import java.awt.Stroke;
022import java.awt.geom.Rectangle2D;
023
024import org.jfree.chart.renderer.category.BarRenderer;
025import org.jfree.chart.renderer.category.CategoryItemRendererState;
026import org.jfree.chart.axis.CategoryAxis;
027import org.jfree.chart.axis.ValueAxis;
028import org.jfree.chart.labels.CategoryItemLabelGenerator;
029import org.jfree.chart.plot.CategoryPlot;
030import org.jfree.chart.plot.PlotOrientation;
031import org.jfree.chart.entity.EntityCollection;
032import org.jfree.data.category.CategoryDataset;
033import org.jfree.ui.GradientPaintTransformer;
034import org.jfree.ui.RectangleEdge;
035
036/**
037 * HybsBarRenderer は、org.jfree.chart.renderer.category.BarRenderer を
038 * 拡張したカスタマイズクラスです。
039 * これは、描画に対して、予め制限を設けて、処理速度の向上を図っています。
040 *
041 * @og.rev 4.1.1.0 (2008/02/04) 新規作成
042 *
043 * @version  0.9.0      2001/05/05
044 * @author       Kazuhiko Hasegawa
045 * @since        JDK1.1,
046 */
047public class HybsBarRenderer extends BarRenderer implements HybsDrawItem {
048        private static final long serialVersionUID = 519020100801L ;
049
050        private boolean isItemLabelLastVisible = false; // 4.1.2.0 (2008/03/12)
051        private final int hsCode = Long.valueOf( System.nanoTime() ).hashCode() ;       // 5.1.9.0 (2010/08/01) equals,hashCode
052
053        // 4.3.4.4 (2009/01/01)
054//      /**
055//       * Creates a new bar renderer with default settings.
056//       */
057//      public HybsBarRenderer() {
058//              super();
059//      }
060
061        /**
062         * itemLabelVisible 時に、最後の値のみ表示するかどうか[true:有効/false:無効]を指定します。
063         *
064         * これは、itemLabelVisible 属性に、"last" という設定値を指定した場合は、
065         * 最後のみラベル表示します。
066         * このメソッドでは、true が指定された場合は、"last" 属性が有効になったと
067         * 判断します。
068         *
069         * @og.rev 4.1.2.0 (2008/03/12) 新規追加
070         *
071         * @param       flag    最後の値のみ表示するかどうか[true:有効/false:無効]
072         */
073        public void setItemLabelLastVisible( final boolean flag ) {
074                isItemLabelLastVisible = flag;
075        }
076
077        /**
078         * drawItem と同等の機能を持った、高速版メソッドです。
079         *
080         * @og.rev 4.1.1.0 (2008/02/04) 新規追加
081         * @og.rev 4.1.2.0 (2008/03/12) ラベルのアンダーライン時にItemLavelを表示しない
082         *
083         * @param g2                    Graphics2Dオブジェクト
084         * @param state                 CategoryItemRendererStateオブジェクト
085         * @param dataArea              Rectangle2Dオブジェクト
086         * @param plot                  CategoryPlotオブジェクト
087         * @param domainAxis    CategoryAxisオブジェクト
088         * @param rangeAxis             ValueAxisオブジェクト
089         * @param dataset               CategoryDatasetオブジェクト
090         * @param serNo                 シリアル番号
091         */
092        public void drawItem2( final Graphics2D g2, final CategoryItemRendererState state,
093                        final Rectangle2D dataArea, final CategoryPlot plot, final CategoryAxis domainAxis,
094                        final ValueAxis rangeAxis, final CategoryDataset dataset, final int serNo ) {
095
096                int clmCount = dataset.getColumnCount();
097                int rowCount = dataset.getRowCount();
098                RectangleEdge edge = plot.getRangeAxisEdge();
099
100                PlotOrientation orientation = plot.getOrientation();
101                double minBarLen = getMinimumBarLength();
102                double barWidth  = state.getBarWidth();
103                boolean isDrawOutline = isDrawBarOutline() &&
104                                                                        state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD ;
105
106                // 4.1.2.0 (2008/03/12)
107                HybsCategoryAxis hybsAxis = null;
108                if( domainAxis instanceof HybsCategoryAxis ) {
109                        hybsAxis = (HybsCategoryAxis)domainAxis;
110                        hybsAxis.setItemLabelLastVisible( isItemLabelLastVisible );
111                }
112
113                for( int row=0; row<rowCount; row++ ) {
114                        boolean isItemLabelsVisible = isSeriesItemLabelsVisible( row );
115
116                        for( int column=0; column<clmCount; column++ ) {
117                                Number v1Num = dataset.getValue( row,column );
118                                if(v1Num == null) { continue; }
119                                double value = v1Num.doubleValue();
120
121                                // 書き出し開始位置をずらす。
122                                double barW0 = calculateBarW0( plot,orientation,dataArea,domainAxis,state,row,column );
123                                double[] barL0L1 = calculateBarL0L1( value );
124                                if (barL0L1 == null) { continue; }
125
126                                double transL0 = rangeAxis.valueToJava2D( barL0L1[0],dataArea,edge );
127                                double transL1 = rangeAxis.valueToJava2D( barL0L1[1],dataArea,edge );
128                                double barL0 = Math.min( transL0,transL1 );
129                                double barLength = Math.max( Math.abs( transL1 - transL0 ),minBarLen );
130
131                                // Bar の描画
132                                Rectangle2D bar = null;
133                                if( orientation == PlotOrientation.HORIZONTAL ) {
134                                        bar = new Rectangle2D.Double( barL0,barW0,barLength,barWidth );
135                                }
136                                else {
137                                        bar = new Rectangle2D.Double( barW0,barL0,barWidth,barLength );
138                                }
139
140                                Paint itemPaint = getItemPaint( row,column );
141                                // 4.3.1.1 (2008/08/23) 変数名を t ⇒ gpt に変更
142                                GradientPaintTransformer gpt = getGradientPaintTransformer();
143                                if( gpt != null && itemPaint instanceof GradientPaint ) {
144                                        itemPaint = gpt.transform( (GradientPaint) itemPaint,bar );
145                                }
146                                g2.setPaint( itemPaint );
147                                g2.fill( bar );
148
149                                // outline の描画
150                                if( isDrawOutline ) {
151                                        Stroke stroke = getItemOutlineStroke( row,column );
152                                        Paint paint = getItemOutlinePaint( row,column );
153                                        if( stroke != null && paint != null ) {
154                                                g2.setStroke( stroke );
155                                                g2.setPaint( paint );
156                                                g2.draw( bar );
157                                        }
158                                }
159
160                                // ItemLabel の描画
161                                CategoryItemLabelGenerator generator = getItemLabelGenerator( row,column );
162                                if( generator != null && isItemLabelsVisible ) {
163                                        // 4.1.2.0 (2008/03/12) アンダースコアの場合は、表示しない。
164                                        if( hybsAxis != null && hybsAxis.isViewItemLabel( column ) ) {
165                                                drawItemLabel( g2,dataset,row,column,plot,generator,bar,(value < 0.0) );
166                                        }
167                                }
168                                // 4.3.1.0 (2008/08/09) item entity の追加
169                                EntityCollection entities = state.getEntityCollection();
170                                if( entities != null ) {
171                                        addItemEntity( entities, dataset, row, column, bar );
172                                }
173                        }
174                }
175        }
176
177        /**
178         * この文字列と指定されたオブジェクトを比較します。
179         *
180         * 親クラスで、equals メソッドが実装されているため、警告がでます。
181         *
182         * @og.rev 5.1.8.0 (2010/07/01) findbug対応
183         * @og.rev 5.1.9.0 (2010/08/01) findbug対応
184         *
185         * @param       object  比較するオブジェクト
186         *
187         * @return      Objectが等しい場合は true、そうでない場合は false
188         */
189        @Override
190        public boolean equals( final Object object ) {
191//              return super.equals( object );
192                if( super.equals( object ) ) {
193                        return hsCode == ((HybsBarRenderer)object).hsCode;
194                }
195                return false;
196        }
197
198        /**
199         * このオブジェクトのハッシュコードを取得します。
200         *
201         * @og.rev 5.1.8.0 (2010/07/01) findbug対応
202         * @og.rev 5.1.9.0 (2010/08/01) findbug対応
203         *
204         * @return      ハッシュコード
205         */
206//      public int hashCode() { return super.hashCode() ; }
207        @Override
208        public int hashCode() { return hsCode ; }
209
210        /**
211         * このオブジェクトと指定されたオブジェクトを比較します。
212         *
213         * @og.rev 4.3.1.1 (2008/08/23) 新規追加
214         * @og.rev 5.1.8.0 (2010/07/01) 廃止
215         *
216         * @param anObject Object 比較されるオブジェクト
217         *
218         * @return      指定されたオブジェクトが等しい場合は true、そうでない場合は false
219         */
220//      public boolean equals( final Object anObject ) {
221//              if( super.equals( anObject ) ) {
222//                      HybsBarRenderer other = ((HybsBarRenderer)anObject);
223//                      return ( isItemLabelLastVisible == other.isItemLabelLastVisible );
224//              }
225//              return false;
226//      }
227
228        /**
229         * このオブジェクトのハッシュコードを返します。
230         *
231         * @og.rev 4.3.1.1 (2008/08/23) 新規追加
232         * @og.rev 5.1.8.0 (2010/07/01) 廃止
233         *
234         * @return      このオブジェクトのハッシュコード値
235         */
236//      public int hashCode() {
237//              return super.hashCode() + ( (isItemLabelLastVisible) ? 1 : 0 );
238//      }
239}