def drawPathContinuPoints(svg, pts, strokeWidth=0.5, color=None): path = 'M ' for i in range(len(pts)): x, y = pts[i] path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=strokeWidth, color=color or random_color()))
def circleInflation(svg, x, y, r, color=None, fromR=0, toR=0, durS=5, begin=None): x, y, r = clip_float(x), clip_float(y), clip_float(r) fromR, toR = clip_float(fromR), clip_float(toR) id, circle = createCircle(svg, x, y, r, color) animateDict = {} # animateDict['xlink:href'] = f'#{id}' animateDict["{{{}}}".format(svg.xlink) + 'href'] = f'#{id}' animateDict['id'] = 'ani_' + id + '_' + rand_str(2) animateDict['fill'] = 'freeze' animateDict['attributeName'] = 'r' animateDict['from'] = str(fromR) # '10' animateDict['to'] = str(toR) # '50' animateDict['dur'] = str(durS) # str(random.randint(0,durS)) #'5' animateDict['begin'] = begin or str(random.randint( 0, 5)) + 's' # '0s' #'click' # animateDict["repeatCount"] = "indefinite" # "5" addNodeAnitmation(svg, circle, animateDict) return id, circle
def my_path_potrace(paths, N=2): print('len(path)=', len(paths)) # Iterate over path curves for curve in paths: start_pt = curve.start_point # pts = curve.decomposition_points # print('pts=', pts) path = 'M %f %f ' % (clip_float(start_pt.x, N), clip_float(start_pt.y, N)) # print("start_point =", start_pt) for segment in curve: # print('end_pt=', end_pt, segment.is_corner) if segment.is_corner: c = segment.c # print('c=', c, 'x=', c.x, 'y=', c.y) end_pt = segment.end_point end = f'L {clip_float(end_pt.x, N)} {clip_float(end_pt.y, N)}' seg = f'L {clip_float(c.x, N)} {clip_float(c.y, N)} ' + end else: c1 = segment.c1 c2 = segment.c2 # print('c1=', c1, 'x=', c1.x, 'y=', c1.y) # print('c2=', c2, 'x=', c2.x, 'y=', c2.y) end_pt = segment.end_point end = f'{clip_float(end_pt.x, N)} {clip_float(end_pt.y, N)}' seg = f'C {clip_float(c1.x, N)},{clip_float(c1.y, N)} {clip_float(c2.x, N)},{clip_float(c2.y, N)} ' + end path += seg path += 'z' yield path
def draw_heart_curve(): file = gImageOutputPath + r'\heartPath.svg' H, W = 100, 100 svg = SVGFileV2(file, W, H) offsetX = W // 2 offsetY = H // 2 svg.draw(add_style_path(stroke='red', stroke_width=0.5, fill='red')) N = 100 r = 30 path = 'M %.1f %.1f L ' % (0 + offsetX, heartFuc(0, r) + offsetY ) # start point x = np.linspace(-r, r, N) y = heartFuc(x, r=r) # Up part points of heart curve, set sqrt value positive xr = np.flip(x) # Down part points of heart curve, set sqrt value negative yr = heartFuc(xr, r=r, up=False) x = np.concatenate((x, xr), axis=0) y = np.concatenate( (y, yr), axis=0 ) * -1 # *-1 svg coordinate system different from standard cod system # print('x=', x) # print('y=', y) x = x + offsetX y = y + offsetY for i, j in zip(x, y): path = path + ' ' + str(clip_float(i)) + ' ' + str(clip_float(j)) svg.draw(draw_only_path(path)) svg.close()
def drawOneFuncSVG(svg, ptX, ptY, N=10, color=None): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.5, color=color or random_color()))
def drawPloygon(svg, pts, color=None, stroke_color=None): # print('pts',pts) points = [] for i in pts: points.append(str(clip_float(i[0])) + ',' + str(clip_float(i[1])) + ' ') points = ''.join(points) svg.draw(draw_polygon(points, stroke_width=0.5, color=color or random_color(), stroke_color=stroke_color))
def drawlinePoints(svg, pts, node=None, stroke_width=0.5, color=None, stroke_widths=None, dash=None): for i, pt in enumerate(pts): x1, y1, x2, y2 = pt x1 = clip_float(x1) y1 = clip_float(y1) x2 = clip_float(x2) y2 = clip_float(y2) if stroke_widths: stroke_width = stroke_widths[i] svg.draw_node(node, draw_line(x1, y1, x2, y2, stroke_width=stroke_width, color=color or random_color(), stroke_dasharray=dash))
def drawPointsCircleFadeColor(svg, pts, r=2): c = rainbow_colors(N=len(pts)) # print(c,type(c)) for i, pt in enumerate(pts): x = clip_float(pt[0]) y = clip_float(pt[1]) color = c[i] svg.draw(draw_circle(x, y, radius=r, color=color))
def drawRandomCirclePath(svg): W, H = svg.get_size() styles = ['circles', 'circle points', 'circle points random'] onlyPath = False times = 100 r = 2 offsetX = W // 2 offsetY = H // 2 style = styles[1] if style == styles[0]: for _ in range(times): r = r + random.random() * 8 ptX, ptY = getCirclePtsSVG(svg, r=r, N=200, offsetX=offsetX, offsetY=offsetY, noise=False, onlyPath=onlyPath) drawOnePathcSVG(svg, ptX, ptY, onlyPath=onlyPath) elif style == styles[1]: times = 10 for _ in range(times): r = r + random.random() * 18 ptX, ptY = getCirclePtsSVG(svg, r=r, N=20, offsetX=offsetX, offsetY=offsetY, noise=False, onlyPath=onlyPath) ptNumber = int(5 * r) # ptX = np.random.choice(ptX, ptNumber) ptX = ptX.reshape((len(ptX), 1)) ptY = ptY.reshape((len(ptY), 1)) pts = np.concatenate((ptX, ptY), axis=1) # print(ptX.shape, pts.shape) ptsIndex = np.random.randint(len(pts), size=ptNumber) # print('ptsIndex=', ptsIndex, len(pts)) pts = pts[ptsIndex, :] for i in pts: # print('i=', i) # ra = 0.5 ra = np.random.random() * (3 - 0.2) + 0.2 ra = clip_float(ra) x = clip_float(i[0]) y = clip_float(i[1]) svg.draw(draw_circle(x, y, radius=ra, color=random_color()))
def drawlinePointsContinus(svg, pts, stroke_width=0.5, color=None, stroke_widths=None): n = len(pts) for i in range(n - 1): (x1, y1), (x2, y2) = pts[i], pts[i + 1] x1 = clip_float(x1) y1 = clip_float(y1) x2 = clip_float(x2) y2 = clip_float(y2) if stroke_widths: stroke_width = stroke_widths[i] svg.draw(draw_line(x1, y1, x2, y2, stroke_width=stroke_width, color=color or random_color()))
def drawOnePathcSVG(svg, ptX, ptY, width=1, onlyPath=True): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) path = path + 'z' if onlyPath: svg.draw((path)) else: svg.draw(draw_path(path, stroke_width=width, color=random_color()))
def drawRandomWalkPath(svg): W, H = svg.get_size() times = 1000 cx, cy = W // 2, H // 2 x = cx y = cy path = 'M %.1f %.1f L ' % (x, y) for _ in range(times): x, y = random_walk(x, y, 1, step=2) path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2))
def drawlinePointsContinusRainbow(svg, pts, stroke_width=0.5, color=None, colors=None, stroke_widths=None): # c = rainbow_colors(N=len(pts)) for i in range(len(pts) - 1): (x1, y1), (x2, y2) = pts[i], pts[i + 1] x1 = clip_float(x1) y1 = clip_float(y1) x2 = clip_float(x2) y2 = clip_float(y2) if stroke_widths: stroke_width = stroke_widths[i] if colors: color = colors[i] # color = c[i] svg.draw(draw_line(x1, y1, x2, y2, stroke_width=stroke_width, color=color))
def drawRandomNumbersPath(svg): W, H = svg.get_size() times = 100 N = 500 xW = 5 for _ in range(times): path = 'M 0 0 L ' ptX = np.arange(N) + xW ptY = randomContinueNumbers(N=N) # print(ptX) # print(ptY) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2, color=random_color()))
def draw_points_svg(svg, ptX, ptY, close=False, stroke=0.6, color=None, fillColor='transparent'): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L' % (x, y) for x, y in zip(ptX[1:], ptY[1:]): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) if close: path += ' Z' # color = color or random_color_hsv() return svg.draw( draw_path(path, stroke_width=stroke, color=color, fill_color=fillColor))
def drawNodeShape(svg, node): H, W = svg.get_size() cx, cy = W // 2, H // 2 r = 45 angle = np.pi / 5 x0 = [cx, cx + 2 * r, cx + r] y0 = [cy, cy, cy - r * np.tan(angle)] # print('x0 ,y0=', x0 ,y0) times = 8 theta = 0 rainbowC = rainbow_colors(times) for i in range(times): theta = i * (2 * np.pi / times) x, y = rotation_pts_xy_point(x0, y0, (cx, cy), theta) color = random_color_hsv() # color = random_color() drawPloygonNode(svg, node=node, pts=[(x[0], y[0]), (x[1], y[1]), (x[2], y[2])], color=color) # drawPloygonNode(svg, node=node, pts=[(x[0],y[0]), (x[1],y[1]), (x[2],y[2])], color=rainbowC[i]) # --------- draw text-------------- # dis = get_distance([x0[0], y0[0]], [x0[1], y0[1]]) x_t = x0[0] + dis * 1.5 / 3 y_t = y0[0] - (dis * 1.5 / 3) * np.tan(angle) * 0.6 / 2 x_t = clip_float(x_t, 2) y_t = clip_float(y_t, 2) # print('x_t, y_t=', x_t, y_t) txt_child = svg.draw_node( node, draw_text(x_t, y_t, 'Love', color=reverse_hex(color))) strTmp = 'rotate({},{},{})'.format(i * 360 / times, x0[0], y0[0]) svg.set_node(txt_child, 'transform', strTmp) svg.set_node(txt_child, 'text-anchor', 'middle') svg.set_node(txt_child, 'dominant-baseline', 'central')
def svg_path2(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 N = 400 xoffset = 0.5 x = np.linspace(xoffset, 10 - xoffset, N) * 20 y = funcSin(x / 2) * 20 for i in range(4): offsetY = i * H // 4 + 20 + 5 ptx, pty = translation_pts_xy(x, y, (0, offsetY)) stroke = clip_float(random.random() * 2) draw_points_svg(svg, ptx, pty, stroke=stroke)
def drawPointsCircle(svg, pts=[], node=None, r=2, color='black'): for pt in pts: x = clip_float(pt[0]) y = clip_float(pt[1]) svg.draw_node(node, draw_circle(x, y, radius=r, color=color))
def drawPloygonNode(svg, pts, node=None, color=None): # print('pts',pts) points = [str(clip_float(i[0])) + ',' + str(clip_float(i[1])) + ' ' for i in pts] points = ''.join(points) svg.draw_node(node, draw_polygon(points, stroke_width=0.5, color=color or random_color()))
def drawPointsRect_colors(svg, pts, r, colors): for i, pt in enumerate(pts): x = clip_float(pt[0]) y = clip_float(pt[1]) svg.draw(draw_rect(x, y, r, r, color=colors[i]))
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 drawPointsCircle_colors(svg, pts, r, colors): for i, pt in enumerate(pts): x = clip_float(pt[0]) y = clip_float(pt[1]) svg.draw(draw_circle(x, y, radius=r, color=colors[i]))