Repository navigation
Expand file tree
/
Copy pathtable_image_raster_lib.py
More file actions
390 lines (346 loc) · 12.5 KB
/
Copy pathtable_image_raster_lib.py
File metadata and controls
390 lines (346 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""Reusable HTML table raster chart library.
Renders data as a grid of colored HTML table cells, producing
chart images that survive Blackboard HTML sanitizer (no SVG,
Canvas, or JavaScript required).
"""
# Standard Library
import math
#===========================================================
#===========================================================
class TableImageRaster:
"""Rasterize 2-D data onto a grid of HTML table cells.
Each cell is a fixed-size colored square. Three rendering layers
(guides, curves, dots) are composited with dot > curve > guide
priority. Axis labels and right-margin annotations are supported.
"""
#===========================================================
#===========================================================
def __init__(
self, x_range: tuple, y_range: tuple,
cols: int, rows: int, cell_px: int = 12
):
"""Create a new raster chart.
Args:
x_range: (x_min, x_max) data range for horizontal axis.
y_range: (y_min, y_max) data range for vertical axis.
cols: number of grid columns.
rows: number of grid rows.
cell_px: pixel width and height of each cell (default 12).
"""
self.x_min, self.x_max = x_range
self.y_min, self.y_max = y_range
self.cols = cols
self.rows = rows
self.cell_px = cell_px
# Computed spacing between grid cells in data units
self.x_step = (self.x_max - self.x_min) / max(1, cols - 1)
self.y_step = (self.y_max - self.y_min) / max(1, rows - 1)
# Dash stride adapts to cell size for ~12px visual dashes
self._dash_stride = max(1, round(12.0 / cell_px))
# Cell layers: (row, col) -> color string
self._curve_cells = {}
self._guide_cells = {}
self._dot_cells = {}
# Axis configuration
self._y_tick_interval = None
self._x_tick_values = []
self._right_labels = {}
self._x_axis_title = None
# Default colors
self.color_curve = '#2563eb'
self.color_guide = '#d1d5db'
self.color_dot = '#1f2937'
self.color_bg = '#f8fafc'
self.color_border = '#e5e7eb'
#===========================================================
#===========================================================
def _x_to_col(self, x: float) -> int:
"""Map an x data value to the nearest grid column index."""
col = int(round((x - self.x_min) / self.x_step))
return col
#===========================================================
#===========================================================
def _y_to_row(self, y: float) -> int:
"""Map a y data value to the nearest grid row index."""
row = int(round((y - self.y_min) / self.y_step))
return row
#===========================================================
#===========================================================
@staticmethod
def _parse_hex(hex_color: str) -> tuple:
"""Parse '#rrggbb' hex color to (r, g, b) integer tuple."""
h = hex_color.lstrip('#')
r = int(h[0:2], 16)
g = int(h[2:4], 16)
b = int(h[4:6], 16)
return (r, g, b)
#===========================================================
#===========================================================
@staticmethod
def _blend_hex(fg_hex: str, bg_hex: str, alpha: float) -> str:
"""Blend foreground over background at given alpha.
Args:
fg_hex: foreground color as '#rrggbb'.
bg_hex: background color as '#rrggbb'.
alpha: blend factor (0.0 = pure bg, 1.0 = pure fg).
Returns:
str: blended color as '#rrggbb'.
"""
fg_rgb = TableImageRaster._parse_hex(fg_hex)
bg_rgb = TableImageRaster._parse_hex(bg_hex)
blended = []
for fc, bc in zip(fg_rgb, bg_rgb):
blended.append(int(fc * alpha + bc * (1 - alpha)))
result = f"#{blended[0]:02x}{blended[1]:02x}{blended[2]:02x}"
return result
#===========================================================
#===========================================================
def plot_curve(
self, points: list, color: str = None,
antialiased: bool = False
) -> None:
"""Plot a curve from a sequence of (x, y) data points.
Each point is mapped to the nearest grid cell and colored.
Points outside the grid are silently clipped.
Args:
points: iterable of (x, y) tuples.
color: CSS color string (default: self.color_curve).
antialiased: if True, use bilinear sub-pixel blending
to smooth curve edges.
"""
if color is None:
color = self.color_curve
if not antialiased:
# Simple nearest-cell rendering
for x, y in points:
col = self._x_to_col(x)
row = self._y_to_row(y)
if 0 <= col < self.cols and 0 <= row < self.rows:
self._curve_cells[(row, col)] = color
return
# Anti-aliased: bilinear sub-pixel blending across 4 neighbors
alpha_map = {}
for x, y in points:
col_f = (x - self.x_min) / self.x_step
row_f = (y - self.y_min) / self.y_step
col_lo = int(math.floor(col_f))
row_lo = int(math.floor(row_f))
col_frac = col_f - col_lo
row_frac = row_f - row_lo
# Bilinear weights for the four surrounding cells
weights = [
(row_lo, col_lo, (1 - row_frac) * (1 - col_frac)),
(row_lo, col_lo + 1, (1 - row_frac) * col_frac),
(row_lo + 1, col_lo, row_frac * (1 - col_frac)),
(row_lo + 1, col_lo + 1, row_frac * col_frac),
]
for row, col, alpha in weights:
# Skip near-zero contributions
if alpha < 0.05:
continue
if not (0 <= row < self.rows and 0 <= col < self.cols):
continue
key = (row, col)
# Keep the highest alpha per cell
if alpha > alpha_map.get(key, 0):
alpha_map[key] = alpha
blended = self._blend_hex(
color, self.color_bg, alpha
)
self._curve_cells[key] = blended
#===========================================================
#===========================================================
def add_hline(
self, y: float, x_max: float = None,
color: str = None, dashed: bool = True
) -> None:
"""Add a horizontal guide line at a given y value.
Args:
y: y data value for the line.
x_max: draw from x_min to x_max (default: full width).
color: CSS color (default: self.color_guide).
dashed: if True, alternate on/off cells for a dashed look.
"""
if color is None:
color = self.color_guide
row = self._y_to_row(y)
if x_max is not None:
end_col = self._x_to_col(x_max) + 1
else:
end_col = self.cols
stride2 = self._dash_stride * 2
for c in range(min(end_col, self.cols)):
if not dashed or (c % stride2 < self._dash_stride):
if 0 <= row < self.rows:
self._guide_cells[(row, c)] = color
#===========================================================
#===========================================================
def add_vline(
self, x: float, y_max: float = None,
color: str = None, dashed: bool = True
) -> None:
"""Add a vertical guide line at a given x value.
Args:
x: x data value for the line.
y_max: draw from y_min up to y_max (default: full height).
color: CSS color (default: self.color_guide).
dashed: if True, alternate on/off cells for a dashed look.
"""
if color is None:
color = self.color_guide
col = self._x_to_col(x)
if y_max is not None:
end_row = self._y_to_row(y_max) + 1
else:
end_row = self.rows
stride2 = self._dash_stride * 2
for r in range(min(end_row, self.rows)):
if not dashed or (r % stride2 < self._dash_stride):
if 0 <= col < self.cols:
self._guide_cells[(r, col)] = color
#===========================================================
#===========================================================
def add_crosshair(
self, x: float, y: float,
color: str = None, dashed: bool = True
) -> None:
"""Add crossing horizontal and vertical guide lines at (x, y).
Convenience wrapper around add_hline and add_vline.
"""
self.add_hline(y, x_max=x, color=color, dashed=dashed)
self.add_vline(x, y_max=y, color=color, dashed=dashed)
#===========================================================
#===========================================================
def add_dot(
self, x: float, y: float, color: str = None
) -> None:
"""Place a dot marker at a data point.
Dots render with highest priority (above curves and guides).
"""
if color is None:
color = self.color_dot
col = self._x_to_col(x)
row = self._y_to_row(y)
if 0 <= col < self.cols and 0 <= row < self.rows:
self._dot_cells[(row, col)] = color
#===========================================================
#===========================================================
def set_y_tick_interval(self, interval: float) -> None:
"""Set the interval for y-axis tick labels."""
self._y_tick_interval = interval
#===========================================================
#===========================================================
def set_x_tick_values(self, values: list) -> None:
"""Set specific x values to label on the x-axis."""
self._x_tick_values = list(values)
#===========================================================
#===========================================================
def add_right_label(self, y: float, text: str) -> None:
"""Add an annotation label on the right margin at y value."""
row = self._y_to_row(y)
self._right_labels[row] = text
#===========================================================
#===========================================================
def set_x_axis_title(self, title: str) -> None:
"""Set the title shown below the x-axis."""
self._x_axis_title = title
#===========================================================
#===========================================================
def to_html(self) -> str:
"""Render the chart as an HTML table string.
Cells are composited with priority: dot > curve > guide > empty.
Uses table-layout:fixed so column widths are set once in the
first row, keeping subsequent rows compact.
Returns:
str: self-contained HTML table fragment.
"""
px = self.cell_px
has_right = bool(self._right_labels)
# Width style only needed on first-row data cells
w_sty = f"width:{px}px"
# Open table with fixed layout for compact cell rendering
html = ""
html += "<table style='table-layout:fixed;"
html += "border-collapse:collapse;margin:8px auto;"
html += f"background:{self.color_bg};"
html += f"border:1px solid {self.color_border};'>"
# Data rows from top (high y) to bottom (low y)
is_first_row = True
for row_idx in range(self.rows - 1, -1, -1):
y_val = self.y_min + row_idx * self.y_step
# Row height set here instead of per-cell
html += f"<tr style='height:{px}px'>"
# Left column: y-axis tick label
label = ""
if self._y_tick_interval is not None:
quotient = y_val / self._y_tick_interval
if abs(quotient - round(quotient)) < 0.01:
label = f"{y_val:g}"
if is_first_row:
html += "<td style='width:20px;font-size:10px;"
else:
html += "<td style='font-size:10px;"
html += f"text-align:right;padding-right:2px;'>{label}</td>"
# Data cells for the grid
for col_idx in range(self.cols):
key = (row_idx, col_idx)
# Priority: dot > curve > guide > empty
if key in self._dot_cells:
bg = self._dot_cells[key]
elif key in self._curve_cells:
bg = self._curve_cells[key]
elif key in self._guide_cells:
bg = self._guide_cells[key]
else:
bg = None
if is_first_row:
# First row sets column widths for table-layout:fixed
if bg is not None:
html += f"<td style='{w_sty};background:{bg}'></td>"
else:
html += f"<td style='{w_sty}'></td>"
else:
# Subsequent rows inherit widths, only need bg
if bg is not None:
html += f"<td style='background:{bg}'></td>"
else:
html += "<td></td>"
# Right column: annotation label
if has_right:
right_text = self._right_labels.get(row_idx, "")
if is_first_row:
html += "<td style='width:50px;font-size:9px;"
else:
html += "<td style='font-size:9px;"
html += f"padding-left:3px;'>{right_text}</td>"
html += "</tr>"
is_first_row = False
# X-axis tick labels
if self._x_tick_values:
tick_map = {}
for x_val in self._x_tick_values:
col = self._x_to_col(x_val)
if 0 <= col < self.cols:
tick_map[col] = f"{x_val:g}"
html += "<tr><td></td>"
for col_idx in range(self.cols):
if col_idx in tick_map:
html += "<td style='font-size:10px;"
html += "text-align:center;'>"
html += f"{tick_map[col_idx]}</td>"
else:
html += "<td></td>"
if has_right:
html += "<td></td>"
html += "</tr>"
# X-axis title
if self._x_axis_title is not None:
html += "<tr><td></td>"
html += f"<td colspan='{self.cols}' style='font-size:10px;"
html += "text-align:center;padding-top:2px;'>"
html += f"{self._x_axis_title}</td>"
if has_right:
html += "<td></td>"
html += "</tr>"
html += "</table>"
return html