Datalabels#
Custom datalabel policy with filter
import easychart
datalabels = {
"enabled": True,
"format": "{point.y} {point.series.name} in {point.category} ({point.color})",
"filter": {"property": "y", "operator": ">", "value": 0},
}
chart = easychart.new("column")
chart.stacked = True
chart.title = "Adoptions per month, by animal"
chart.categories = ["June", "July", "August"]
chart.plot([1, 3, 5], name="cats", datalabels=datalabels)
chart.plot([1, 0, 4], name="dogs", datalabels=datalabels)
chart
Using named points with datalabel filter
import easychart
datalabels = {
"enabled": True,
"format": "{point.name}",
"filter": {"property": "y", "operator": ">", "value": 0},
}
def pluralize(quantity, label):
"""
Pluralize a category if quantity > 2
"""
if quantity < 2:
return label
return label + "s"
def label(quantity, category):
"""
Create a data label for a given point (y) and category (category)
"""
return str(quantity) + " " + pluralize(quantity, category)
chart = easychart.new("column")
chart.stacked = True
chart.title = "Adoptions per month, by animal"
chart.categories = ["June", "July", "August"]
chart.plot(
[{"y": y, "name": label(y, "cat")} for y in [1, 3, 5]],
name="cats",
datalabels=datalabels,
)
chart.plot(
[{"y": y, "name": label(y, "dog")} for y in [2, 1, 5]],
name="dogs",
datalabels=datalabels,
)
chart
With various label options enabled
import easychart
chart = easychart.new("column")
chart.stacked = True
chart.title = "Total fruit consumption, grouped by gender"
chart.categories = ["Apples", "Oranges", "Pears", "Grapes", "Bananas"]
with chart.yAxis as axis:
axis.allowDecimals = False
axis.min = 0
axis.title.text = "Number of fruits"
chart.tooltip = "{value:.0f}"
chart.plot(
[5, 3, 4, 7, 2],
name="John",
stack="male",
labels="{point.y} fruits <br /> ({point.percentage:.0f}%)",
)
chart.plot([3, 4, 4, 2, 5], name="Joe", stack="male", labels="{point.series.name}")
chart.plot([2, 5, 6, 2, 1], name="Jane", stack="female", labels="{point.category}")
chart.plot(
[3, 0, 4, 4, 3],
name="Janet",
stack="female",
labels="{point.y} out of {point.total}",
)
chart
Labeled pie chart
import easychart
labels = ["O", "A", "B", "AB"]
values = [45, 40, 11, 4]
chart = easychart.new("pie", title="Distribution of blood type in the US")
chart.subtitle = "Source: American Red Cross"
chart.tooltip = "{point.percentage:.0f}%"
chart.plot(values, index=labels, labels="{point.name} ({point.y}%)")
chart