score:1

Accepted answer

the scatter chart uses a line chart internally but changes it so the showline property becomes false and both axis are set to linear.

so you can just set the showline property to false in your dataset and use a line chart.

score:2

you can define your x-axis as follows

x: {      
  type: 'time',
  time: {
    parser: 'yyyy-m-d',
    unit: 'day',
    displayformats: {
      day: 'd mmm yyyy'
    },
    tooltipformat: 'd mmm yyyy'
  }
}

further information can be found at the chart.js documentation here and in the chart.js samples here.

note that i use chartjs-adapter-moment together with moment to make this work. the formats for parsing and displaying time values as desired can be found here.

please take a look at your amended code and see how it works.

var test = [
  { x: "2022-1-8", y: 950 },
  { x: "2022-1-9", y: 1100 },
  { x: "2022-1-10", y: 990 },
  { x: "2022-1-12", y: 1250 },
  { x: "2022-1-13", y: 1050 }
];

var chart = new chart('chart-line', {
  type: 'scatter',
  data: {
    datasets: [{
      data: test,
      label: 'buys',
      bordercolor: "#3e95cd"
    }]
  },
  options: {
    responsive: false,
    scales: {
      x: {      
        type: 'time',
        time: {
          parser: 'yyyy-m-d',
          unit: 'day',
          displayformats: {
            day: 'd mmm yyyy'
          },
          tooltipformat: 'd mmm yyyy'
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.7.0/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@1.0.0"></script>
<canvas id="chart-line" height="200"></canvas>


Related Query