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

时间:2015-12-04 06:13:08

标签: javascript html c3.js

我需要帮助在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

先谢谢。

4 个答案:

答案 0 :(得分:5)

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

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

C3将使用tspan的textContent属性附加您的函数返回的文本内容。
正如other questions中所述,您无法在<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)
};

&#13;
&#13;
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)
};
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要为每个新行创建新的<tspan>。原因是<tspan>通常位于<text>元素中。哪个有一定的坐标。  你不能去&#34;反对&#34;那些坐标。

您唯一能做的就是创建另一个具有不同坐标集的<tspan>并根据需要定位。

  

因为SVG文本元素使用与SVG图形元素的其余部分相同的渲染方法进行渲染,所以使用相同的坐标   系统,转换,...​​等也适用。

     

SVG文本元素在初始时呈现第一个字符   目前的文字位置。

     

此位置由&#39; x&#39;定义。并且&#39; y&#39; SVG文本的属性   元件。

     

<text>元素,文本和字体属性以及当前文本中   可以使用绝对或相对坐标值调整位置   通过包含<tspan>元素。

答案 2 :(得分:-1)

也许这就是你所需要的:

var calculate= '<pre>' + x + '\n' + y + '</pre>';

你必须将整个事物放在预标签中,\ n被解释为换行符。

关于:http://www.sitepoint.com/everything-need-know-html-pre-element/

CodePen上的演示:http://codepen.io/mizech/pen/gPOrEz

答案 3 :(得分:-1)

我已尝试在abc.html中使用以下代码并正常工作。请尝试。

    <!DOCTYPE html>
<html>
<body>
<div id="demo"></div>
<script>
var x = "jun";
var y = "2015";
document.getElementById("demo").innerHTML =x+"<br>"+y;
</script>
</body>
</html>