Skip to content

feat: DecimalNumber() - added spacing between values and unit #3366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 4, 2023
51 changes: 47 additions & 4 deletions manim/mobject/text/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@
class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
"""An mobject representing a decimal number.

Parameters
----------
number
The numeric value to be displayed. It can later be modified using :meth:`.set_value`.
num_decimal_places
The number of decimal places after the decimal separator. Values are automatically rounded.
mob_class
The class for rendering digits and units, by default :class:`.MathTex`.
include_sign
Set to ``True`` to include a sign for positive numbers and zero.
group_with_commas
When ``True`` thousands groups are separated by commas for readability.
digit_buff_per_font_unit
Additional spacing between digits. Scales with font size.
show_ellipsis
When a number has been truncated by rounding, indicate with an ellipsis (``...``).
unit
A unit string which can be placed to the right of the numerical values.
unit_buff_per_font_unit
An additional spacing between the numerical values and the unit. A value
of ``unit_buff_per_font_unit=0.003`` gives a decent spacing. Scales with font size.
include_background_rectangle
Adds a background rectangle to increase contrast on busy scenes.
edge_to_fix
Assuring right- or left-alignment of the full object.
font_size
Size of the font.

Examples
--------

Expand All @@ -34,6 +62,8 @@ def construct(self):
show_ellipsis=True,
num_decimal_places=3,
include_sign=True,
unit=r"\text{M-Units}",
unit_buff_per_font_unit=0.003
)
square = Square().to_edge(UP)

Expand All @@ -59,6 +89,7 @@ def __init__(
digit_buff_per_font_unit: float = 0.001,
show_ellipsis: bool = False,
unit: str | None = None, # Aligned to bottom unless it starts with "^"
unit_buff_per_font_unit: float = 0,
include_background_rectangle: bool = False,
edge_to_fix: Sequence[float] = LEFT,
font_size: float = DEFAULT_FONT_SIZE,
Expand All @@ -75,6 +106,7 @@ def __init__(
self.digit_buff_per_font_unit = digit_buff_per_font_unit
self.show_ellipsis = show_ellipsis
self.unit = unit
self.unit_buff_per_font_unit = unit_buff_per_font_unit
self.include_background_rectangle = include_background_rectangle
self.edge_to_fix = edge_to_fix
self._font_size = font_size
Expand All @@ -89,6 +121,7 @@ def __init__(
"digit_buff_per_font_unit": digit_buff_per_font_unit,
"show_ellipsis": show_ellipsis,
"unit": unit,
"unit_buff_per_font_unit": unit_buff_per_font_unit,
"include_background_rectangle": include_background_rectangle,
"edge_to_fix": edge_to_fix,
"font_size": font_size,
Expand Down Expand Up @@ -130,15 +163,25 @@ def _set_submobjects_from_number(self, number):
self._string_to_mob("\\dots", SingleStringMathTex, color=self.color),
)

if self.unit is not None:
self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
self.add(self.unit_sign)

self.arrange(
buff=self.digit_buff_per_font_unit * self._font_size,
aligned_edge=DOWN,
)

if self.unit is not None:
self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex)
self.add(
self.unit_sign.next_to(
self,
direction=RIGHT,
buff=(self.unit_buff_per_font_unit + self.digit_buff_per_font_unit)
* self._font_size,
aligned_edge=DOWN,
)
)

self.move_to(ORIGIN)

# Handle alignment of parts that should be aligned
# to the bottom
for i, c in enumerate(num_string):
Expand Down