从数据库中循环svg矩形

时间:2017-12-21 14:39:13

标签: python css mysql svg

我正在使用python从数据库中读取svg矩形,我不知道这是否正确,因为我似乎是硬编码,这是因为我希望每个矩形在我的css样式表中改变颜色。是否有更好的方法来调用这些矩形而不是使用ifs和elifs,因为如果我有100个矩形,那么更好的方法是什么。我添加了样式表以及

var photoId = db.photo.insertOne({name : "xyz.png"}).insertedId
db.example.insertOne({
    photoId : photoId.str //<-- Changed objectId to string
})

css stylesheet

for row in c: 

 box_x = ((row[3]-row[1])/2 + row[1] - 0.25)
 box_y = ((row[4]-row[2])/2 + row[2] - 0.25)
 move1 = box_y * 2
 try1 =  row[1] * 2  

 if row[0] == 1:
    print('<rect id= rectangle1 class= "rectangles" onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 2:
    print('<rect  id="rectangle2" class= "rectangles"   onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 3:
    print('<rect  id="rectangle3" class= "rectangles"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 4:
    print('<rect id="rectangle4" class= "rectangles" onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 5:
    print('<rect id="rectangle5" class= "rectangles"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 6:
    print('<rect id="rectangle6" class= "rectangles"  onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 elif row[0] == 7:
    print('<rect id="rectangle7" class= "rectangles"  onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')
 else:
    print('<rect id="rectangle8" class= "rectangles"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onmousemove="myFunction3()" x=',row[1],' y=',row[2],' width=',row[3]-row[1],' height=',row[4]-row[2],'><title>Owned by',row[6],'</title></rect>')

1 个答案:

答案 0 :(得分:0)

据我所知,代码看起来你应该能够做到:

for i, row in enumerate(c): 
    box_x = ((row[3]-row[1])/2 + row[1] - 0.25)
    box_y = ((row[4]-row[2])/2 + row[2] - 0.25)
    move1 = box_y * 2
    try1 =  row[1] * 2  

    print('<rect id="rectangle{0}" class="rectangles" x="{1}" y="{2}" width="{3}" height="{4}" onmousemove="myFunction3()"><title>Owned by {5}</title></rect>'.format(i, row[1], row[2], row[3] - row[1], row[4] - row[2], row[6]))