Skip to content

Commit

Permalink
PieChart update (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
myrmarachne authored Dec 10, 2024
1 parent f13cbd4 commit f56a2d7
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions qf_lib/plotting/charts/pie_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple
from typing import Tuple, Optional

import numpy as np

Expand All @@ -27,41 +27,34 @@ class PieChart(Chart):
----------
data: QFSeries
The series to plot in the pie chart.
slices_distance: float
slices_distance: Tuple[float, QFSeries]
The distance between slices. Default is 0.01
sort_values:
plot_settings
Options to pass to the ``pie`` function.
"""

def __init__(self, data: QFSeries, slices_distance: float = 0.01, **plot_settings):
def __init__(self, data: QFSeries, slices_distance: Tuple[float, QFSeries] = 0.01, sort_values: Optional[bool] = True, **plot_settings):
super().__init__()
self.plot_settings = plot_settings

self.distance = slices_distance

self.assert_is_qfseries(data)
self.data = data.sort_values(ascending=False)
self.data = data.sort_values(ascending=False) if sort_values else data
self.distance = slices_distance if not isinstance(slices_distance, QFSeries) else slices_distance.reindex_like(self.data)

def plot(self, figsize: Tuple[float, float] = None) -> None:
self._setup_axes_if_necessary(figsize)

plot_kwargs = self.plot_settings
separate = ((self.distance, ) * len(self.data))
separate = ((self.distance, ) * len(self.data)) if isinstance(self.distance, float) else self.distance.values
wedges, _ = self.axes.pie(self.data, startangle=90, counterclock=False, explode=separate, **plot_kwargs)

arrow_props = {
'arrowstyle': '-',
'color': 'black'
}
bbox_props = {
"boxstyle": "square,pad=0.3",
"fc": "w",
"ec": "k",
"lw": 0.72
}
kw = {
'arrowprops': arrow_props,
'bbox': bbox_props,
'zorder': 0,
'va': 'center'
}
Expand All @@ -79,8 +72,11 @@ def plot(self, figsize: Tuple[float, float] = None) -> None:
kw["arrowprops"].update({"connectionstyle": connection_style})
horizontal_alignment = "right" if x <= 0 else "left"

self.axes.annotate(labels[i], xy=(0.8 * x, 0.8 * y), xytext=((1.3 + (i % 2) * 0.4) * np.sign(x), 1.4 * yc),
horizontalalignment=horizontal_alignment, **kw)
label_pos = ((1.3 + (i % 2) * 0.4) * np.sign(x), 1.4 * yc)

self.axes.annotate(labels[i], xy=(0.8 * x, 0.8 * y), xytext=label_pos,
horizontalalignment=horizontal_alignment,
**kw)

self._apply_decorators()
self._adjust_style()

0 comments on commit f56a2d7

Please sign in to comment.