gpt4 book ai didi

javascript - 使用 D3.js 将 SVG 元素附加到 HTML 表格单元格

转载 作者:行者123 更新时间:2023-11-28 06:40:37 26 4
gpt4 key购买 nike

我正在尝试使用 D3 根据这些单元格中的值将 SVG 圆圈附加到表格内的单元格。

这就是我想要得到的:

        .canvasBackground {
background-color: white
}

.table {
border-collapse: collapse;
border: #d0d4d5 solid 1px;
border-spacing: 0px;
font: normal 11px Georgia, 'Times New Roman', Times, serif;
letter-spacing: 1px;
line-height: 14px;
padding: 5px;
width: 100%
}

.headerStyle {
vertical-align: middle;
}

.headerRowStyle {
background-color: #fff;
border-bottom: 3px solid #ccc;
color: #4078a9;
font-size: 14px;
height: 48px;
line-height: 14px;
padding: 10px 5px 5px 5px
}

.headerCellStyle {
border-left: 1px solid #d0d4d5;

}

.tableBodyStyle {
text-align: left;
vertical-align: middle
}

.tableRowStyle {
background-color: #fff;
border-bottom: 1px solid #d0d4d5;
color: #565656;
padding: 5px 5px
}

.tableCellStyle {
border: 1px solid #d0d4d5;

}
<body>
<div class="canvasBackground">
<div class="tables">
<table id="sample" class="table display">
<thead class="headerStyle">
<tr class="headerRowStyle">
<th>Title</th>
<th>ID</th>
<th>Name</th>
<th>Color 1</th>
<th>Color 2</th>
</tr>
</thead>
<tbody class="tableBodyStyle">
<tr class="tableRowStyle">
<td class="tableCellStyle">Title 1</td>
<td class="tableCellStyle">00001</td>
<td class="tableCellStyle">Rena</td>
<td class="tableCellStyle">
<svg width="50" height="50">
<g>
<circle cx="28" cy="25" r="20" style="fill: rgb(244, 248, 94);"></circle>
<text dy="30" dx="24" style="fill: rgb(86, 86, 86);">Y</text>
</g>
</svg>
</td>
<td class="tableCellStyle">
<svg width="50" height="50">
<g>
<circle cx="28" cy="25" r="20" style="fill: rgb(122, 162, 92);"></circle>
<text dy="30" dx="24" style="fill: rgb(255, 255, 255);">G</text>
</g>
</svg>
</td>
</tr>
<tr class="tableRowStyle">
<td class="tableCellStyle">Title 2</td>
<td class="tableCellStyle">00002</td>
<td class="tableCellStyle">Elsa</td>
<td class="tableCellStyle">
<svg width="50" height="50">
<g>
<circle cx="28" cy="25" r="20" style="fill: rgb(122, 162, 92);"></circle>
<text dy="30" dx="24" style="fill: rgb(255, 255, 255);">G</text>
</g>
</svg>
</td>
<td class="tableCellStyle">
<svg width="50" height="50">
<g>
<circle cx="28" cy="25" r="20" style="fill: rgb(216, 75, 42);"></circle>
<text dy="30" dx="24" style="fill: rgb(255, 255, 255);">R</text>
</g>
</svg>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>

这是我到目前为止所得到的:

function evalColor(d) {
if (d == "Green" | d == "Yellow" | d == "Red") {
return createSVG(d);
}
if (d != "Green" | d != "Yellow" | d != "Red") {
return d;
}
}

function evalText(d) {
if (d == "Green" | d == "Yellow" | d == "Red") {
console.log(d);
}
else if (d != "Green" | d != "Yellow" | d != "Red") {
return d;
}
}

function createTable() {
var dataSet = [{
"Title": "Title 1",
"ID": "00001",
"Name": "Rena",
"Color 1": "Yellow",
"Color 2": "Green"
}, {
"Title": "Title 2",
"ID": "00002",
"Name": "Elsa",
"Color 1": "Green",
"Color 2": "Red"
}, ];

var div = d3.select('.tables');

// append a table to the div
var table = div.append("table")
.attr({
id: "sample",
class: 'table'
})
.classed("display", true);

// append a header to the table
var thead = table.append("thead")
.attr({
class: 'headerStyle'
});

// append a body to the table
var tbody = table.append("tbody")
.attr({
class: 'tableBodyStyle'
});

// append a row to the header
var theadRow = thead.append("tr")
.attr({
class: 'headerRowStyle'
});

// return a selection of cell elements in the header row
// attribute (join) data to the selection
// update (enter) the selection with nodes that have data
// append the cell elements to the header row
// return the text string for each item in the data array
theadRow.selectAll("th")
.data(d3.keys(dataSet[0]))
.enter()
.append("th")
.text(function(d) {
return d;
});

// table body rows
var tableBodyRows = tbody.selectAll("tr")
.data(dataSet)
.enter()
.append("tr")
.attr({
class: 'tableRowStyle'
});

//table body row cells
tableBodyRows.selectAll("td")
.data(function(d) {
return d3.values(d);
})
.enter()
.append("td")
.append(function(d) {
return createSVG(d);
})
.text(function(d) {
return evalText(d);
});
}

function createSVG(d) {

function colorPicker(value) {
if (value == "Green") {
return "#7aa25c";
}
else if (value == "Yellow") {
return "#f4f85e";
}
else if (value == "Red") {
return "#d84b2a";
}
}

function colorFill(value) {
if (value == "Green") {
return "#fff";
}
else if (value == "Yellow") {
return "#565656";
}
else if (value == "Red") {
return "#fff";
}
}

function letterChoice(value) {
if (value == "Green") {
return "G";
}
else if (value == "Yellow") {
return "Y";
}
else if (value == "Red") {
return "R";
}
}

var w = 50;
var h = 50;

var kpi = document.createElement("div");

var svg = d3.select(kpi).append("svg")
.attr({
width: w,
height: h
});


var elem = svg.selectAll("div")
.data([d]);

var elemEnter = elem.enter()
.append("g");

elemEnter.append("circle")
.attr({
cx: 28,
cy: 25,
r: 20
})
.style("fill", function(d) {
return colorPicker(d);
});

elemEnter.append("text")
.style("fill", function(d) {
return colorFill(d);
})
.attr("dy", 55)
.attr("dx", 45)
.text(function(d) {
return letterChoice(d);
});

return kpi;
}

createTable();
        .canvasBackground {
background-color: white
}

.table {
border-collapse: collapse;
border: #d0d4d5 solid 1px;
border-spacing: 0px;
font: normal 11px Georgia, 'Times New Roman', Times, serif;
letter-spacing: 1px;
line-height: 14px;
padding: 5px;
width: 100%
}

.headerStyle {
vertical-align: middle;
}

.headerRowStyle {
background-color: #fff;
border-bottom: 3px solid #ccc;
color: #4078a9;
font-size: 14px;
height: 48px;
line-height: 14px;
padding: 10px 5px 5px 5px
}

.headerCellStyle {
border-left: 1px solid #d0d4d5;

}

.tableBodyStyle {
text-align: left;
vertical-align: middle
}

.tableRowStyle {
background-color: #fff;
border-bottom: 1px solid #d0d4d5;
color: #565656;
padding: 5px 5px
}

.tableCellStyle {
border: 1px solid #d0d4d5;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<body>
<div class="canvasBackground">
<div class="tables"></div>
</body>

本质上,我想使用.append附加方法 <td>带有文本的元素(如果文本不包含颜色,例如“红色”、“黄色”或“绿色”)。如果文本确实包含这些颜色,我想使用相同的 .append附加子 svg cirlce 元素的方法。但是当我使用.text时方法,我的 svg 元素消失了。

有什么想法吗?

最佳答案

颠倒调用顺序,并在其中添加一个过滤器:

  //table body row cells
tableBodyRows.selectAll("td")
.data(function(d) {
return d3.values(d);
})
.enter()
.append("td")
.text(function(d) {
return evalText(d);
})
// only make the SVG for those text that have a color
.filter(function(d){
return (d === "Green" ||
d === "Yellow" ||
d === "Red");
})
.append(function(d) {
return createSVG(d);
});

工作代码:

<!DOCTYPE html>
<html>

<head>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<style>
.canvasBackground {
background-color: white
}

.table {
border-collapse: collapse;
border: #d0d4d5 solid 1px;
border-spacing: 0px;
font: normal 11px Georgia, 'Times New Roman', Times, serif;
letter-spacing: 1px;
line-height: 14px;
padding: 5px;
width: 100%
}

.headerStyle {
vertical-align: middle;
}

.headerRowStyle {
background-color: #fff;
border-bottom: 3px solid #ccc;
color: #4078a9;
font-size: 14px;
height: 48px;
line-height: 14px;
padding: 10px 5px 5px 5px
}

.headerCellStyle {
border-left: 1px solid #d0d4d5;
}

.tableBodyStyle {
text-align: left;
vertical-align: middle
}

.tableRowStyle {
background-color: #fff;
border-bottom: 1px solid #d0d4d5;
color: #565656;
padding: 5px 5px
}

.tableCellStyle {
border: 1px solid #d0d4d5;
}
</style>
</head>

<body>
<div class="canvasBackground">
<div class="tables"></div>
</div>

<script>
function evalColor(d) {
if (d == "Green" | d == "Yellow" | d == "Red") {
return createSVG(d);
}
if (d != "Green" | d != "Yellow" | d != "Red") {
return d;
}
}

function evalText(d) {
if (d == "Green" | d == "Yellow" | d == "Red") {
console.log(d);
} else if (d != "Green" | d != "Yellow" | d != "Red") {
return d;
}
}

function createTable() {
var dataSet = [{
"Title": "Title 1",
"ID": "00001",
"Name": "Rena",
"Color 1": "Yellow",
"Color 2": "Green"
}, {
"Title": "Title 2",
"ID": "00002",
"Name": "Elsa",
"Color 1": "Green",
"Color 2": "Red"
}, ];

var div = d3.select('.tables');

// append a table to the div
var table = div.append("table")
.attr({
id: "sample",
class: 'table'
})
.classed("display", true);

// append a header to the table
var thead = table.append("thead")
.attr({
class: 'headerStyle'
});

// append a body to the table
var tbody = table.append("tbody")
.attr({
class: 'tableBodyStyle'
});

// append a row to the header
var theadRow = thead.append("tr")
.attr({
class: 'headerRowStyle'
});

// return a selection of cell elements in the header row
// attribute (join) data to the selection
// update (enter) the selection with nodes that have data
// append the cell elements to the header row
// return the text string for each item in the data array
theadRow.selectAll("th")
.data(d3.keys(dataSet[0]))
.enter()
.append("th")
.text(function(d) {
return d;
});

// table body rows
var tableBodyRows = tbody.selectAll("tr")
.data(dataSet)
.enter()
.append("tr")
.attr({
class: 'tableRowStyle'
});

//table body row cells
tableBodyRows.selectAll("td")
.data(function(d) {
return d3.values(d);
})
.enter()
.append("td")
.text(function(d) {
return evalText(d);
})
.filter(function(d){
return (d === "Green" ||
d === "Yellow" ||
d === "Red");
})
.append(function(d) {
return createSVG(d);
});

}

function createSVG(d) {

function colorPicker(value) {
if (value == "Green") {
return "#7aa25c";
} else if (value == "Yellow") {
return "#f4f85e";
} else if (value == "Red") {
return "#d84b2a";
}
}

function colorFill(value) {
if (value == "Green") {
return "#fff";
} else if (value == "Yellow") {
return "#565656";
} else if (value == "Red") {
return "#fff";
}
}

function letterChoice(value) {
if (value == "Green") {
return "G";
} else if (value == "Yellow") {
return "Y";
} else if (value == "Red") {
return "R";
}
}

var w = 50;
var h = 50;

var kpi = document.createElement("div");

var svg = d3.select(kpi).append("svg")
.attr({
width: w,
height: h
});

var elem = svg.selectAll("div")
.data([d]);

var elemEnter = elem.enter()
.append("g");

elemEnter.append("circle")
.attr({
cx: 28,
cy: 25,
r: 20
})
.style("fill", colorPicker);

elemEnter.append("text")
.style("fill", colorFill)
.attr("dy", 30)
.attr("dx", 25)
.text(letterChoice);

return kpi;
}

createTable();
</script>
</body>

</html>

关于javascript - 使用 D3.js 将 SVG 元素附加到 HTML 表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com