1 /* 2 * Copyright (C) 2006 The Android Open Source Project 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 17 package com.android.traceview; 18 19 class TickScaler { 20 21 private double mMinVal; // required input 22 private double mMaxVal; // required input 23 private double mRangeVal; 24 private int mNumPixels; // required input 25 private int mPixelsPerTick; // required input 26 private double mPixelsPerRange; 27 private double mTickIncrement; 28 private double mMinMajorTick; 29 30 TickScaler(double minVal, double maxVal, int numPixels, int pixelsPerTick) { 31 mMinVal = minVal; 32 mMaxVal = maxVal; 33 mNumPixels = numPixels; 34 mPixelsPerTick = pixelsPerTick; 35 } 36 37 public void setMinVal(double minVal) { 38 mMinVal = minVal; 39 } 40 41 public double getMinVal() { 42 return mMinVal; 43 } 44 45 public void setMaxVal(double maxVal) { 46 mMaxVal = maxVal; 47 } 48 49 public double getMaxVal() { 50 return mMaxVal; 51 } 52 53 public void setNumPixels(int numPixels) { 54 mNumPixels = numPixels; 55 } 56 57 public int getNumPixels() { 58 return mNumPixels; 59 } 60 61 public void setPixelsPerTick(int pixelsPerTick) { 62 mPixelsPerTick = pixelsPerTick; 63 } 64 65 public int getPixelsPerTick() { 66 return mPixelsPerTick; 67 } 68 69 public void setPixelsPerRange(double pixelsPerRange) { 70 mPixelsPerRange = pixelsPerRange; 71 } 72 73 public double getPixelsPerRange() { 74 return mPixelsPerRange; 75 } 76 77 public void setTickIncrement(double tickIncrement) { 78 mTickIncrement = tickIncrement; 79 } 80 81 public double getTickIncrement() { 82 return mTickIncrement; 83 } 84 85 public void setMinMajorTick(double minMajorTick) { 86 mMinMajorTick = minMajorTick; 87 } 88 89 public double getMinMajorTick() { 90 return mMinMajorTick; 91 } 92 93 // Convert a time value to a 0-based pixel value 94 public int valueToPixel(double value) { 95 return (int) Math.ceil(mPixelsPerRange * (value - mMinVal) - 0.5); 96 } 97 98 // Convert a time value to a 0-based fractional pixel 99 public double valueToPixelFraction(double value) { 100 return mPixelsPerRange * (value - mMinVal); 101 } 102 103 // Convert a 0-based pixel value to a time value 104 public double pixelToValue(int pixel) { 105 return mMinVal + (pixel / mPixelsPerRange); 106 } 107 108 public void computeTicks(boolean useGivenEndPoints) { 109 int numTicks = mNumPixels / mPixelsPerTick; 110 mRangeVal = mMaxVal - mMinVal; 111 mTickIncrement = mRangeVal / numTicks; 112 double dlogTickIncrement = Math.log10(mTickIncrement); 113 int logTickIncrement = (int) Math.floor(dlogTickIncrement); 114 double scale = Math.pow(10, logTickIncrement); 115 double scaledTickIncr = mTickIncrement / scale; 116 if (scaledTickIncr > 5.0) 117 scaledTickIncr = 10; 118 else if (scaledTickIncr > 2) 119 scaledTickIncr = 5; 120 else if (scaledTickIncr > 1) 121 scaledTickIncr = 2; 122 else 123 scaledTickIncr = 1; 124 mTickIncrement = scaledTickIncr * scale; 125 126 if (!useGivenEndPoints) { 127 // Round up the max val to the next minor tick 128 double minorTickIncrement = mTickIncrement / 5; 129 double dval = mMaxVal / minorTickIncrement; 130 int ival = (int) dval; 131 if (ival != dval) 132 mMaxVal = (ival + 1) * minorTickIncrement; 133 134 // Round down the min val to a multiple of tickIncrement 135 ival = (int) (mMinVal / mTickIncrement); 136 mMinVal = ival * mTickIncrement; 137 mMinMajorTick = mMinVal; 138 } else { 139 int ival = (int) (mMinVal / mTickIncrement); 140 mMinMajorTick = ival * mTickIncrement; 141 if (mMinMajorTick < mMinVal) 142 mMinMajorTick = mMinMajorTick + mTickIncrement; 143 } 144 145 mRangeVal = mMaxVal - mMinVal; 146 mPixelsPerRange = (double) mNumPixels / mRangeVal; 147 } 148 } 149