-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg_builder.py
163 lines (129 loc) · 4.59 KB
/
svg_builder.py
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
import py_svg as psvg
def num2per(num):
try:
per = str(num) + '%'
except TypeError:
per = '0%'
return per
class Chromatogram(list):
def __init__(self, name, w, h):
super().__init__()
self.append('<svg viewBox="0 0 1000 250" xmlns="http://www.w3.org/2000/svg">')
self.keys = []
self.col1 = {'fill': (0, 0, 0),
'stroke': (0, 0, 0),
'fill-opacity': 0,
'stroke-width': 7,
'stroke-dash-array': None}
self.col2 = {'fill': (70, 225, 170),
'stroke': (70, 225, 170),
'fill-opacity': .15,
'stroke-width': 2,
'stroke-dash-array': None}
self.col3 = {'fill': (70, 70, 225),
'stroke': (70, 70, 225),
'fill-opacity': .4,
'stroke-width': 2,
'stroke-dash-array': None}
self.col4 = {'fill': (70, 225, 170),
'stroke': (70, 225, 170),
'fill-opacity': .5,
'stroke-width': 2,
'stroke-dash-array': None}
self.blck = {'fill': (0, 0, 0),
'stroke': (0, 0, 0),
'fill-opacity': 0,
'stroke-width': 4,
'stroke-dash-array': 10}
self.margin = 3
def add_cubic(self, x1, y1, body, settings=None):
if settings is None:
settings = self.col1
row = psvg.Cubic(x1, y1, body)
row.set_fill(settings['fill'])
row.set_stroke(settings['stroke'])
row.set_fill_opacity(settings['fill-opacity'])
row.set_stroke_width(settings['stroke-width'])
svg = row.construct()
self.append(svg)
def add_key(self, label, settings):
n = len(self.keys)
dy = (2 * n + 1) * self.margin
x = num2per(self.margin)
y = num2per(dy)
w = num2per(self.margin)
h = num2per(self.margin)
# Set Box
row = psvg.Rect(x, y, w, h)
row.set_fill(settings['fill'])
row.set_fill_opacity(settings['fill-opacity'])
svg = row.construct()
self.append(svg)
# Set Line
x1 = num2per(self.margin)
x2 = num2per(2 * self.margin)
if settings['fill-opacity'] == 0:
y1 = num2per(dy + (self.margin / 2))
else:
y1 = y
row = psvg.Line(x1, y1, x2, y1)
row.set_stroke(settings['stroke'])
row.set_stroke_width(settings['stroke-width'])
row.set_dash_array(settings['stroke-dash-array'])
svg = row.construct()
self.append(svg)
# Set Text
x = num2per(3 * self.margin)
y = num2per(dy + (self.margin / 2))
self.add_text(label, x, y, 'left')
self.keys.append(label)
def add_text(self, label, x, y, side):
row = psvg.Text(label, x, y)
row.set_font_family('IBM Plex Mono')
row.set_font_size(40)
row.set_font_weight(500)
row.set_dominant_baseline('central')
row.set_text_anchor(side)
svg = row.construct()
self.append(svg)
def add_line(self, x1, y1, x2, y2):
row = psvg.Line(x1, y1, x2, y2)
row.set_stroke(self.blck['stroke'])
row.set_stroke_width(self.blck['stroke-width'])
row.set_dash_array(self.blck['stroke-dash-array'])
svg = row.construct()
self.append(svg)
def save(self):
row = psvg.Rect(0, 0, '100%', '100%')
row.set_fill_opacity(0)
# row.set_stroke((10, 10, 15))
# row.set_stroke_width(5)
self.append(row.construct())
self.append('</svg>')
ans = '\n'.join(self)
return ans
class HTML(object):
def __init__(self):
super().__init__()
self.body = []
def add_section(self, drug, svg):
div = ['<div class="myDiv">',
'<h2>%s</h2>' % (drug,),
svg,
'</div>']
self.body.append('\n'.join(div))
def save(self):
htmlist = ['<!DOCTYPE html>',
'<html>',
'<head>',
'<style>',
'.myDiv {border: 0px outset red; background-color: lightblue; text-align: center;}',
'h2 {text-align: left;}',
'</style>',
'</head>',
'<body>']
for item in self.body:
htmlist.append(item)
htmlist.append('</body>')
htmlist.append('</html>')
return '\n'.join(htmlist)