* Compute an interval that split range depending on targetedNumTicks, * while being "rounded" for nice display. Actually, we snap the labels * to be one of the following, multiply by a power of ten: * [10, 12, 15, 18, 20, 25, 30, 40, 50, 60, 80, 90] * (see ACCEPTABLE_LABELS) * * Return the computed number of ticks, that may differ from the target. */
| 220 | * Return the computed number of ticks, that may differ from the target. |
| 221 | */ |
| 222 | int SnapTicksToRoundValues(double range[2], int targetedNumTicks, double& interval) |
| 223 | { |
| 224 | double delta = std::fabs(range[1] - range[0]); |
| 225 | double roughInterval = delta / targetedNumTicks; |
| 226 | |
| 227 | // get order of magnitude of the range |
| 228 | int rootPower = static_cast<int>(std::floor(std::log10(roughInterval) - 1)); |
| 229 | double root = std::pow(10.0, rootPower); |
| 230 | |
| 231 | // roundedInterval will be between 10 and 100 inclusive of 10 but not 100 |
| 232 | // and has 2 significant digits |
| 233 | int roundedInterval = roughInterval / root; |
| 234 | |
| 235 | auto resulting = |
| 236 | std::lower_bound(ACCEPTABLE_LABELS.begin(), ACCEPTABLE_LABELS.end(), roundedInterval); |
| 237 | if (resulting != ACCEPTABLE_LABELS.end()) |
| 238 | { |
| 239 | roundedInterval = *resulting; |
| 240 | } |
| 241 | |
| 242 | // scale back rounded interval to actual range |
| 243 | interval = roundedInterval * root; |
| 244 | int numTicks = (delta / interval) + 1; |
| 245 | |
| 246 | return numTicks; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Update Range, so outRange can be split into `numberOfTick` |