Man*_*ppa 7 html javascript c3.js
我需要帮助在html中生成换行符.
使用Javascript
var x = "jun";
var y = "2015";
var calculate= x + "<br>" + y;
Run Code Online (Sandbox Code Playgroud)
Html返回如下
<div>jan <br> 2015</div>
Run Code Online (Sandbox Code Playgroud)
预期结果:我需要在html中换行,但不应该呈现<br>标记.
更新:我想要的是"jan"在第一行和下一行"2015"
我在c3图表x值中使用这些值.
提前致谢.
您的问题声明是有点unprecise:您正在使用C3.js将产生SVG元素.
所以返回的标记实际上是<tspan dx="0" dy=".71em" x="0">0<br>2014</tspan>.
C3将使用textContenttspan 的属性来附加函数返回的文本内容.
正如在其他问题中已经说过的那样,您无法在<tspan>元素中添加换行符.
所以解决方案是在同一个<text>元素中有效地创建一个新的tspan .
不幸的是,没有办法获得这些精确的元素,除非通过循环所有其他tspans,所以这可能听起来像一个真正的黑客,但这里是一个脚本,将做你想要的...
// 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)
};
Run Code Online (Sandbox Code Playgroud)
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)
};Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1305 次 |
| 最近记录: |