Chart.js : straight lines instead of curves

前端 未结 4 1305
不思量自难忘°
不思量自难忘° 2021-01-30 09:55

I\'m using Chart.JS to plot a dataset,

However I got a smooth effect !

Here is the curve I\'ve got :

Here is my code :



        
相关标签:
4条回答
  • 2021-01-30 10:20

    You can use lineTension option to set the desired curve. Set 0 for straight lines. You can give a number between 0-1

    data: {
        datasets: [{
            lineTension: 0
        }]
    }
    
    0 讨论(0)
  • 2021-01-30 10:20

    I think it's been updated to lineTension. Check the docs.

    0 讨论(0)
  • 2021-01-30 10:29

    Solution for Version 1 (old charts version)

    According to documentation on chartjs.org

    you can set the 'bezierCurve' in options and pass it in when you create the chart.

    bezierCurve: false
    

    eg:

    var options = {
        //Boolean - Whether the line is curved between points
        bezierCurve : false
    };
    var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData, options);
    

    Update for version 2

    According to updated documentation for Line Configuration in global options

    Name        Type    Default Description
    tension     Number  0.4     Default bezier curve tension. Set to 0 for no bezier curves.
    

    eg:

    var options = {
        elements: {
            line: {
                tension: 0
            }
        }
    };
    

    And also directly in the Dataset Structure by setting lineTension to 0 (zero).

    Property        Type    Usage
    lineTension     Number  Bezier curve tension of the line. Set to 0 to draw straightlines. 
                            This option is ignored if monotone cubic interpolation is used. 
                            Note This was renamed from 'tension' but the old name still works.
    

    An example data object using these attributes is shown below.

    var lineChartData = {
        labels: labels,
        datasets: [
            {
                label: "My First dataset",
                lineTension: 0,           
                data: data,
            }
        ]
    };
    
    0 讨论(0)
  • 2021-01-30 10:42

    I have used lineTension to set the smoothness of the curve.

    From the docs: lineTension receives a number, Bezier curve tension of the line. Set to 0 to draw straight lines. This option is ignored if monotone cubic interpolation is used.

    Just make sure to test with different values how smooth you want the line.

    For example:

    var data = {
      labels: ["Jan", "Feb", "Mar"],
      datasets: [{
          label: "Label 1",
          lineTension: 0.2
        }, {
          label: "Stock B",
          lineTension: 0.2
        }
    
      ]
    };
    
    0 讨论(0)
提交回复
热议问题