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        /**
054         * itemLabelVisible 時に、最後の値のみ表示するかどうか[true:有効/false:無効]を指定します。
055         *
056         * これは、itemLabelVisible 属性に、"last" という設定値を指定した場合は、
057         * 最後のみラベル表示します。
058         * このメソッドでは、true が指定された場合は、"last" 属性が有効になったと
059         * 判断します。
060         *
061         * @og.rev 4.1.2.0 (2008/03/12) 新規追加
062         *
063         * @param       flag    最後の値のみ表示するかどうか[true:有効/false:無効]
064         */
065        public void setItemLabelLastVisible( final boolean flag ) {
066                isItemLabelLastVisible = flag;
067        }
068
069        /**
070         * drawItem と同等の機能を持った、高速版メソッドです。
071         *
072         * @og.rev 4.1.1.0 (2008/02/04) 新規追加
073         * @og.rev 4.1.2.0 (2008/03/12) ラベルのアンダーライン時にItemLavelを表示しない
074         *
075         * @param g2                    Graphics2Dオブジェクト
076         * @param state                 CategoryItemRendererStateオブジェクト
077         * @param dataArea              Rectangle2Dオブジェクト
078         * @param plot                  CategoryPlotオブジェクト
079         * @param domainAxis    CategoryAxisオブジェクト
080         * @param rangeAxis             ValueAxisオブジェクト
081         * @param dataset               CategoryDatasetオブジェクト
082         * @param serNo                 シリアル番号
083         */
084        public void drawItem2( final Graphics2D g2, final CategoryItemRendererState state,
085                        final Rectangle2D dataArea, final CategoryPlot plot, final CategoryAxis domainAxis,
086                        final ValueAxis rangeAxis, final CategoryDataset dataset, final int serNo ) {
087
088                int clmCount = dataset.getColumnCount();
089                int rowCount = dataset.getRowCount();
090                RectangleEdge edge = plot.getRangeAxisEdge();
091
092                PlotOrientation orientation = plot.getOrientation();
093                double minBarLen = getMinimumBarLength();
094                double barWidth  = state.getBarWidth();
095                boolean isDrawOutline = isDrawBarOutline() &&
096                                                                        state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD ;
097
098                // 4.1.2.0 (2008/03/12)
099                HybsCategoryAxis hybsAxis = null;
100                if( domainAxis instanceof HybsCategoryAxis ) {
101                        hybsAxis = (HybsCategoryAxis)domainAxis;
102                        hybsAxis.setItemLabelLastVisible( isItemLabelLastVisible );
103                }
104
105                for( int row=0; row<rowCount; row++ ) {
106                        boolean isItemLabelsVisible = isSeriesItemLabelsVisible( row );
107
108                        for( int column=0; column<clmCount; column++ ) {
109                                Number v1Num = dataset.getValue( row,column );
110                                if(v1Num == null) { continue; }
111                                double value = v1Num.doubleValue();
112
113                                // 書き出し開始位置をずらす。
114                                double barW0 = calculateBarW0( plot,orientation,dataArea,domainAxis,state,row,column );
115                                double[] barL0L1 = calculateBarL0L1( value );
116                                if (barL0L1 == null) { continue; }
117
118                                double transL0 = rangeAxis.valueToJava2D( barL0L1[0],dataArea,edge );
119                                double transL1 = rangeAxis.valueToJava2D( barL0L1[1],dataArea,edge );
120                                double barL0 = Math.min( transL0,transL1 );
121                                double barLength = Math.max( Math.abs( transL1 - transL0 ),minBarLen );
122
123                                // Bar の描画
124                                Rectangle2D bar = null;
125                                if( orientation == PlotOrientation.HORIZONTAL ) {
126                                        bar = new Rectangle2D.Double( barL0,barW0,barLength,barWidth );
127                                }
128                                else {
129                                        bar = new Rectangle2D.Double( barW0,barL0,barWidth,barLength );
130                                }
131
132                                Paint itemPaint = getItemPaint( row,column );
133                                // 4.3.1.1 (2008/08/23) 変数名を t ⇒ gpt に変更
134                                GradientPaintTransformer gpt = getGradientPaintTransformer();
135                                if( gpt != null && itemPaint instanceof GradientPaint ) {
136                                        itemPaint = gpt.transform( (GradientPaint) itemPaint,bar );
137                                }
138                                g2.setPaint( itemPaint );
139                                g2.fill( bar );
140
141                                // outline の描画
142                                if( isDrawOutline ) {
143                                        Stroke stroke = getItemOutlineStroke( row,column );
144                                        Paint paint = getItemOutlinePaint( row,column );
145                                        if( stroke != null && paint != null ) {
146                                                g2.setStroke( stroke );
147                                                g2.setPaint( paint );
148                                                g2.draw( bar );
149                                        }
150                                }
151
152                                // ItemLabel の描画
153                                CategoryItemLabelGenerator generator = getItemLabelGenerator( row,column );
154                                if( generator != null && isItemLabelsVisible ) {
155                                        // 4.1.2.0 (2008/03/12) アンダースコアの場合は、表示しない。
156                                        if( hybsAxis != null && hybsAxis.isViewItemLabel( column ) ) {
157                                                drawItemLabel( g2,dataset,row,column,plot,generator,bar,(value < 0.0) );
158                                        }
159                                }
160                                // 4.3.1.0 (2008/08/09) item entity の追加
161                                EntityCollection entities = state.getEntityCollection();
162                                if( entities != null ) {
163                                        addItemEntity( entities, dataset, row, column, bar );
164                                }
165                        }
166                }
167        }
168
169        /**
170         * この文字列と指定されたオブジェクトを比較します。
171         *
172         * 親クラスで、equals メソッドが実装されているため、警告がでます。
173         *
174         * @og.rev 5.1.8.0 (2010/07/01) findbug対応
175         * @og.rev 5.1.9.0 (2010/08/01) findbug対応
176         *
177         * @param       object  比較するオブジェクト
178         *
179         * @return      Objectが等しい場合は true、そうでない場合は false
180         */
181        @Override
182        public boolean equals( final Object object ) {
183                if( super.equals( object ) ) {
184                        return hsCode == ((HybsBarRenderer)object).hsCode;
185                }
186                return false;
187        }
188
189        /**
190         * このオブジェクトのハッシュコードを取得します。
191         *
192         * @og.rev 5.1.8.0 (2010/07/01) findbug対応
193         * @og.rev 5.1.9.0 (2010/08/01) findbug対応
194         *
195         * @return      ハッシュコード
196         */
197        @Override
198        public int hashCode() { return hsCode ; }
199}