Skip to content

Commit e910ef3

Browse files
committed
Number precision style settings added.
1 parent 7349039 commit e910ef3

File tree

14 files changed

+81
-55
lines changed

14 files changed

+81
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
animation control methods are chainable.
99
- CSS properties can be used to style vizzu charts
1010
E.g. `--vizzu-plot-marker-colorPalette: whatever` for `{style: {plot: {marker: {colorPalette: "whatever"}}}}`
11+
- `maxFractionDigits` style parameter added to labels showing numbers.
1112
- WASM build size reduction.
1213

1314
### Fixed

src/apps/weblib/js-api/vizzu.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ interface Text {
279279
data such as marker and axis labels. 'grouped' uses thousand separators,
280280
'prefixed' uses scientific notation. */
281281
numberFormat?: 'none'|'grouped'|'prefixed'|null;
282+
/** The maximum number of digits in fraction part if the text contains a
283+
number. */
284+
maxFractionDigits?: number|null;
282285
}
283286

284287
/** The following CSS like filters can be used to alter the color:

src/base/conv/numtostr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ class NumberToString {
3535

3636
}
3737

38-
#endif //CONV_NUMTOSTR
38+
#endif

src/base/text/smartstring.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,6 @@ void SmartString::trimBOM(std::string &string)
7171
}
7272
}
7373

74-
std::string SmartString::fromNumber(double value, NumberFormat format)
75-
{
76-
switch(format) {
77-
case NumberFormat::prefixed: {
78-
return humanReadable(value);
79-
}
80-
case NumberFormat::grouped: {
81-
Conv::NumberToString converter;
82-
converter.fractionDigitCount = 10;
83-
converter.integerGgrouping = ' ';
84-
return converter(value);
85-
}
86-
default:
87-
case NumberFormat::none: {
88-
Conv::NumberToString converter;
89-
return converter(value);
90-
}
91-
}
92-
}
93-
9474
std::vector<std::string> SmartString::split(
9575
const std::string &str, char delim, bool ignoreEmpty)
9676
{
@@ -139,15 +119,15 @@ std::vector<std::string> SmartString::split(const std::string &str,
139119
return result;
140120
}
141121

142-
std::string SmartString::humanReadable(double value, int digits,
143-
const std::vector<std::string> &prefixes)
122+
std::string SmartString::fromNumber(double value, size_t digits)
144123
{
145-
Math::EngineeringNumber num(value);
124+
auto negative = value < 0;
125+
auto absValue = std::abs(value);
146126

147-
std::string res = std::to_string(num.coefficient);
127+
std::string res = std::to_string(absValue);
148128

149-
if (!Math::Floating(num.coefficient).isInteger()
150-
&& num.coefficient < pow(10, digits - 1))
129+
if (!Math::Floating(absValue).isInteger()
130+
&& absValue < pow(10, digits - 1))
151131
{
152132
res = res.substr(0, digits + 1);
153133
}
@@ -163,15 +143,49 @@ std::string SmartString::humanReadable(double value, int digits,
163143
if (ch == '.') break;
164144
}
165145

166-
if(!num.positive)
167-
res = '-' + res;
146+
if(negative) res = '-' + res;
147+
148+
return res;
149+
}
150+
151+
std::string SmartString::fromNumber(
152+
double value,
153+
NumberFormat format,
154+
size_t maxFractionDigits)
155+
{
156+
switch(format) {
157+
case NumberFormat::prefixed: {
158+
return humanReadable(value, maxFractionDigits);
159+
}
160+
case NumberFormat::grouped: {
161+
Conv::NumberToString converter;
162+
converter.fractionDigitCount = maxFractionDigits;
163+
converter.integerGgrouping = ' ';
164+
return converter(value);
165+
}
166+
default:
167+
case NumberFormat::none: {
168+
Conv::NumberToString converter;
169+
converter.fractionDigitCount = maxFractionDigits;
170+
return converter(value);
171+
}
172+
}
173+
}
174+
175+
std::string SmartString::humanReadable(double value, int maxFractionDigits,
176+
const std::vector<std::string> &prefixes)
177+
{
178+
Math::EngineeringNumber num(value);
179+
180+
std::string res = fromNumber(num.coefficient,
181+
NumberFormat::none, maxFractionDigits);
168182

169183
if (num.exponent >= 0 && num.exponent < (int)prefixes.size())
170184
{
171185
auto prefix = prefixes.at(num.exponent);
172186
return res + (!prefix.empty() ? " " + prefix : "");
173187
}
174-
else return fromNumber(value);
188+
else return fromNumber(value, NumberFormat::none, maxFractionDigits);
175189
}
176190

177191
std::string SmartString::deescape(const std::string &str)

src/base/text/smartstring.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ class SmartString
6565

6666
static void trimBOM(std::string &string);
6767

68+
static std::string fromNumber(double value, size_t digits);
69+
6870
static std::string fromNumber(double value,
69-
NumberFormat format = NumberFormat::none);
71+
NumberFormat format = NumberFormat::none,
72+
size_t maxFractionDigits = 2);
7073

7174
static std::string humanReadable(double value,
72-
int digits = 6,
75+
int maxFractionDigits = 2,
7376
const std::vector<std::string> &prefixes
7477
= {"", "k", "M", "G", "T", "P", "E", "Z", "Y"});
7578

src/chart/generator/marker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ std::string Marker::toJson(const Data::DataCube &data) const {
154154
"\"values\":{"
155155
+ Text::SmartString::join(values, ",") +
156156
"},"
157-
"\"id\":"
158-
+ Text::SmartString::fromNumber(idx) +
157+
"\"id\":" + std::to_string(idx) +
159158
"}";
160159
}
161160

src/chart/main/style.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Chart Chart::def()
9898
.textAlign = Anim::Interpolated<Text::TextAlign>
9999
(Text::TextAlign::center),
100100
.backgroundColor = Gfx::Color(),
101-
.numberFormat = ::Text::NumberFormat::grouped
101+
.numberFormat = ::Text::NumberFormat::grouped,
102+
.maxFractionDigits = 3
102103
}
103104
},
104105
.orientation = Anim::Interpolated
@@ -133,7 +134,8 @@ Chart Chart::def()
133134
.textAlign = Anim::Interpolated<Text::TextAlign>
134135
(Text::TextAlign::left),
135136
.backgroundColor = Gfx::Color(),
136-
.numberFormat = ::Text::NumberFormat::prefixed
137+
.numberFormat = ::Text::NumberFormat::prefixed,
138+
.maxFractionDigits = 3
137139
}
138140
},
139141
.position = Anim::Interpolated<AxisTitle::Position>
@@ -167,7 +169,8 @@ Chart Chart::def()
167169
.textAlign = Anim::Interpolated<Text::TextAlign>
168170
(Text::TextAlign::left),
169171
.backgroundColor = Gfx::Color(),
170-
.numberFormat = ::Text::NumberFormat::prefixed
172+
.numberFormat = ::Text::NumberFormat::prefixed,
173+
.maxFractionDigits = 3
171174
}
172175
},
173176
.orientation = Anim::Interpolated
@@ -216,7 +219,8 @@ Chart Chart::def()
216219
.textAlign = Anim::Interpolated<Text::TextAlign>
217220
(Text::TextAlign::left),
218221
.backgroundColor = Gfx::Color(),
219-
.numberFormat = ::Text::NumberFormat::prefixed
222+
.numberFormat = ::Text::NumberFormat::prefixed,
223+
.maxFractionDigits = 3
220224
}
221225
},
222226
.position = Anim::Interpolated<AxisTitle::Position>
@@ -250,7 +254,8 @@ Chart Chart::def()
250254
.textAlign = Anim::Interpolated<Text::TextAlign>
251255
(Text::TextAlign::left),
252256
.backgroundColor = Gfx::Color(),
253-
.numberFormat = ::Text::NumberFormat::prefixed
257+
.numberFormat = ::Text::NumberFormat::prefixed,
258+
.maxFractionDigits = 3
254259
}
255260
},
256261
.orientation = Anim::Interpolated
@@ -313,7 +318,8 @@ Chart Chart::def()
313318
.textAlign = Anim::Interpolated<Text::TextAlign>
314319
(Text::TextAlign::left),
315320
.backgroundColor = Gfx::Color(),
316-
.numberFormat = ::Text::NumberFormat::prefixed
321+
.numberFormat = ::Text::NumberFormat::prefixed,
322+
.maxFractionDigits = 3
317323
},
318324
},
319325
.label = {
@@ -334,7 +340,8 @@ Chart Chart::def()
334340
.textAlign = Anim::Interpolated<Text::TextAlign>
335341
(Text::TextAlign::left),
336342
.backgroundColor = Gfx::Color(),
337-
.numberFormat = ::Text::NumberFormat::prefixed
343+
.numberFormat = ::Text::NumberFormat::prefixed,
344+
.maxFractionDigits = 3
338345
},
339346
},
340347
.marker = {
@@ -361,7 +368,8 @@ Chart Chart::def()
361368
.textAlign = Anim::Interpolated<Text::TextAlign>
362369
(Text::TextAlign::center),
363370
.backgroundColor = Gfx::Color(),
364-
.numberFormat = ::Text::NumberFormat::prefixed
371+
.numberFormat = ::Text::NumberFormat::prefixed,
372+
.maxFractionDigits = 3
365373
},
366374
},
367375
.tooltip = {

src/chart/main/style.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,16 @@ struct Text {
117117
Param<Anim::Interpolated<TextAlign>> textAlign;
118118
Param<Gfx::Color> backgroundColor;
119119
Param<::Text::NumberFormat> numberFormat;
120+
Param<double> maxFractionDigits;
120121

121122
void visit(auto &visitor)
122123
{
123124
visitor
124125
(color, "color")
125126
(textAlign, "textAlign")
126127
(backgroundColor, "backgroundColor")
127-
(numberFormat, "numberFormat");
128+
(numberFormat, "numberFormat")
129+
(maxFractionDigits, "maxFractionDigits");
128130
}
129131
};
130132

src/chart/rendering/drawinterlacing.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ void drawInterlacing::drawDataLabel(bool horizontal,
185185
auto &labelStyle = style.plot.getAxis(axisIndex).label;
186186

187187
auto str = Text::SmartString::fromNumber(value,
188-
*labelStyle.numberFormat);
188+
*labelStyle.numberFormat,
189+
*labelStyle.maxFractionDigits);
189190

190191
if (!unit.empty())
191192
{

src/chart/rendering/drawitem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ std::string drawItem::getLabelText(size_t index) const
250250
if (values[index].value.hasValue())
251251
{
252252
valueStr = Text::SmartString::fromNumber(value,
253-
*labelStyle.numberFormat);
253+
*labelStyle.numberFormat,
254+
*labelStyle.maxFractionDigits);
254255

255256
if(!values[index].value.unit.empty())
256257
{

0 commit comments

Comments
 (0)