在Chart.js(2.7.2)标签部分添加换行符

时间:2018-12-10 19:32:17

标签: javascript chart.js

我正在用我的API中的数据填充标签数组。

var chartData = {
    labels: [],
    datasets: [{
        yAxisID: 'Seconds',
        label: 'Min',
        backgroundColor: 'rgb(139, 0, 0)',
        data: []
    }, {
        label: 'Avg',
        backgroundColor: 'rgb(34,139,34)',
        data: []
    }, {
        label: 'Max',
        backgroundColor: 'rgb(255,165,0)',
        data: []
    }]
};

这是我的JS:

initJobHealth()
    .then(function(data){
        data.map(function (job) {
            var avg = job.job_duration_avg / 1000;
            avg = avg.toFixed(2);
            var max = job.job_duration_max / 1000;
            max = max.toFixed(2);
            var min = job.job_duration_min / 1000;
            min = min.toFixed(2);
            var sampleS = job.jobs_sample_size;
            var br = document.createElement("BR");
            chartData.labels.push(job.job_type + br + 'Sample Size ' + sampleS );
            chartData.datasets[0].data.push(min);
            chartData.datasets[1].data.push(avg);
            chartData.datasets[2].data.push(max);
        })
    })
    .then(initChart)

目标是在job.job_type和Sample Size字符串之间的chartData.labels.push部分中插入一个换行符。当我在两者之间插入var br并查看本地主机上的输出而不是换行时,我得到了  [object HTMLBRElement]

This is where I want sampleS to be

Data for job.job_type && SampleS = jobs_sample_size

将var br添加到chartData.labels.push部分时的视图 view when adding var br

在将代码编辑到chartData.labels.push(job.job_type + br.outerHTML +'Sample Size'+ sampleS)时查看; View when editing code

1 个答案:

答案 0 :(得分:0)

这是因为您将对象添加到字符串中。一个解决方案可能是

chartData.labels.push(job.job_type + '<br>Sample Size ' + sampleS );

chartData.labels.push(job.job_type + br.outerHTML + 'Sample Size ' + sampleS );