File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313#include < iostream>
1414#include < sstream>
1515#include < tuple>
16+ #include < vector>
17+
18+ #include " actsvg/core/defs.hpp"
1619
1720namespace actsvg {
1821
1922namespace utils {
2023
24+ // How to format values in string output
25+ enum class value_format : unsigned int {
26+ e_scientific,
27+ e_default,
28+ };
29+
2130/* * Helper method to run enumerate(...) with structured binding
2231 * over STL type containers.
2332 *
@@ -135,7 +144,8 @@ std::string id_to_url(const std::string &id_);
135144 *
136145 * @return a string
137146 */
138- std::string to_string (const scalar &s_, size_t pr_ = 4 );
147+ std::string to_string (const scalar &s_, size_t pr_ = 4 ,
148+ value_format f_ = value_format::e_default);
139149
140150/* * Helper to format point2
141151 *
Original file line number Diff line number Diff line change @@ -1086,25 +1086,27 @@ svg::object gradient_box(
10861086
10871087 // Create the tick value
10881088 scalar value = std::get<1 >(stops_[is]);
1089+ std::string val_str{
1090+ utils::to_string (value, 4 , utils::value_format::e_scientific)};
10891091
10901092 svg::object tval =
10911093 vertical
10921094 ? draw::text (
10931095 id_ + " _tick_val_" + std::to_string (is),
10941096 {static_cast <scalar>(p_[0 ] + w_ + 0.2 * font_._size ),
10951097 p_[1 ] + h_ * s.first },
1096- {utils::to_string (value) }, font_)
1098+ {val_str }, font_)
10971099 : draw::text (id_ + " _tick_val_" + std::to_string (is),
10981100 {p_[0 ] + w_ * s.first ,
10991101 static_cast <scalar>(p_[1 ] - 1.2 * font_._size )},
1100- {utils::to_string (value) }, font_);
1102+ {val_str }, font_);
11011103 tval._sterile = true ;
11021104 tval._attribute_map [" alignment-baseline" ] = " middle" ;
11031105 if (not vertical) {
11041106 tval._attribute_map [" text-anchor" ] = " middle" ;
11051107 }
11061108 font_.attach_attributes (tval);
1107- tval._field = {utils::to_string (value )};
1109+ tval._field = {std::move (val_str )};
11081110 g.add_object (tval);
11091111 }
11101112
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ rgb gradient::rgb_from_scale(scalar s_) const {
4242 s_ < 0 ._scalar ? 0 ._scalar : (s_ > 1 ._scalar ? 1 ._scalar : s_);
4343 // find our stops
4444 unsigned int is = 1u ;
45- for (; is <= _stops.size (); ++is) {
45+ for (; is < _stops.size (); ++is) {
4646 if (_stops[is].first > s_reg) {
4747 break ;
4848 }
Original file line number Diff line number Diff line change 66// License, v. 2.0. If a copy of the MPL was not distributed with this
77// file, You can obtain one at http://mozilla.org/MPL/2.0/.
88
9+ #include " actsvg/core/utils.hpp"
10+
911#include < cmath>
1012#include < iomanip>
1113#include < iostream>
1214#include < sstream>
1315#include < tuple>
1416
15- #include " actsvg/core/defs.hpp"
16-
1717namespace actsvg {
1818
1919namespace utils {
2020std::string id_to_url (const std::string &id_) {
2121 return std::string (" url(#" ) + id_ + " )" ;
2222}
2323
24- std::string to_string (const scalar &s_, size_t pr_) {
24+ std::string to_string (const scalar &s_, size_t pr_, value_format f_ ) {
2525 std::stringstream sstream;
26+ if (f_ == value_format::e_scientific) {
27+ sstream << std::scientific;
28+ }
2629 sstream << std::setprecision (pr_) << s_;
2730 return sstream.str ();
2831}
You can’t perform that action at this time.
0 commit comments