1/*
2 * Copyright 2012 AndroidPlot.com
3 *
4 *    Licensed under the Apache License, Version 2.0 (the "License");
5 *    you may not use this file except in compliance with the License.
6 *    You may obtain a copy of the License at
7 *
8 *        http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *    Unless required by applicable law or agreed to in writing, software
11 *    distributed under the License is distributed on an "AS IS" BASIS,
12 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *    See the License for the specific language governing permissions and
14 *    limitations under the License.
15 */
16
17package com.androidplot.xy;
18
19import android.graphics.RectF;
20import com.androidplot.util.ValPixConverter;
21
22/**
23 * Calculates "stepping" values for a plot.  These values are most commonly used for
24 * drawing grid lines on a graph.
25 */
26public class XYStepCalculator {
27
28
29    /**
30     * Convenience method - wraps other form of getStep().
31     * @param plot
32     * @param axisType
33     * @param rect
34     * @param minVal
35     * @param maxVal
36     * @return
37     */
38    public static XYStep getStep(XYPlot plot, XYAxisType axisType, RectF rect, Number minVal, Number maxVal) {
39        XYStep step = null;
40        switch(axisType) {
41            case DOMAIN:
42                step = getStep(plot.getDomainStepMode(), rect.width(), plot.getDomainStepValue(), minVal, maxVal);
43                break;
44            case RANGE:
45                step = getStep(plot.getRangeStepMode(), rect.height(), plot.getRangeStepValue(), minVal, maxVal);
46                break;
47        }
48        return step;
49    }
50
51    public static XYStep getStep(XYStepMode typeXY, float plotPixelSize, double stepValue, Number minVal, Number maxVal) {
52        //XYStep step = new XYStep();
53        double stepVal = 0;
54        float stepPix = 0;
55        float stepCount = 0;
56        switch(typeXY) {
57            case INCREMENT_BY_VAL:
58                stepVal = stepValue;
59                stepPix = (float)(stepValue/ ValPixConverter.valPerPix(minVal.doubleValue(), maxVal.doubleValue(), plotPixelSize));
60                stepCount = plotPixelSize /stepPix;
61                break;
62            case INCREMENT_BY_PIXELS:
63                stepPix = new Double(stepValue).floatValue();
64                stepCount = plotPixelSize /stepPix;
65                stepVal = ValPixConverter.valPerPix(minVal.doubleValue(), maxVal.doubleValue(), plotPixelSize)*stepPix;
66                break;
67            case SUBDIVIDE:
68                stepCount = new Double(stepValue).floatValue();
69                stepPix = (plotPixelSize /(stepCount-1));
70                stepVal = ValPixConverter.valPerPix(minVal.doubleValue(), maxVal.doubleValue(), plotPixelSize)*stepPix;
71                break;
72        }
73        return new XYStep(stepCount, stepPix, stepVal);
74    }
75}
76