我正在尝试为我的svg图表创建g元素,但是它们都堆叠在一起,尽管我试图在它们上面设置一个变换距离。
我尝试过的事情:使用"setAttribute“为每个元素添加一个转换转换距离。这似乎没有起到任何作用。
let tick = document.createElementNS(svg, "g");
tick.setAttributeNS(svg, "transform", "translate("+ translateDistance +",0)");
tick.setAttributeNS(svg, "class", "tick");
let line = document.createElementNS(svg, "line");
line.setAttributeNS(svg, "class", "y-axis-zero-line axis-line dash gray");
line.setAttributeNS(svg, "x1", "-13");
line.setAttributeNS(svg, "y1", "0");
line.setAttributeNS(svg, "x2", "-13");
line.setAttributeNS(svg, "y2", "240");
tick.appendChild(line);
let textDay = document.createElementNS(svg, "text");
textDay.setAttributeNS(svg, "class", "label-day");
textDay.setAttributeNS(svg, "dy", "0.71em");
textDay.setAttributeNS(svg, "y", "-28");
textDay.setAttributeNS(svg, "x", "0");
let textDayNode = document.createTextNode(formattedLabels[i][0]);
textDay.appendChild(textDayNode);
tick.appendChild(textDay);
let textTime = document.createElementNS(svg, "text");
textDay.setAttributeNS(svg, "class", "label-time");
textDay.setAttributeNS(svg, "dy", "0.71em");
textDay.setAttributeNS(svg, "y", "-15");
textDay.setAttributeNS(svg, "x", "0");
let textTimeNode = document.createTextNode(formattedLabels[i][1]);
textTime.appendChild(textTimeNode);
tick.appendChild(textTime);
totalXAxis.appendChild(tick);
下面是一个可编辑的示例:https://codepen.io/Finches/pen/RwwQOPY
预期的结果是刻度在图表的顶部被隔开。实际结果是刻度都堆叠在图表原点附近,就好像转换没有做任何事情一样。
有什么帮助吗?
发布于 2019-11-05 06:38:32
您需要更改线路
tick.setAttributeNS(svg, "transform", "translate("+ translateDistance +",0)");
至
tick.style.transform = "translate("+translateDistance+"px)";
注意:你会注意到你的距离计算的错误,但至少事情是可以翻译的。
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate
发布于 2019-11-05 10:23:03
只有在创建新元素时才使用名称空间(const svg = "http://www.w3.org/2000/svg";
):
let tick = document.createElementNS(svg, "g");
在设置属性时,不使用名称空间。您可以改用null
:
tick.setAttributeNS(null, "transform", "translate("+ translateDistance +",0)");
const labels = [
['Mon', '7 AM'],
['Tue', '7 AM'],
['Tue', '8 AM'],
['Tue', '9 AM'],
['Tue', '10 AM'],
['Tue', '11 AM'],
['Tue', '12 PM']
]
const xAxis = document.getElementById('total-x-ticks');
function createTotalLabels(labels, totalChartWidth, totalXAxis) {
const hoursInterval = 1;
const svg = "http://www.w3.org/2000/svg";
let spacing = totalChartWidth / labels.length;
let currentDay = '';
const formattedLabels = labels.map((label, i) => {
if (i % hoursInterval === 0) {
if (label[0] !== currentDay) {
currentDay = label[0];
return [label[0], label[1]];
}
return ['', label[1]];
}
return ['', ''];
});
for (let i = 0; i < formattedLabels.length; i++) {
let translateDistance;
if (i === 0) {
translateDistance = 15;
} else {
translateDistance = i * spacing + 15;
}
if (formattedLabels[i][1].length && i !== 0) {
if (formattedLabels[i][0].length) {
let tick = document.createElementNS(svg, "g");
tick.setAttributeNS(null, "transform", "translate("+ translateDistance +",0)");
tick.setAttributeNS(null, "class", "tick");
let line = document.createElementNS(svg, "line");
line.setAttributeNS(null, "class", "y-axis-zero-line axis-line dash gray");
line.setAttributeNS(null, "x1", "-13");
line.setAttributeNS(null, "y1", "0");
line.setAttributeNS(null, "x2", "-13");
line.setAttributeNS(null, "y2", "240");
tick.appendChild(line);
let textDay = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-day");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-28");
textDay.setAttributeNS(null, "x", "0");
let textDayNode = document.createTextNode(formattedLabels[i][0]);
textDay.appendChild(textDayNode);
tick.appendChild(textDay);
let textTime = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-time");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-15");
textDay.setAttributeNS(null, "x", "0");
let textTimeNode = document.createTextNode(formattedLabels[i][1]);
textTime.appendChild(textTimeNode);
tick.appendChild(textTime);
totalXAxis.appendChild(tick);
} else {
let tick = document.createElementNS(svg, "g");
tick.setAttributeNS(null, "transform", "translate("+ translateDistance +",0)");
tick.setAttributeNS(null, "class", "tick");
let line = document.createElementNS(svg, "line");
line.setAttributeNS(null, "class", "y-axis-zero-line axis-line dash gray");
line.setAttributeNS(null, "x1", "-13");
line.setAttributeNS(null, "y1", "0");
line.setAttributeNS(null, "x2", "-13");
line.setAttributeNS(null, "y2", "240");
tick.appendChild(line);
let textDay = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-day");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-28");
textDay.setAttributeNS(null, "x", "0");
let textDayNode = document.createTextNode(formattedLabels[i][0]);
textDay.appendChild(textDayNode);
tick.appendChild(textDay);
let textTime = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-time gray");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-15");
textDay.setAttributeNS(null, "x", "0");
let textTimeNode = document.createTextNode(formattedLabels[i][1]);
textTime.appendChild(textTimeNode);
tick.appendChild(textTime);
totalXAxis.appendChild(tick);
}
} else {
let tick = document.createElementNS(svg, "g");
tick.setAttributeNS(null, "transform", "translate("+ translateDistance +",0)");
tick.setAttributeNS(null, "class", "tick");
let textDay = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-day");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-28");
textDay.setAttributeNS(null, "x", "0");
let textDayNode = document.createTextNode(formattedLabels[i][0]);
textDay.appendChild(textDayNode);
tick.appendChild(textDay);
let textTime = document.createElementNS(svg, "text");
textDay.setAttributeNS(null, "class", "label-time");
textDay.setAttributeNS(null, "dy", "0.71em");
textDay.setAttributeNS(null, "y", "-15");
textDay.setAttributeNS(null, "x", "0");
let textTimeNode = document.createTextNode(formattedLabels[i][1]);
textTime.appendChild(textTimeNode);
tick.appendChild(textTime);
totalXAxis.appendChild(tick);
}
}
}
createTotalLabels(labels, 500, xAxis);
.container {
width: 500px;
}
svg .bar {
fill: #4a93ff;
}
svg .axis {
font: 11px 'Solis';
}
svg .axis path,
svg .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
svg .x.axis path {
display: none;
}
svg .axis-line {
stroke-width: 1px;
}
svg .axis-line.dash.gray {
stroke-dasharray: 4;
stroke-width: 1;
}
svg .tick {
opacity: 1;
}
svg .tick text {
text-anchor: middle;
}
svg .tick line.gray,
svg line.gray {
stroke: rgba(0, 0, 0, 0.2);
}
svg .label-day,
svg .y-tick {
font-weight: bold;
}
svg .label-time {
font-weight: normal;
}
svg .label-time.gray {
fill: #878787;
}
<div class="container">
<svg id="total-chart" height="285" width="100%">
<defs>
<linearGradient id="total-gradient" gradientTransform="rotate(90)">
<stop offset="0" stop-color="rgba(76, 150, 254, 1.00)" />
<stop offset="0.5" stop-color="rgba(76, 150, 254, 0.50)" />
<stop offset="1" stop-color="rgba(209, 225, 250, 0.1)" />
</linearGradient>
</defs>
<g transform="translate(32,40)">
<g class="y axis" id="total-y-ticks">
<line
class="y-axis-zero-line axis-line"
id="y-axis-zero-line-total"
x1="0"
y1="0"
x2="0"
y2="240"
/>
</g>
<g>
<polygon id="total-chart-polygon" fill="url('#total-gradient')" />
</g>
<g class="x axis"
id="total-x-ticks"
transform="translate(0,0)"
>
<line
class="x-axis-zero-line axis-line"
id="x-axis-zero-line-total"
x1="0"
y1="240"
x2="600"
y2="240"
/>
</g>
</g>
</svg>
</div>
https://stackoverflow.com/questions/58705458
复制相似问题