Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
How do you plot scatter graph with chart.js

I am trying to figure out how to plot a scatter graph with chart.js the data I am trying to plot is randomly generated where dates is a list of datetime.now() objects and prices is a list of float numbers

dates = [datetime.now().strftime("%Y-%m-%d")  for i in range(10)]
var dates_ = {{dates|tojson}}
var prices_ = {{prices|tojson}}
function parse_data(dates , prices)
{
    console.log(dates.length , prices.length)
    var tmp = [];
    for (let i = 0; i < dates.length; i++)
    {
        var x;
        var y;
        tmp[i] = {x: dates.at(i), y: prices.at(i)};
    }
    return tmp;
}
var data_ = parse_data(dates_, prices_);

var chart = new Chart(ctx, {
    type: 'scatter',
    data: {
        datasets: [{
        lablel: "some_data",
        data: data_}]
});

converting the dates to float numbers does the job but it shows the dates as number and it is not that good looking

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda