data = range(1, 6)
pie = Pie(sizes=data)
fig = Figure(marks=[pie], animation_duration=1000)
# Add `animation_duration` (in milliseconds) to have smooth transitions
display(fig)

"""
Explanation: Basic Pie Chart
Generating the simplest possible pie chart and plotting it in a Figure.
The area and angle of each slice will be proportional to the data passed to sizes
End of explanation
"""


Nslices = 5
pie.sizes = np.random.rand(Nslices)

"""
Explanation: As with all bqplot Marks, pie data can be dynamically modified
End of explanation
"""


pie.sort = True

"""
Explanation: Sort the pie slices by ascending size
End of explanation
"""


pie.selected_style = {"opacity": "1", "stroke": "white", "stroke-width": "2"}
pie.unselected_style = {"opacity": "0.2"}
pie.selected = [3]

pie.selected = None

"""
Explanation: Setting different styles for selected slices
End of explanation
"""


pie.labels = ['{:.2f}'.format(d) for d in pie.sizes]
fig

"""
Explanation: For more on piechart interactions, see the Mark Interactions notebook
Adding labels
End of explanation
"""


pie.label_color = 'white'
pie.font_size = '20px'
pie.font_weight = 'normal'

"""
Explanation: Modify label styling
End of explanation
"""


pie1 = Pie(sizes=np.random.rand(6), inner_radius=0.05)
fig1 = Figure(marks=[pie1], animation_duration=1000)
display(fig1)

"""
Explanation: Updating pie shape and style
End of explanation
"""


# As of now, the radius sizes are absolute, in pixels
pie1.radius = 250
pie1.inner_radius = 80

# Angles are in radians, 0 being the top vertical
pie1.start_angle = -90
pie1.end_angle = 90

"""
Explanation: Change pie dimensions
End of explanation
"""


pie1.y = 0.9
pie1.x = 0.4
pie1.radius = 320

"""
Explanation: Moving the pie around
x and y attributes control the position of the pie in the figure.
If no scales are passed for x and y, they are taken in absolute 
figure coordinates, between 0 and 1.
End of explanation
"""


pie1.stroke = 'brown'
pie1.colors = ['orange', 'darkviolet']
pie1.opacities = [.1, 1]
display(fig1)

"""
Explanation: Changing slice styles
Pie slice colors cycle through the colors and opacities attribute, as the Lines Mark.
End of explanation
"""


from bqplot import ColorScale, ColorAxis

Nslices = 7
size_data = np.random.rand(Nslices)
color_data = np.random.randn(Nslices)

sc = ColorScale(scheme='Reds')
# The ColorAxis gives a visual representation of its ColorScale
ax = ColorAxis(scale=sc)

pie2 = Pie(sizes=size_data, scales={'color': sc}, color=color_data)
Figure(marks=[pie2], axes=[ax])

"""
Explanation: Representing an additional dimension using Color
The Pie allows for its colors to be determined by data, that is passed to the color attribute. 
A ColorScale with the desired color scheme must also be passed.
End of explanation
"""


from datetime import datetime
from bqplot.traits import convert_to_date
from bqplot import DateScale, LinearScale, Axis

avg_precipitation_days = [(d/30., 1-d/30.) for d in [2, 3, 4, 6, 12, 17, 23, 22, 15, 4, 1, 1]]
temperatures = [9, 12, 16, 20, 22, 23, 22, 22, 22, 20, 15, 11]

dates = [datetime(2010, k, 1) for k in range(1, 13)]

sc_x = DateScale()
sc_y = LinearScale()
ax_x = Axis(scale=sc_x, label='month', tick_format='%B')
ax_y = Axis(scale=sc_y, orientation='vertical', label='average temperature')

pies = [Pie(sizes=precipit, x=date, y=temp, 
            scales={"x": sc_x, "y": sc_y}, radius=30., stroke='navy',
            colors=['navy', 'navy'], opacities=[1, .1]) 
        for precipit, date, temp in zip(avg_precipitation_days, dates, temperatures)]

Figure(title='Kathmandu precipitation', marks=pies, axes=[ax_x, ax_y],
       padding_x=.05, padding_y=.1)

"""
Explanation: Positioning the Pie using custom scales
Pies can be positioned, via the x and y attributes, 
using either absolute figure scales or custom 'x' or 'y' scales
End of explanation
"""


