001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
006 * 
007 * http://www.object-refinery.com/orsoncharts/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package com.orsoncharts.renderer.category;
034
035import com.orsoncharts.Range;
036import com.orsoncharts.data.category.CategoryDataset3D;
037import com.orsoncharts.data.DataUtils;
038import com.orsoncharts.data.Values3D;
039import com.orsoncharts.graphics3d.Dimension3D;
040import com.orsoncharts.graphics3d.World;
041import com.orsoncharts.plot.CategoryPlot3D;
042import com.orsoncharts.Chart3DFactory;
043import com.orsoncharts.data.KeyedValues3DItemKey;
044import com.orsoncharts.graphics3d.Object3D;
045import com.orsoncharts.graphics3d.Offset3D;
046import com.orsoncharts.label.ItemLabelPositioning;
047
048/**
049 * A renderer that can be used with the {@link CategoryPlot3D} class to create
050 * 3D stacked bar charts from data in a {@link CategoryDataset3D}.  The 
051 * {@code createStackedBarChart()} method in the {@link Chart3DFactory} 
052 * class will construct a chart that uses this renderer.  Here is a sample:
053 * <div>
054 * <object id="ABC" data="../../../../doc-files/StackedBarChart3DDemo1.svg"  
055 * type="image/svg+xml" width="500" height="359"></object>
056 * </div>
057 * (refer to {@code StackedBarChart3DDemo1.java} for the code to generate 
058 * the above chart).
059 * <br><br> 
060 * There is a factory method to create a chart using this renderer - see
061 * {@link Chart3DFactory#createStackedBarChart(String, String, CategoryDataset3D, String, String, String)}.
062 * <br><br>
063 * NOTE: This class is serializable, but the serialization format is subject 
064 * to change in future releases and should not be relied upon for persisting 
065 * instances of this class.
066 */
067@SuppressWarnings("serial")
068public class StackedBarRenderer3D extends BarRenderer3D {
069
070    /**
071     * Creates a default constructor.
072     */
073    public StackedBarRenderer3D() {
074        super();
075        setItemLabelPositioning(ItemLabelPositioning.FRONT_AND_BACK);
076        setItemLabelOffsets(new Offset3D(0.0, 0.0, -1.0));
077    }
078    
079    /**
080     * Returns the range of values that will be required on the value axis
081     * to see all the data from the dataset.  We override the method to 
082     * account for the bars from each series being stacked on top of one 
083     * another.
084     * 
085     * @param data  the data ({@code null} not permitted).
086     * 
087     * @return The range (possibly {@code null}) 
088     */
089    @Override
090    public Range findValueRange(Values3D<? extends Number> data) {
091        return DataUtils.findStackedValueRange(data);
092    }
093    
094    /**
095     * Constructs and places one item from the specified dataset into the given 
096     * world.  This method will be called by the {@link CategoryPlot3D} class
097     * while iterating over the items in the dataset.
098     * 
099     * @param dataset  the dataset ({@code null} not permitted).
100     * @param series  the series index.
101     * @param row  the row index.
102     * @param column  the column index.
103     * @param world  the world ({@code null} not permitted).
104     * @param dimensions  the plot dimensions ({@code null} not permitted).
105     * @param xOffset  the x-offset.
106     * @param yOffset  the y-offset.
107     * @param zOffset  the z-offset.
108     */
109    @Override
110    @SuppressWarnings("unchecked")
111    public void composeItem(CategoryDataset3D dataset, int series, int row, 
112            int column, World world, Dimension3D dimensions,  
113            double xOffset, double yOffset, double zOffset) {
114        
115        double value = dataset.getDoubleValue(series, row, column);
116        if (Double.isNaN(value)) {
117            return;
118        }        
119        double[] stack = DataUtils.stackSubTotal(dataset, getBase(), series,
120                row, column);
121        double lower = stack[1];
122        if (value < 0.0) {
123            lower = stack[0];
124        }
125        double upper = lower + value;
126        composeItem(upper, lower, dataset, series, row, column, world, 
127                dimensions, xOffset, yOffset, zOffset);
128        
129    }
130    
131    @Override
132    protected void drawItemLabels(World world, CategoryDataset3D dataset, 
133            KeyedValues3DItemKey itemKey, double xw, double yw, double zw, 
134            double basew, boolean inverted) {
135        ItemLabelPositioning positioning = getItemLabelPositioning();
136        if (getItemLabelGenerator() != null) {
137            String label = getItemLabelGenerator().generateItemLabel(dataset, 
138                   itemKey.getSeriesKey(), itemKey.getRowKey(), 
139                   itemKey.getColumnKey());
140            if (label != null) {
141                Dimension3D dimensions = getPlot().getDimensions();
142                double dx = getItemLabelOffsets().getDX();
143                double dy = getItemLabelOffsets().getDY() 
144                        * dimensions.getHeight();
145                double dz = getItemLabelOffsets().getDZ() * getBarZWidth();
146                if (positioning.equals(ItemLabelPositioning.CENTRAL)) {
147                    double yy = yw;
148                    if (inverted) {
149                        yy = basew;
150                        dy = -dy;
151                    }
152                    Object3D labelObj = Object3D.createLabelObject(label, 
153                            getItemLabelFont(), getItemLabelColor(), 
154                            getItemLabelBackgroundColor(), xw + dx, 
155                            yy + dy, zw, false, true);
156                    labelObj.setProperty(Object3D.ITEM_KEY, itemKey);
157                    world.add(labelObj);
158                } else if (positioning.equals(
159                        ItemLabelPositioning.FRONT_AND_BACK)) {
160                    double yy = (yw + basew) / 2.0;
161                    Object3D labelObj1 = Object3D.createLabelObject(label, 
162                            getItemLabelFont(), getItemLabelColor(), 
163                            getItemLabelBackgroundColor(), xw + dx, 
164                            yy + dy, zw + dz, false, false);
165                    labelObj1.setProperty(Object3D.ITEM_KEY, itemKey);
166                    world.add(labelObj1);
167                    Object3D labelObj2 = Object3D.createLabelObject(label, 
168                            getItemLabelFont(), getItemLabelColor(), 
169                            getItemLabelBackgroundColor(), xw + dx, 
170                            yy + dy, zw - dz, true, false);
171                    labelObj2.setProperty(Object3D.ITEM_KEY, itemKey);
172                    world.add(labelObj2);
173                }
174            }
175        }        
176    }    
177    
178    /**
179     * Tests this renderer for equality with an arbitrary object.
180     * 
181     * @param obj  the object ({@code null} permitted).
182     * 
183     * @return A boolean. 
184     */
185    @Override
186    public boolean equals(Object obj) {
187        if (obj == this) {
188            return true;
189        }
190        if (!(obj instanceof StackedBarRenderer3D)) {
191            return false;
192        }
193        return super.equals(obj);
194    }
195    
196}