Skip to content

Commit d457da9

Browse files
committed
working visualisation
1 parent 3440fbc commit d457da9

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

llm-complete-guide/steps/populate_index.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ def create_charts(stats: Dict[str, Dict[str, int]]) -> Image.Image:
104104
draw = ImageDraw.Draw(image)
105105

106106
# Draw the histogram of chunk sizes
107-
histogram_width = 400
108-
histogram_height = 300
107+
histogram_width = 600
108+
histogram_height = 250
109109
histogram_data = [
110110
document_stats["min_chunk_size"],
111111
document_stats["avg_chunk_size"],
112112
document_stats["max_chunk_size"],
113113
]
114114
histogram_labels = ["Min", "Avg", "Max"]
115-
histogram_x = 50
115+
histogram_x = (image_width - histogram_width) // 2
116116
histogram_y = 50
117117
draw_histogram(
118118
draw,
@@ -125,12 +125,12 @@ def create_charts(stats: Dict[str, Dict[str, int]]) -> Image.Image:
125125
)
126126

127127
# Draw the bar chart of chunk counts per section
128-
bar_chart_width = 400
129-
bar_chart_height = 300
128+
bar_chart_width = 600
129+
bar_chart_height = 250
130130
bar_chart_data = list(chunks_per_section.values())
131131
bar_chart_labels = list(chunks_per_section.keys())
132-
bar_chart_x = 450
133-
bar_chart_y = 50
132+
bar_chart_x = (image_width - bar_chart_width) // 2
133+
bar_chart_y = histogram_y + histogram_height + 50
134134
draw_bar_chart(
135135
draw,
136136
bar_chart_x,
@@ -143,8 +143,10 @@ def create_charts(stats: Dict[str, Dict[str, int]]) -> Image.Image:
143143

144144
# Add a title to the combined image
145145
title_text = "Document Chunk Statistics"
146-
title_font = ImageFont.truetype("arial.ttf", 24)
147-
title_width, title_height = draw.textsize(title_text, font=title_font)
146+
title_font = ImageFont.load_default(size=24)
147+
title_bbox = draw.textbbox((0, 0), title_text, font=title_font)
148+
title_width = title_bbox[2] - title_bbox[0]
149+
title_height = title_bbox[3] - title_bbox[1]
148150
title_x = (image_width - title_width) // 2
149151
title_y = 10
150152
draw.text((title_x, title_y), title_text, font=title_font, fill="black")
@@ -190,18 +192,20 @@ def draw_histogram(
190192

191193
# Draw the label below the bar
192194
label_text = labels[i]
193-
label_font = ImageFont.truetype("arial.ttf", 12)
194-
label_width, label_height = draw.textsize(label_text, font=label_font)
195+
label_font = ImageFont.load_default(size=12)
196+
label_bbox = draw.textbbox((0, 0), label_text, font=label_font)
197+
label_width = label_bbox[2] - label_bbox[0]
198+
label_height = label_bbox[3] - label_bbox[1]
195199
label_x = bar_x + (bar_width - label_width) // 2
196200
label_y = y + height + 5
197-
draw.text(
198-
(label_x, label_y), label_text, font=label_font, fill="black"
199-
)
201+
draw.text((label_x, label_y), label_text, font=label_font, fill="black")
200202

201203
# Draw the title above the histogram
202204
title_text = "Chunk Size Distribution"
203-
title_font = ImageFont.truetype("arial.ttf", 16)
204-
title_width, title_height = draw.textsize(title_text, font=title_font)
205+
title_font = ImageFont.load_default(size=16)
206+
title_bbox = draw.textbbox((0, 0), title_text, font=title_font)
207+
title_width = title_bbox[2] - title_bbox[0]
208+
title_height = title_bbox[3] - title_bbox[1]
205209
title_x = x + (width - title_width) // 2
206210
title_y = y - title_height - 10
207211
draw.text((title_x, title_y), title_text, font=title_font, fill="black")
@@ -245,18 +249,20 @@ def draw_bar_chart(
245249

246250
# Draw the label below the bar
247251
label_text = labels[i]
248-
label_font = ImageFont.truetype("arial.ttf", 12)
249-
label_width, label_height = draw.textsize(label_text, font=label_font)
252+
label_font = ImageFont.load_default(size=12)
253+
label_bbox = draw.textbbox((0, 0), label_text, font=label_font)
254+
label_width = label_bbox[2] - label_bbox[0]
255+
label_height = label_bbox[3] - label_bbox[1]
250256
label_x = bar_x + (bar_width - label_width) // 2
251257
label_y = y + height + 5
252-
draw.text(
253-
(label_x, label_y), label_text, font=label_font, fill="black"
254-
)
258+
draw.text((label_x, label_y), label_text, font=label_font, fill="black")
255259

256260
# Draw the title above the bar chart
257261
title_text = "Chunk Counts per Section"
258-
title_font = ImageFont.truetype("arial.ttf", 16)
259-
title_width, title_height = draw.textsize(title_text, font=title_font)
262+
title_font = ImageFont.load_default(size=16)
263+
title_bbox = draw.textbbox((0, 0), title_text, font=title_font)
264+
title_width = title_bbox[2] - title_bbox[0]
265+
title_height = title_bbox[3] - title_bbox[1]
260266
title_x = x + (width - title_width) // 2
261267
title_y = y - title_height - 10
262268
draw.text((title_x, title_y), title_text, font=title_font, fill="black")

0 commit comments

Comments
 (0)