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

javascript - Display a limited number of labels only on X-Axis of Line Chart using Chart.js - Stack Overflow

programmeradmin2浏览0评论

1) How to display only first, middle and last OR only first and last labels(Dates) on X-Axis of Line chart using chart.js?

2) Also, if possible I want to display only those specific number of vertical gridlines.

Code:

var myLineChart = new Chart(document.getElementById("line-chart"), {
    type: 'line',
    data: {
        labels: reportDate,
        datasets:[{
            label: 'ABC',
            data: abcArray,
            backgroundColor:"white",
            borderColor: "darkblue",
            fill: false,
            lineTension: 0,
            radius: 5
        },{
            label: 'DEF',
            data: defArray,
            backgroundColor:"white",
            borderColor: "magenta",
            fill: false,
            lineTension: 0,
            radius: 5
        },{
            label: 'XYZ',
            data: xyzArray,
            backgroundColor:"white",
            borderColor: "lightgreen",
            fill: false,
            lineTension: 0,
            radius: 5
        }]
    },
    options: {
        responsive: true,
        scales:{
            xAxes:[{
                ticks:{
                    display: true
                }
            }],
            yAxes:[{
                gridLines:{
                    display: false
                }
            }]
        },
        tooltips:{
            mode:'index'
        }
    }
});

Line Chart Image

1) How to display only first, middle and last OR only first and last labels(Dates) on X-Axis of Line chart using chart.js?

2) Also, if possible I want to display only those specific number of vertical gridlines.

Code:

var myLineChart = new Chart(document.getElementById("line-chart"), {
    type: 'line',
    data: {
        labels: reportDate,
        datasets:[{
            label: 'ABC',
            data: abcArray,
            backgroundColor:"white",
            borderColor: "darkblue",
            fill: false,
            lineTension: 0,
            radius: 5
        },{
            label: 'DEF',
            data: defArray,
            backgroundColor:"white",
            borderColor: "magenta",
            fill: false,
            lineTension: 0,
            radius: 5
        },{
            label: 'XYZ',
            data: xyzArray,
            backgroundColor:"white",
            borderColor: "lightgreen",
            fill: false,
            lineTension: 0,
            radius: 5
        }]
    },
    options: {
        responsive: true,
        scales:{
            xAxes:[{
                ticks:{
                    display: true
                }
            }],
            yAxes:[{
                gridLines:{
                    display: false
                }
            }]
        },
        tooltips:{
            mode:'index'
        }
    }
});

Line Chart Image

Share Improve this question asked Sep 16, 2018 at 19:51 JaiJai 2412 silver badges9 bronze badges 1
  • Line number 4 - labels: reportDate - In this reportDate is an array of dates which will be later in format of mm/dd/yyyy, thus I need to display less number of dates so the UI looks cleaner. – Jai Commented Sep 16, 2018 at 19:58
Add a ment  | 

2 Answers 2

Reset to default 15

This worked perfectly.

xAxes:[{
    ticks:{
        display: true,
        autoSkip: true,
        maxTicksLimit: 4
    }
}]

Above Char.js Version 4.21

xAxes was changed to x and array format was changed to object format

Example:

  scales: {
    x: {
      ticks: {
        display: true,
        autoSkip: true,
        maxTicksLimit: 15,
      },
    },
  },

Source: https://www.chartjs/docs/latest/axes/cartesian/linear.html#mon-tick-options-to-all-cartesian-axes

发布评论

评论列表(0)

  1. 暂无评论