dvilasuero HF staff commited on
Commit
bead680
1 Parent(s): df4792a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -104,41 +104,64 @@ def donut_chart2() -> alt.Chart:
104
 
105
  return chart
106
 
 
 
 
 
107
  def donut_chart() -> alt.Chart:
108
- """
109
- This function returns a donut chart with the number of annotated and pending records.
110
- Returns:
111
- An altair chart with the donut chart.
112
- """
113
  source_dataset, results = obtain_source_target_datasets()
114
  pending_records = len(source_dataset)
115
  annotated_records = len(results)
116
 
117
- source = pd.DataFrame(
118
- {
119
- "values": [annotated_records, pending_records],
120
- "category": ["Completed", "Remaining"], # Add a new column for categories
121
- }
122
- )
123
 
 
124
  base = alt.Chart(source).encode(
125
- theta=alt.Theta("values:Q", stack=True),
126
- radius=alt.Radius(
127
- "values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)
128
- ),
129
- color=alt.Color("category:N", legend=alt.Legend(title="Category")),
130
  )
131
 
132
- c1 = base.mark_arc(innerRadius=20, stroke="#fff")
 
133
 
134
- c2 = base.mark_text(radiusOffset=10).encode(text="values:Q")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- chart = c1 + c2
 
 
 
 
 
 
 
137
 
138
  return chart
139
 
140
 
141
 
 
142
  def kpi_chart() -> alt.Chart:
143
  """
144
  This function returns a KPI chart with the total amount of annotators.
 
104
 
105
  return chart
106
 
107
+ import altair as alt
108
+ import pandas as pd
109
+ import os
110
+
111
  def donut_chart() -> alt.Chart:
112
+ # Load your data
 
 
 
 
113
  source_dataset, results = obtain_source_target_datasets()
114
  pending_records = len(source_dataset)
115
  annotated_records = len(results)
116
 
117
+ # Prepare data for the donut chart
118
+ source = pd.DataFrame({
119
+ "values": [annotated_records, pending_records],
120
+ "category": ["Completed", "Remaining"],
121
+ "colors": ["#4CAF50", "#757575"] # Green for Completed, Grey for Remaining
122
+ })
123
 
124
+ # Base chart for donut segments
125
  base = alt.Chart(source).encode(
126
+ theta=alt.Theta(field="values", type="quantitative", stack=True), # The angle encoding
127
+ color=alt.Color(field="colors", type="nominal", legend=None), # Direct color specification
128
+ tooltip=['category', 'values'] # Tooltips for interactivity
 
 
129
  )
130
 
131
+ # Arc marks for donut segments
132
+ arcs = base.mark_arc(innerRadius=100, outerRadius=150, stroke="#fff") # Increased inner radius for a thicker donut
133
 
134
+ # Calculate the midpoints of the arc segments for label placement
135
+ text_df = pd.DataFrame({
136
+ 'start_angle': [0],
137
+ 'end_angle': [360 * annotated_records / (annotated_records + pending_records)]
138
+ })
139
+ text_df = text_df.append({
140
+ 'start_angle': text_df.iloc[0]['end_angle'],
141
+ 'end_angle': 360
142
+ }, ignore_index=True)
143
+ text_df['mid_angle'] = (text_df['start_angle'] + text_df['end_angle']) / 2
144
+
145
+ # Create labels based on the mid_angle for better placement
146
+ text = alt.Chart(text_df).mark_text(radius=120, size=20, color='black').encode(
147
+ text=alt.Text('mid_angle:O', format='.1f'),
148
+ angle=alt.Angle('mid_angle:Q')
149
+ )
150
 
151
+ # Combine the arcs and text
152
+ chart = arcs + text
153
+
154
+ # Configure the view to have a transparent background
155
+ chart = chart.configure_view(
156
+ strokeWidth=0, # Remove border around chart
157
+ fill=None # Ensure background is transparent
158
+ )
159
 
160
  return chart
161
 
162
 
163
 
164
+
165
  def kpi_chart() -> alt.Chart:
166
  """
167
  This function returns a KPI chart with the total amount of annotators.