Mercurial > hg > octave-nkf
changeset 13211:78744376463a
src/graphics.cc: Consistent log-scale axis tick labels.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 24 Sep 2011 17:31:19 -0400 |
parents | d0f9d7353ee5 |
children | e81b93284605 |
files | src/graphics.cc |
diffstat | 1 files changed, 44 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/graphics.cc +++ b/src/graphics.cc @@ -5375,17 +5375,55 @@ void axes::properties::calc_ticklabels (const array_property& ticks, - any_property& labels, bool /*logscale*/) + any_property& labels, bool logscale) { Matrix values = ticks.get ().matrix_value (); Cell c (values.dims ()); std::ostringstream os; - for (int i = 0; i < values.numel (); i++) - { - os.str (std::string ()); - os << values(i); - c(i) = os.str (); + if (logscale) + { + double significand; + double exponent; + double exp_max = 0.; + double exp_min = 0.; + + for (int i = 0; i < values.numel (); i++) + { + exp_max = std::max (exp_max, std::log10 (values(i))); + exp_min = std::max (exp_min, std::log10 (values(i))); + } + + for (int i = 0; i < values.numel (); i++) + { + if (values(i) < 0.) + exponent = gnulib::floor (std::log10 (-values(i))); + else + exponent = gnulib::floor (std::log10 (values(i))); + significand = values(i) * std::pow (10., -exponent); + os.str (std::string ()); + os << significand; + if (exponent < 0.) + { + os << "e-"; + exponent = -exponent; + } + else + os << "e+"; + if (exponent < 10. && (exp_max > 9 || exp_min < -9)) + os << "0"; + os << exponent; + c(i) = os.str (); + } + } + else + { + for (int i = 0; i < values.numel (); i++) + { + os.str (std::string ()); + os << values(i); + c(i) = os.str (); + } } labels = c;