最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fix the distance between horizontal points (x-axis) in chartJS - Stack Overflow

programmeradmin2浏览0评论

Problem: Hi all! i am using chart.js to draw the charts. chart is a dynamic sometimes shows 10 points and sometimes can be more than thousand i have applied a panel successfully so that my points can be shown at some distance to read them easily.

Right now i want to know if there is any option to set the distance between points in x-axis grid. right now it automatically adjust the points.

What i tried: i tried to do the stepsize and scalewidth by searching other stackoverflow answers but did not succeed. Any kind of help would be much appreciated.

P.S: i m using chart.js2

This is my chartjs dataset

var data = {
    labels: data.graph_date,
    datasets: [
        {
            label: 'Assay Values',
            fill: false,
            lineTension: 0,
            backgroundColor: "rgba(255, 0, 0,1)",
            borderColor: "rgba(255, 0, 0,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderWidth: 1,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(255, 0, 0,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(255, 0, 0,1)",
            pointHoverBorderColor: "rgba(255, 0, 0,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: data.assay_value,
            spanGaps: false
        },var options = {
    responsive: false,
    title: {
        display: true,
        position: "top",
        text: label,
        fontSize: 18,
        fontColor: "#111"
    },
    legend: {
        display: true,
        position: "bottom",
        labels: {
            fontColor: "#333",
            fontSize: 16
        }
    },
    tooltips: {
        enabled: true,
        mode: 'single',
        callbacks: {
            label: function(tooltipItems, data) {
                var multistringText = ['Assay Value: '+tooltipItems.yLabel];
                multistringText.push('Assigned Value: '+assigned_value[tooltipItems.index]);
                multistringText.push('Sample ID: '+sample_id[tooltipItems.index]);
                return multistringText;
            }
        }
    },
    scales:{
        yAxes:[{
            ticks:{
                min:graph_min
            }
        }],
        xAxes: [{
            gridLines: {
                display:false
            }
        }]
    }
};
if(new_chart[ctx.canvas.id] != null)
    new_chart[ctx.canvas.id].destroy();

new_chart[ctx.canvas.id] =new Chart(ctx, {
    type: 'line',
    data: data,
    options: options
});

In x-axis there is data like this

[19-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,22-Aug-2015,27-Aug-2015,29-Aug-2015,1-Sep-2015,2-Sep-2015,3-Sep-2015,]

in y-axis data is like this

[0.1,0.05,0.89,0.89,0.79,0.58,0.68,0.25,0.98]

Problem: Hi all! i am using chart.js to draw the charts. chart is a dynamic sometimes shows 10 points and sometimes can be more than thousand i have applied a panel successfully so that my points can be shown at some distance to read them easily.

Right now i want to know if there is any option to set the distance between points in x-axis grid. right now it automatically adjust the points.

What i tried: i tried to do the stepsize and scalewidth by searching other stackoverflow answers but did not succeed. Any kind of help would be much appreciated.

P.S: i m using chart.js2

This is my chartjs dataset

var data = {
    labels: data.graph_date,
    datasets: [
        {
            label: 'Assay Values',
            fill: false,
            lineTension: 0,
            backgroundColor: "rgba(255, 0, 0,1)",
            borderColor: "rgba(255, 0, 0,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderWidth: 1,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(255, 0, 0,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(255, 0, 0,1)",
            pointHoverBorderColor: "rgba(255, 0, 0,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: data.assay_value,
            spanGaps: false
        },var options = {
    responsive: false,
    title: {
        display: true,
        position: "top",
        text: label,
        fontSize: 18,
        fontColor: "#111"
    },
    legend: {
        display: true,
        position: "bottom",
        labels: {
            fontColor: "#333",
            fontSize: 16
        }
    },
    tooltips: {
        enabled: true,
        mode: 'single',
        callbacks: {
            label: function(tooltipItems, data) {
                var multistringText = ['Assay Value: '+tooltipItems.yLabel];
                multistringText.push('Assigned Value: '+assigned_value[tooltipItems.index]);
                multistringText.push('Sample ID: '+sample_id[tooltipItems.index]);
                return multistringText;
            }
        }
    },
    scales:{
        yAxes:[{
            ticks:{
                min:graph_min
            }
        }],
        xAxes: [{
            gridLines: {
                display:false
            }
        }]
    }
};
if(new_chart[ctx.canvas.id] != null)
    new_chart[ctx.canvas.id].destroy();

new_chart[ctx.canvas.id] =new Chart(ctx, {
    type: 'line',
    data: data,
    options: options
});

In x-axis there is data like this

[19-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,22-Aug-2015,27-Aug-2015,29-Aug-2015,1-Sep-2015,2-Sep-2015,3-Sep-2015,]

in y-axis data is like this

[0.1,0.05,0.89,0.89,0.79,0.58,0.68,0.25,0.98]
Share Improve this question edited Oct 17, 2019 at 6:10 ashleedawg 21.7k9 gold badges81 silver badges117 bronze badges asked Mar 29, 2017 at 12:12 tech_geektech_geek 1,8643 gold badges27 silver badges47 bronze badges 3
  • stepSize does work, what do you mean you did not suceed? – kristjan reinhold Commented Mar 29, 2017 at 13:28
  • kristjan it did not work on xaxis it only work to the yaxis – tech_geek Commented Mar 29, 2017 at 13:42
  • Can you post your entire chart.js config and a sample of your data? This is what is needed to provide a specific answer. – jordanwillis Commented Mar 29, 2017 at 15:22
Add a ment  | 

1 Answer 1

Reset to default 1

The way to control distance between points it to set the X and Y axis with a min, max, and step size so that they never change regardless of the number of points that are in the chart.

Here is an example that sets the scales so that they will never change. Regardless of how many points appear on the chart everything will stay the same.

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: 'Points',
      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)',
      borderColor: 'rgba(33, 232, 234, 1)',
      borderWidth: 1,
      fill: false,
      showLine: false,
    }],
  },
  options: {
    title: {
      display: true,
      text: 'Chart.js - Fixed X and Y Axis',
    },
    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,
        }
      }]
    }
  }
});

Here is a codepen example that demonstrates what this looks like

发布评论

评论列表(0)

  1. 暂无评论