def anim7(svg): H, W = svg.get_size() cx, cy = W // 2, H // 2 # define gradient color color_id = 'myGradient' defs = svg.draw(draw_any('defs')) grad = svg.draw_node(defs, draw_any('radialGradient ', id=f'{color_id}')) stop_dict = {} stop_dict['offset'] = '2%' stop_dict['stop-color'] = 'gold' svg.draw_node(grad, draw_any('stop', None, **stop_dict)) stop_dict['offset'] = '90%' stop_dict['stop-color'] = 'red' svg.draw_node(grad, draw_any('stop', None, **stop_dict)) g = svg.draw(draw_tag('g')) svg.set_node(g, 'opacity', '1.0') r = 8 pt = [cx, cy] # color = 'red' color = f"url('#{color_id}')" draw_ball_movin(svg, g, r, W, H, start_pt=pt, step_x=-2, step_y=3, N=500, color=color, draw_path_line=True)
def _prepare_svg(self): # style css define self.svg.draw(draw_any('title ', text=self.dataDict['name'])) styleNode = self.svg.draw(draw_any('style ', type="text/css")) dictStyle = {'fill': "black", 'font-family': "sans-serif", 'font-size': '28px'} self.svg.add_child(styleNode, self.svg.new_node(add_style('.big', get_styles(dictStyle)))) dictStyle = {'fill': "black", 'font-family': "sans-serif", 'font-size': '20px'} self.svg.add_child(styleNode, self.svg.new_node(add_style('.small', get_styles(dictStyle)))) dictStyle = {'fill': "black", 'font-family': "cursive", 'font-size': '20px'} # cursive fantasy monospace self.svg.add_child(styleNode, self.svg.new_node(add_style('.bodyText', get_styles(dictStyle))))
def drawPointsLineGraphic12(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 N = 10 defs = svg.draw(draw_any('defs')) # for cliping the outside drawing clip = svg.draw_node(defs, draw_any('clipPath', id='clip')) svg.draw_node(clip, draw_any('rect', x="0", y="0", width=f"{W}", height=f"{H}")) group = svg.draw(draw_any('g')) svg.set_node(group, 'clip-path', 'url(#clip)') offset = 6 # pts = random_points((50, 2), min=offset, max=W-offset) pts = uniform_random_points(W, H, N, N, x_offset=W//N/8, y_offset=H//N/8) # pts = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]])*40 # x, y = translation_pts(pts, (cx, cy)) # pts = combine_xy(x, y) # pts = center_cordinates(pts, (cx, cy)) # print('pts.shape=', pts, pts.shape) vor = Voronoi(pts) # print('vor.vertices=', vor.vertices, vor.vertices.shape) # print('vor.ridge_points=', vor.ridge_points, vor.ridge_points.shape) # print('vor.regions=', vor.regions, len(vor.regions)) # print('vor.ridge_vertices=', vor.ridge_vertices, len(vor.ridge_vertices)) # print('vor.point_region=', vor.point_region, len(vor.point_region)) f_segments, i_segments = get_voronoi_lines(vor) f_segments = [i.flatten() for i in f_segments] i_segments = [i.flatten() for i in i_segments] # print('f_segments=', f_segments) # print('i_segments=', i_segments) # draw Voronoi ridges--lines for i in f_segments: drawlinePoints(svg, [i], node=group, color='green') for i in i_segments: drawlinePoints(svg, [i], node=group, color='green', dash='None') draw_voronoi_regions(svg, group, vor) # color regions drawPointsCircle(svg, pts, node=group, r=1, color='black') drawPointsCircle(svg, vor.vertices, node=group, r=0.8, color='green')
def imgSvgElement(): file = gImageOutputPath + r'\image.svg' svg = SVGFileV2(file, W=200, H=200, border=False) W, H = svg.get_size() styleDict = {} styleDict['x'] = '0' styleDict['y'] = '0' # 'green' styleDict['width'] = 100 styleDict['height'] = 100 # styleDict['href'] = 'https://www.python.org/static/img/[email protected]' styleDict['href'] = r'../images/download.png' # styleDict['preserveAspectRatio'] = 'none' # styleDict['crossorigin'] = '' svg.draw(draw_any('image ', **styleDict)) styleDict['x'] = '20' styleDict['y'] = '20' # 'green' svg.draw(draw_any('image ', **styleDict))
def drawDataFrame(svg): H, W = svg.get_size() cx, cy = W // 2, H // 2 svg.set_title('draw Dataframe data') g = svg.draw(draw_any('g', opacity=1.0)) # anyNode = svg.draw_node(g, draw_any('test','222', a=10, b="4",c='red',xml='www.ss')) # svg.draw_node(g, draw_any('test','hello')) df = getDataSet() plotTable(svg, g, df, TableContentStyle.TEXT)
def drawPointsLineGraphic13(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 r = 55 cy -= 20 g = svg.draw(draw_any('g')) svg.draw_node(g, draw_ring(cx, cy, radius=r)) pts_12 = get_regular_ngons(r, 12) pts_12 = translation_pts(pts_12, (cx, cy), True) # print('pts_12, pts_12.shape', pts_12, pts_12.shape) drawPointsCircle(svg, pts_12, node=g, r=1, color='green') drawPloygonNode(svg, pts_12, g, color='blue') pts_6 = get_regular_ngons(r, 6) pts_6 = translation_pts(pts_6, (cx, cy), True) # print('pts_6, pts_6.shape', pts_6, pts_6.shape) drawPointsCircle(svg, pts_6, node=g, r=1, color='red') drawPloygonNode(svg, pts_6, g, color='green') # draw lines linePts = np.array([]).reshape(0, 2) for pt in pts_6: linePts = np.vstack((linePts, pt)) linePts = np.vstack((linePts, [cx, cy])) linePts = np.vstack((linePts, [cx, cy])) linePts = np.vstack((linePts, pts_12[3])) # print('linePts=', linePts) drawlinePointsContinus(svg, linePts, stroke_width=0.5, color='black') svg.draw_node(g, draw_text(52, 15, "Pi Day of 2022", fontsize='12px')) svg.draw_node(g, draw_text(25, 145, "Liu Hui's π algorithm", fontsize='10px')) svg.draw_node(g, draw_text(15, 155, "S_2N = Chord*R*N, where N=6,12,24,48,...", fontsize='8px')) strs = [] y0 = 165 x0 = 2 r = 1 chord_len = r # chord length of hexagon equal to r for i in range(1, 11): N = np.power(2, i - 1) * 6 # s = area_regular_polygon(N=N) s, chord_len = chord_length(r=1, pre_chord_len=chord_len, N=N) # print('N, s, chord_len=', N, s, chord_len) x = f'S_{2*N}={s}' print(x) strs.append(x) if i % 2 == 0: svg.draw_node(g, draw_text(x0, y0, ','.join(strs), fontsize='7px')) y0 += 8 strs = []
def plotTable(svg, node, df, style=TableContentStyle.TEXT): W, H = svg.get_size() print('df.shape=', df.shape) indexs = df.index columns = df.columns print('indexs=', indexs) print('columns=', columns) print(df) row, col = df.shape offsetX = 5 offsetY = 5 colWidth = (W - 2 * offsetX) / (col + 1) # 45 rowHeight = (H - 2 * offsetY) / (row + 1) # 25 styleDict = {} styleDict['stroke'] = 'black' styleDict['stroke-width'] = '0.5' svg.draw_node(node, add_style('line', get_styles(styleDict))) anyDict = {} # anyDict['stroke'] = 'black' # anyDict['stroke-width'] = 0.5 for i in range(row + 2): x1 = offsetX y1 = offsetY + i * rowHeight x2 = x1 + (col + 1) * colWidth y2 = y1 anyDict['x1'] = x1 anyDict['y1'] = y1 anyDict['x2'] = x2 anyDict['y2'] = y2 svg.draw_node(node, draw_any('line', **anyDict)) for i in range(col + 2): x1 = offsetX + i * colWidth y1 = offsetY x2 = x1 y2 = y1 + (row + 1) * rowHeight anyDict['x1'] = x1 anyDict['y1'] = y1 anyDict['x2'] = x2 anyDict['y2'] = y2 svg.draw_node(node, draw_any('line', **anyDict)) styleDict = {} # styleDict['fill'] = 'black' styleDict['font-family'] = 'Consolas' # styleDict['font-size'] = '22px' styleDict['dominant-baseline'] = "middle" styleDict['text-anchor'] = "middle" svg.draw_node(node, add_style('text', get_styles(styleDict))) anyDict = {} anyDict['font-size'] = '14px' anyDict['fill'] = 'red' """draw index text""" for i, index in enumerate(indexs): # print(index) x1 = offsetX y1 = offsetY + (i + 1) * rowHeight x = x1 + colWidth / 2 y = y1 + rowHeight / 2 anyDict['x'] = x anyDict['y'] = y svg.draw_node(node, draw_any('text', index, **anyDict)) """draw column text""" for i, column in enumerate(columns): # print(column) x1 = offsetX + (i + 1) * colWidth y1 = offsetY x = x1 + colWidth / 2 y = y1 + rowHeight / 2 anyDict['x'] = x anyDict['y'] = y svg.draw_node(node, draw_any('text', column, **anyDict)) """draw content text""" for i in range(row): for j in range(col): x = offsetX + (j + 1) * colWidth y = offsetY + (i + 1) * rowHeight g = svg.draw_node( node, draw_any('g', opacity=1.0, id=str(i) + '_' + str(j))) if style == TableContentStyle.TEXT: anyDict['x'] = x + colWidth / 2 anyDict['y'] = y + rowHeight / 2 anyDict['font-size'] = '12px' anyDict['fill'] = 'black' svg.draw_node(g, draw_any('text', df.iloc[i, j], **anyDict)) elif style == TableContentStyle.COLOR: anyDict['x'] = x anyDict['y'] = y anyDict['width'] = colWidth anyDict['height'] = rowHeight maxV = np.max(df.values) minV = np.min(df.values) scalar = (df.iloc[i, j] - minV) / (maxV - minV) anyDict['fill'] = color_fader('#C0392B', '#3498DB', scalar) # 'b' svg.draw_node(g, draw_any('rect', df.iloc[i, j], **anyDict)) elif style == TableContentStyle.SMILE: r = rowHeight // 2 - 1 x = x + colWidth / 2 - r y = y + rowHeight / 2 - r r = clip_float(r) x = clip_float(x) y = clip_float(y) drawSmileSVGNode(svg, g, radius=r, offsetX=x, offsetY=y) # svg.draw_node(node, draw_any('rect', df.iloc[i,j], **anyDict)) # svg.draw_node(node, draw_any('circle', cx=x,cy=y,r=2,fill='red')) else: print('Not implement yet.')
def drawAllTypePath(svg): H, W = svg.get_size() cx, cy = W // 2, H // 2 svg.set_title('draw path') g = svg.draw(draw_any('g', opacity=1.0)) anyDict = {} anyDict['stroke'] = 'black' anyDict['fill'] = 'transparent' anyDict['d'] = 'M 10 10 C 20 20, 40 20, 50 10' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 70 10 C 70 20, 110 20, 110 10' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 130 10 C 120 20, 180 20, 170 10' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 10 30 C 20 50, 40 50, 50 30' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 70 30 C 70 50, 110 50, 110 30' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'MM 130 30 C 120 50, 180 50, 170 30' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 10 50 C 20 80, 40 80, 50 50' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 70 50 C 70 80, 110 80, 110 50' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 130 50 C 120 80, 180 80, 170 50' svg.draw_node(g, draw_any('path', **anyDict)) # anyDict['d'] = 'M 10 10 10 60 60 30' # svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 10 315 \ L 110 215 \ A 30 50 0 0 1 162.55 162.45 \ L 172.55 152.45 \ A 30 50 -45 0 1 215.1 109.9 \ L 315 10' anyDict['fill'] = 'green' anyDict['stroke-width'] = '2' svg.draw_node(g, draw_any('path', **anyDict))
def drawAny(svg): H, W = svg.get_size() cx, cy = W // 2, H // 2 svg.set_title('you can draw anything by using draw_any()') g = svg.draw(draw_any('g', opacity=1.0)) # anyNode = svg.draw_node(g, draw_any('test','222', a=10, b="4",c='red',xml='www.ss')) svg.draw_node(g, draw_any('test', 'hello')) anyDict = {} anyDict['test'] = 1 anyDict['xml'] = 'www.ggg' anyDict['a'] = 'aaaaa anything else' anyDict['b'] = 'red black xxxxxxxxxxxxxxxxx anything you want' svg.draw_node(g, draw_any('test2', **anyDict)) svg.draw_node(g, draw_any('hello', **anyDict)) svg.draw_node(g, draw_any('anything', **anyDict)) for i in range(20): anyDict = {} anyDict['cx'] = cx anyDict['cy'] = cy anyDict['r'] = '5' anyDict['stroke'] = '#80ff00' anyDict['stroke-width'] = '2' anyDict['fill'] = 'none' circle = svg.draw_node(g, draw_any('circle', **anyDict)) # 'from' is a key word of python for import libs, but here last resort change parameter # from(attribute of animate element for svg) to 'From' to avoid conflict. svg.draw_node( circle, draw_any('animate', fill='freeze', attributeName='r', From="5", to="80", dur="4s", begin=str(i), repeatCount="indefinite")) anyDict = {} anyDict['fill'] = 'freeze' anyDict['attributeName'] = 'fill' anyDict['from'] = '#ff0000' anyDict['to'] = '#00ff40' anyDict['dur'] = '6s' anyDict['begin'] = '0s' anyDict['repeatCount'] = 'indefinite' # svg.draw_node(circle, draw_any('animate', **anyDict)) anyDict['attributeName'] = 'stroke-width' anyDict['values'] = '1;2;3;2;1' anyDict.pop("from", None) anyDict.pop("to", None) svg.draw_node(circle, draw_any('animate', **anyDict)) anyDict['attributeName'] = 'stroke' anyDict['from'] = '#80ff00' anyDict['to'] = '#0000ff' anyDict['begin'] = '1s' anyDict.pop("values", None) svg.draw_node(circle, draw_any('animate', **anyDict))
def _draw_(self): self._prepare_svg() W, H = self.svg.get_size() # ----------------header------------------- # dictLink = {} dictLink["{{{}}}".format(self.svg._xlink) + 'href'] = self.dataDict['wiki'] aLink = self.svg.draw(draw_any(tag='a')) self.svg.set_node_dict(aLink, dictLink) itemW = 110 itemH = 150 styleImg = {} styleImg['width'] = str(itemW) styleImg['height'] = str(itemH) styleImg['x'] = str(0) styleImg['y'] = str(0) styleImg['href'] = self.dataDict['photo'] if 0: # self.svg.draw(draw_any('image', **styleImg)) self.svg.add_child(aLink, self.svg.new_node(draw_any('image', **styleImg))) else: self.draw_portrait(aLink, styleImg['href'], itemW, itemH, (0, 0)) y0 = 30 x0 = itemW + 5 node = self.svg.draw(draw_any(tag='text', x=x0, y=y0, text=self.dataDict['name'])) self.svg.set_node(node, 'class', 'big') node = self.svg.draw(draw_any(tag='text', x=x0, y=y0 + 30, text=self.dataDict['name_cn'])) self.svg.set_node(node, 'class', 'big') date = '(' + self.dataDict['date_birth'] + ' - ' + self.dataDict['date_death'] + ')' node = self.svg.draw(draw_any(tag='text', x=x0, y=y0 + 60, text=date)) self.svg.set_node(node, 'class', 'small') node = self.svg.draw(draw_any(tag='text', x=x0, y=y0 + 90, text=self.dataDict['profile'])) self.svg.set_node(node, 'class', 'small') # ----------------split line--------------- # self.svg.draw(draw_rect(x=0, y=itemH, width=W, height=0.01 * H, stroke_width=0.2, color='#FCC64A')) # ----------------body--------------------- # y0 = itemH + 10 nodeTextBody = self.svg.draw(draw_any(tag='text')) self.svg.set_node(nodeTextBody, 'class', 'bodyText') dictStyle = {'dy': "0.2em", 'x': "10"} for i, txts in enumerate(self.dataDict['quotes'], start=1): # dictStyle['y'] = str(y0) for k, lineText in enumerate(txts, start=1): if k == 1: s = str(i) + '. ' + lineText else: s = lineText y0 += 30 dictStyle['y'] = str(y0) line = self.svg.new_node(draw_any(tag='tspan', text=s, **dictStyle)) self.svg.add_child(nodeTextBody, line) y0 += 20