最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - chart.js how to make x-axis labels position top - Stack Overflow

matteradmin8PV0评论

I'm new to chart.js, and I have a question about making x-axis labels position to the top.

This is the result that I expected: x-axis labels are on the top.

And this is my attempt:

const ctx = document.getElementById('weather_chart');
const myChart = new Chart(ctx, {
    data: {
        datasets: [{
            type: 'bar',
            label: 'rainfall probability',
            data: [10, 20, 30, 40],
        }, {
            type: 'line',
            label: 'temperature',
            data: [50, 50, 50, 50],
            borderColor: '#EB6E4B',
            backgroundColor: '#EB6E4B',
        }],
        labels: ['now', '9am', '10am', '11am', '12pm'],
    },
    options: {
        plugins: {
            legend: {
                display: false,
            },
        }
    }
});
<script src=".js"></script>
<div id="chart">
  <canvas id="weather_chart"></canvas>
</div>

I'm new to chart.js, and I have a question about making x-axis labels position to the top.

This is the result that I expected: x-axis labels are on the top.

And this is my attempt:

const ctx = document.getElementById('weather_chart');
const myChart = new Chart(ctx, {
    data: {
        datasets: [{
            type: 'bar',
            label: 'rainfall probability',
            data: [10, 20, 30, 40],
        }, {
            type: 'line',
            label: 'temperature',
            data: [50, 50, 50, 50],
            borderColor: '#EB6E4B',
            backgroundColor: '#EB6E4B',
        }],
        labels: ['now', '9am', '10am', '11am', '12pm'],
    },
    options: {
        plugins: {
            legend: {
                display: false,
            },
        }
    }
});
<script src="https://cdn.jsdelivr/npm/chart.js"></script>
<div id="chart">
  <canvas id="weather_chart"></canvas>
</div>

If it is possible, would you take your time to write the answer?

Thanks in advance, and if my question is insufficient, I would appreciate it if you could tell me.

Share Improve this question edited Nov 15, 2021 at 4:13 chunzhi23 asked Nov 15, 2021 at 3:15 chunzhi23chunzhi23 891 silver badge9 bronze badges 2
  • Can you show us any attempts you've made? Take a look at this stackoverflow./help/how-to-ask – about14sheep Commented Nov 15, 2021 at 3:18
  • Oh, I missed it. I truly apologize. I edited it and would this makes work? – chunzhi23 Commented Nov 15, 2021 at 4:15
Add a ment  | 

2 Answers 2

Reset to default 2

You can configure all scale related things like position in this namepsaces: options.scales[scaleId]. For more config options of the scales you can read the documentation: https://www.chartjs/docs/master/axes/

const ctx = document.getElementById('weather_chart');
const myChart = new Chart(ctx, {
  data: {
    datasets: [{
      type: 'bar',
      label: 'rainfall probability',
      data: [10, 20, 30, 40],
    }, {
      type: 'line',
      label: 'temperature',
      data: [50, 50, 50, 50],
      borderColor: '#EB6E4B',
      backgroundColor: '#EB6E4B',
    }],
    labels: ['now', '9am', '10am', '11am', '12pm'],
  },
  options: {
    scales: {
      x: {
        position: 'top'
      }
    },
    plugins: {
      legend: {
        display: false,
      },
    }
  }
});
<script src="https://cdn.jsdelivr/npm/chart.js"></script>
<div id="chart">
  <canvas id="weather_chart"></canvas>
</div>

inside options > scales > xAxes the xAxes is an array, so we can define multiple axis and there configuration like this:

options:{
    scales: {
        xAxes: [
            {
                position: 'top',
                ticks: {
                    maxRotation: 90,
                    minRotation: 80
                }
            }
        ]
    }
}

Checkout the example here https://codepen.io/ganeshbkdm/pen/oNeaOwm

Post a comment

comment list (0)

  1. No comments so far