Chart.JS:如何使锐利的线条变得平滑的曲线

Par*_*oxi 5 javascript charts chart.js

您好,我是图表新手,这是我的 ChartJS 图表,它当前正在工作,但它以锐线显示,我希望在此图表上使其曲线平滑。有任何想法吗?

function statistics(data) {
    if ($('#stats-currency').length > 0) {

        if (typeof(stats_currency) !== 'undefined') {
            stats_currency.destroy();
        }
        if (typeof(data) == 'undefined') {
            var currency = $('select[name="currency"]').val();
            $.get(admin_url + 'home/stats_currency/' + currency, function(response) {
                stats_currency = new Chart($('#stats-currency'), {
                    type: 'line',
                    data: response,
                    options: {
                        responsive:true,
                        scales: {
                            yAxes: [{
                              ticks: {
                                beginAtZero: true,
                            }
                        }]
                    },
                },
            });
            }, 'json');
        } else {
            stats_currency = new Chart($('#stats-currency'), {
                type: 'line',
                data: data,
                options: {
                    responsive: true,
                    scales: {
                        yAxes: [{
                          ticks: {
                            beginAtZero: true,
                        }
                    }]
                },
            },
        });
        }
Run Code Online (Sandbox Code Playgroud)

umi*_*der 16

lineTension这可以通过需要在数据集上定义的选项来完成。选择一个低于 1 的值。

datasets: [{
  ... 
  lineTension: 0.8
}]
Run Code Online (Sandbox Code Playgroud)

然而,默认情况下,您应该已经看到弯曲的平滑线,因为根据Chart.js 文档,默认值为0.4

lineTension:线的贝塞尔曲线张力。设置为 0 以绘制直线。

请注意,如果该steppedLine值设置为 , 以外的任何值falselineTension将被忽略。

  • 截至 2021 年 8 月,默认的“lineTension”值现在似乎为 0,因此,如果您从 v2 升级到 v3 并想要相同的线条样式,则必须专门将“lineTension”设置为“0.4”。 (5认同)