# Install required packages (if not already installed)
#install.packages("echarts4r")
#install.packages("htmlwidgets")

# Load the necessary libraries
library(echarts4r)
## Warning: 程序包'echarts4r'是用R版本4.4.3 来建造的
library(htmlwidgets)
## Warning: 程序包'htmlwidgets'是用R版本4.4.3 来建造的
# Create a larger dataset with 30 daily activities
df <- data.frame(
  activity = c(
    "Work", "Sleep", "Commute", "Eat", "Watch TV", "Exercise", "Study", "Social Media",
    "Reading", "Housework", "Shopping", "Gaming", "Cooking", "Family Time", "Nap",
    "Pet Care", "Walking", "Music", "Yoga", "Laundry", "Cleaning", "Emails",
    "Meditation", "Driving", "Meetings", "Gardening", "Online Learning", "News",
    "Creative Writing", "Others"
  ),
  hours = c(
    8, 6.5, 1.5, 1.5, 1.5, 1, 1.2, 1, 
    0.5, 1, 0.5, 0.5, 1, 1, 0.5,
    0.3, 0.5, 0.5, 0.5, 0.3, 0.4, 0.3,
    0.3, 0.4, 0.6, 0.3, 0.5, 0.3,
    0.5, 1
  )
)

# Create the pie chart
p <- df |>
  e_charts(activity) |>
  e_pie(
    hours,
    radius = "65%",  # Size of the pie chart
    label = list(
      formatter = "{b}\n{d}%",  # Show activity name and percentage
      position = "outside",
      fontSize = 10
    ),
    labelLine = list(
      show = TRUE,       # Show lines connecting labels
      length = 15,       # First segment length
      length2 = 30,      # Second segment length
      smooth = TRUE      # Enable curved (bent) lines
    )
  ) |>
  e_title("Daily Activities (30 Categories)") |>
  e_tooltip("item")  # Enable tooltip on hover
p
# Export the chart to a standalone HTML file
saveWidget(p, file = "pie_chart.html", selfcontained = TRUE)

# Print completion message
cat("✅ pie_chart.html has been successfully created with 30 activities.\n")
## <U+2705> pie_chart.html has been successfully created with 30 activities.