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

255
Vistas
How to adjust spaces between points in chart js?

I am trying to make a line chart by using chart.js. But I facing a problem. I want to control the space between points, but the chart.js makes it equal.

Here is my code:

<canvas id="myChart"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js" integrity="sha512-OD9Gn6cAUQezuljS6411uRFr84pkrCtw23Hl5TYzmGyD0YcunJIPSBDzrV8EeCiFxGWWvtJOfVo5pOgB++Jsag==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>
var ctx = document.getElementById("myChart");

var points = [1,1,9,9.3];
var Dates = ["Dec 29th", "jan 10th", "Feb 21st", "Feb 25th"];

var myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: Dates,
    datasets: [
      {
        label: "My chart",
        data: points,
        backgroundColor: "black",
        borderColor: "blue",
        borderWidth: 3,
        fill: false,
        lineTension: 0.4,
      }
    ]
  },
  
  options: {
    legend: {display: false},
    scales: {
      yAxes:[{
        ticks: {
            min:1,
            max: 9.9,
                  
            callback: function(value, index, values) {
                return '$' + value;
            }
        }
      }],
      
    },
     elements: {
        point:{
            radius: 0
        }
    }
  }
});
</script>

Here is the result of my code: enter image description here

But I want the big space in middle like this: enter image description here

How can I make the big space between point 3 and point4, please?

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

0

Can you use time on the x axis?

Below is an example

// "+" before (new Date(2021,11,29)) return miliseconds from 1970 year
const dataA = [ 
    {x:+new Date(2021,11,29), y:1},         // { x: 1640732400000, y: 1}, 
    {x:+new Date(2022,0,10), y:1},          // { x: 1641769200000, y: 1}, 
    {x:+new Date(2022,1,21), y:9},          // { x: 1645398000000, y: 9}, 
    {x:+new Date(2022,1,25), y:9.3},        // { x: 1645743600000, y: 9.3}
]; 

const cfgChart = {
    type: 'line',
    data: {
        datasets: [
            {
                backgroundColor: '#74adf7',
                borderColor: '#74adf7',
                data: dataA,
                label: 'A',
                lineTension: 0.4,
            },
        ],
    },
    options: {
        animation: false,
        parsing: false,
        interaction: {
            mode: 'index',
            axis: 'x',
            intersect: false,
        },
        scales: {
            x: {
                type: 'time',
            },
        },
    },
};

const chart = new Chart(document.querySelector('#chart').getContext('2d'), cfgChart);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-adapter-luxon/1.1.0/chartjs-adapter-luxon.min.js"></script>

<div>
  <canvas id="chart"></canvas>
</div>

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