Olivier.B
Olivier.B

Reputation: 109

Char.js Interpolation linear

i'm trying to draw functions with Chart.js I have a problem with my draw let's see : here

This is the 'x' function and the line isn't straight
I don't know if it's a problem of resolution or of interpolation
So here is my chart :

var scatterChart = new Chart(ctx, {
      type: 'line',
      data: {
          datasets: [{
              borderWidth:2,
              pointRadius :0,
              borderColor: 'rgba(0, 0, 255, 1)',
              label: 'Scatter Dataset',
              data: JSON.parse(data),
              fill: false,
              lineTension: 0,
              cubicInterpolationMode: 'linear'

          }]
      },
      options: {
          scales: {
              xAxes: [{
                  type: 'linear',
                  position: 'bottom'
              }]
          },
          pan: {
              enabled: true,
              mode: 'xy'
          },
          zoom: {
              enabled: true,
              mode: 'xy',
          },
          responsive: true,
          maintainAspectRatio: true,
      }
  });

Thx guys

Upvotes: 1

Views: 6784

Answers (1)

Brett Pontarelli
Brett Pontarelli

Reputation: 1728

The problem is how chart.js is drawing the small line segments between data points. Here is a fiddle that animates a full opacity line over top of a lighter line. The second line is growing at a step=1 while the lighter line is nice and straight [{x:-100,y:-100},{x:100,y:100}]. If you change to step=10 you will see the line fill in straight.

In the code below (or in the fiddle) you can play around with changing the borderWidth, borderColor, and opacity. I tried adding borderCapStyle: 'round' and borderJoinStyle: 'round', but neither seemed to have much effect.

var ctx = document.getElementById("test").getContext("2d");
var i = -100;
var data = [{x: i, y: i}];
var scatterChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      borderWidth: 2,
      pointRadius: 0,
      borderColor: 'rgba(0, 0, 255, 1)',
      label: 'Scatter Dataset 1',
      data: data,
      fill: false,
      lineTension: 0,
      cubicInterpolationMode: 'linear'
    }, {
      borderWidth: 2,
      pointRadius: 0,
      borderColor: 'rgba(0, 0, 255, 0.4)',
      label: 'Scatter Dataset 2',
      data: [{x:-100,y:-100},{x:100,y:100}],
      fill: false,
      lineTension: 0,
      cubicInterpolationMode: 'linear'
    }]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'linear',
        position: 'bottom'
      }]
    },
    pan: {
      enabled: true,
      mode: 'xy'
    },
    zoom: {
      enabled: true,
      mode: 'xy',
    },
    responsive: true,
    maintainAspectRatio: true,
  }
});
var step = 1;
var intervalId = setInterval(function() {
  i += step;
  if (i <= 100) {
    scatterChart.data.datasets[0].data.push({x:i,y:i});
    scatterChart.update();
  } else {
    clearInterval(intervalId);
  }
}, 200);

and

<canvas id="test" width="400" height="300"></canvas>

Upvotes: 2

Related Questions