score:1

in order to do this with chart.js you will need to use the alternate data format for line charts where you define an x and y value for each point. you also need to configure both the x and y axis (scales) to be linear scales. lastly, you will probably need to define more points along the bottom part of the curve (the 2nd equation) to ensure the heart shape looks good.

here is an example configuration that applies all the things i mentioned above. i also want to mention that i played with the axis min/max values to get a good looking heart.

var ctx = document.getelementbyid("mychart");
var mychart = new chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: 'equation plotted',
      data: [{
        x: 0,
        y: 2
      }, {
        x: 1,
        y: 3
      }, {
        x: 2,
        y: 2
      }, {
        x: 1.02,
        y: 0.4
      }, {
        x: 0,
        y: -1
      }],
      backgroundcolor: [
        'rgba(123, 83, 252, 0.8)',
        'rgba(123, 83, 252, 0.8)',
        'rgba(123, 83, 252, 0.8)',
        'rgba(123, 83, 252, 0.8)',
        'rgba(123, 83, 252, 0.8)',
        'rgba(123, 83, 252, 0.8)'
      ],
      bordercolor: [
        'rgba(33, 232, 234, 1)',
        'rgba(33, 232, 234, 1)',
        'rgba(33, 232, 234, 1)',
        'rgba(33, 232, 234, 1)',
        'rgba(33, 232, 234, 1)',
        'rgba(33, 232, 234, 1)'
      ],
      borderwidth: 1
    }],
  },
  options: {
    scales: {
      xaxes: [{
        type: 'linear',
        position: 'bottom',
        ticks: {
          min: -1,
          max: 8,
          stepsize: 1,
          fixedstepsize: 1,
        }
      }],
      yaxes: [{
        ticks: {
          min: -2,
          max: 4,
          stepsize: 1,
          fixedstepsize: 1,
        }
      }]
    }
  }
});

you can see this in action in this codepen example.


Related Query