Зацикливание SVG прямоугольников из базы данных

0

Я читаю прямоугольники svg из базы данных с помощью python, я не знаю, правильно ли это, поскольку мне кажется, что это жесткое кодирование, потому что я хочу, чтобы каждый прямоугольник менял цвета в моей таблице стилей CSS. Есть ли лучший способ называть эти прямоугольники, а не использовать ifs и elifs, потому что, если у меня есть 100 прямоугольников, что лучше для этого. Я также добавил свою таблицу стилей

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>')

CSS-таблица стилей

.rectangles{
        fill:       #ff3333;
        stroke:     #000000;
        stroke-width:   0.1; 

}
#rectangle1:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #A52A2A;
}  
#rectangle2:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #A52A2A;
}
#rectangle3:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #006400;
}
#rectangle4:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #000000;
}
#rectangle5:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #006400;
}
#rectangle6:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #000000;
}
#rectangle7:hover{
        stroke:     #FF7F00
        stroke-width:   0.1;
        fill:       #FFFFFF;
}
#rectangle8:hover{
        stroke:     #FF7F00;
        stroke-width:   0.1;
        fill:       #FFFFFF;
}
Теги:
svg

1 ответ

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]))

Ещё вопросы

Сообщество Overcoder
Наверх
Меню