如何使用图表JS在垂直条形图中设置y轴值
P粉729436537
P粉729436537 2023-08-29 10:38:36
[CSS3讨论组]

在此垂直条形图中,y 轴有正值、负值。

我想根据零值上下使用正整数。

我使用的是4.2.1版本

我该怎么办?

垂直图表示例

var MONTHS = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December",
    ];
    var color = Chart.helpers.color;
    var barChartData = {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [
        {
          label: "Dataset 1",
          backgroundColor: 'rgba(255, 201, 14, 1)',
          borderColor: 'rgba(255, 201, 14, 1)',
          borderWidth: 1,
          data: [
            10,
            20,
            30,
            40,
            50
          ],
        },
        {
          label: "Dataset 2",
          backgroundColor: 'rgba(255, 201, 14, 1)',
          borderColor: 'rgba(255, 201, 14, 1)',
          borderWidth: 1,
          data: [
            -100,
            -200,
            -300,
            -400,
            -500
          ],
        },
      ],
    };

    var ctx = bloodPressureChart;
    new Chart(ctx, {
      type: "bar",
      data: barChartData,
      options: {
        responsive: true,
        legend: {
          position: "top",
        },
        title: {
          display: true,
          text: "Chart.js Bar Chart",
        },
      },
    });

这是我使用图表 JS 的代码。

我的代码图表

P粉729436537
P粉729436537

全部回复(1)
P粉316110779

如果您只想更改 y 轴标签的文本,可以通过设置函数 options.scales.y.ticks.callback 来完全更改它们,请参阅文档API 参考了解详细信息。在您的情况下,要使负值读为正值,您可以使用:

callback: function(val){
    return this.getLabelForValue(Math.abs(val));
}

callback: function(val){
    return this.getLabelForValue(val).replace(/^-/, '');
}

var ctx = document.getElementById('chart1');
var MONTHS = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

var barChartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "Dataset 1",
            backgroundColor: 'rgba(255, 201, 14, 1)',
            borderColor: 'rgba(255, 201, 14, 1)',
            borderWidth: 1,
            data: [
                10,
                20,
                30,
                40,
                50
            ],
        },
        {
            label: "Dataset 2",
            backgroundColor: 'rgba(255, 201, 14, 1)',
            borderColor: 'rgba(255, 201, 14, 1)',
            borderWidth: 1,
            data: [
                -100,
                -200,
                -300,
                -400,
                -500
            ],
        },
    ],
};

new Chart(ctx, {
    type: "bar",
    data: barChartData,
    options: {
        responsive: true,
        legend: {
            position: "top",
        },
        title: {
            display: true,
            text: "Chart.js Bar Chart",
        },
        scales:{
            y: {
                ticks: {
                    callback: function(val){
                        return this.getLabelForValue(Math.abs(val));
                        // or: //return this.getLabelForValue(val).replace(/^-/, '');
                    },
                }
            }
        },
    },
});
<canvas id="chart1" style="height:500px"></canvas>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.1.2/chart.umd.js"
        integrity="sha512-t41WshQCxr9T3SWH3DBZoDnAT9gfVLtQS+NKO60fdAwScoB37rXtdxT/oKe986G0BFnP4mtGzXxuYpHrMoMJLA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号