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     */
016    package org.opengion.hayabusa.io;
017    
018    import java.awt.GradientPaint;
019    import java.awt.Graphics2D;
020    import java.awt.Paint;
021    import java.awt.Stroke;
022    import java.awt.geom.Rectangle2D;
023    
024    import org.jfree.chart.renderer.category.BarRenderer;
025    import org.jfree.chart.renderer.category.CategoryItemRendererState;
026    import org.jfree.chart.axis.CategoryAxis;
027    import org.jfree.chart.axis.ValueAxis;
028    import org.jfree.chart.labels.CategoryItemLabelGenerator;
029    import org.jfree.chart.plot.CategoryPlot;
030    import org.jfree.chart.plot.PlotOrientation;
031    import org.jfree.chart.entity.EntityCollection;
032    import org.jfree.data.category.CategoryDataset;
033    import org.jfree.ui.GradientPaintTransformer;
034    import 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     */
047    public 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    }