gpt4 book ai didi

javascript - C3 中的换行符通过 JavaScript 生成 SVG 图表

转载 作者:太空狗 更新时间:2023-10-29 14:17:29 24 4
gpt4 key购买 nike

我需要有关在 html 中生成换行符的帮助。

Javascript

var x = "jun";
var y = "2015";

var calculate= x + "<br>" + y;

Html 返回如下

<div>jan <br> 2015</div>

预期结果:我需要在 html 中换行但不应该呈现 <br>标签。

更新:我想要的是第一行的“jan”和下一行的“2015”

我在 c3 图表 x 值中使用这些值。

JSFIDDLE

提前致谢。

最佳答案

您的问题陈述有点不精确:您正在使用将生成 svg 元素的 C3.js。

所以返回的标记实际上是 <tspan dx="0" dy=".71em" x="0">0&lt;br&gt;2014</tspan> .

C3 将使用 textContent tspan 的属性附加函数返回的文本内容。
正如在 other questions 中已经说过的那样, 您不能在 <tspan> 中添加换行符元素。

因此解决方案是在同一 <text> 中有效地在另一个 tspan 的下方创建一个新的 tspan元素。

不幸的是,除了遍历所有其他 tspans 之外,没有办法获得这些精确的元素,所以这听起来像是一个真正的 hack,但这里有一个脚本可以做你想做的事......

// get our svg doc
var svg = document.querySelector('svg');
// get our tspans element
var tspans = svg.querySelectorAll('tspan');
// transform it to an array so the clones don't add to the list
var ts = Array.prototype.slice.call(tspans);

for(var i = 0; i<ts.length; i++){
// get the content
var cont = ts[i].textContent.split('\n');
// that wasn't the good one...
if(cont.length<2) continue;
// create a clone
var clone = ts[i].cloneNode(1);
// set the text to the new line
clone.textContent = cont[1];
// space it a litlle bit more
clone.setAttribute('dy', '0.9em')
// set the good text to the upperline
ts[i].textContent = cont[0];
// append our clone
ts[i].parentNode.insertBefore(clone, ts[i].nextSibling)
};

var chart = c3.generate({
data: {
json: [{
date: '2014-01-01',
upload: 200,
download: 200,
total: 400
}, {
date: '2014-01-02',
upload: 100,
download: 300,
total: 400
}, {
date: '2014-02-01',
upload: 300,
download: 200,
total: 500
}, {
date: '2014-02-02',
upload: 400,
download: 100,
total: 500
}],
keys: {
x: 'date',
value: ['upload', 'download']
}
},
axis: {
x: {
type: 'timeseries',
tick: {
format: function (x) {
if (x.getDate() === 1) {
return x.getMonth() + '\n' + x.getFullYear();

}
}
}
}
}
});
// get our svg doc
var svg = document.querySelector('svg');
// get our tspans element
var tspans = svg.querySelectorAll('tspan');
// transform it to an array so the clones don't add to the list
var ts = Array.prototype.slice.call(tspans);

for(var i = 0; i<ts.length; i++){
// get the content
var cont = ts[i].textContent.split('\n');
// that wasn't the good one...
if(cont.length<2) continue;
// create a clone
var clone = ts[i].cloneNode(1);
// set the text to the new line
clone.textContent = cont[1];
// space it a litlle bit more
clone.setAttribute('dy', '0.9em')
// set the good text to the upperline
ts[i].textContent = cont[0];
// append our clone
ts[i].parentNode.insertBefore(clone, ts[i].nextSibling)
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://rawgit.com/masayuki0812/c3/master/c3.js"></script>
<link href="https://rawgit.com/masayuki0812/c3/master/c3.css" rel="stylesheet">
<div id="chart"></div>

关于javascript - C3 中的换行符通过 JavaScript 生成 SVG 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34082025/

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