• <bdo id='xoLWU'></bdo><ul id='xoLWU'></ul>

    <small id='xoLWU'></small><noframes id='xoLWU'>

    <tfoot id='xoLWU'></tfoot>

    1. <legend id='xoLWU'><style id='xoLWU'><dir id='xoLWU'><q id='xoLWU'></q></dir></style></legend>

      <i id='xoLWU'><tr id='xoLWU'><dt id='xoLWU'><q id='xoLWU'><span id='xoLWU'><b id='xoLWU'><form id='xoLWU'><ins id='xoLWU'></ins><ul id='xoLWU'></ul><sub id='xoLWU'></sub></form><legend id='xoLWU'></legend><bdo id='xoLWU'><pre id='xoLWU'><center id='xoLWU'></center></pre></bdo></b><th id='xoLWU'></th></span></q></dt></tr></i><div id='xoLWU'><tfoot id='xoLWU'></tfoot><dl id='xoLWU'><fieldset id='xoLWU'></fieldset></dl></div>
    2. Chart.js 压缩条形图上的垂直轴

      时间:2023-11-02
      • <small id='6AOiF'></small><noframes id='6AOiF'>

          <tbody id='6AOiF'></tbody>
        <i id='6AOiF'><tr id='6AOiF'><dt id='6AOiF'><q id='6AOiF'><span id='6AOiF'><b id='6AOiF'><form id='6AOiF'><ins id='6AOiF'></ins><ul id='6AOiF'></ul><sub id='6AOiF'></sub></form><legend id='6AOiF'></legend><bdo id='6AOiF'><pre id='6AOiF'><center id='6AOiF'></center></pre></bdo></b><th id='6AOiF'></th></span></q></dt></tr></i><div id='6AOiF'><tfoot id='6AOiF'></tfoot><dl id='6AOiF'><fieldset id='6AOiF'></fieldset></dl></div>
        <legend id='6AOiF'><style id='6AOiF'><dir id='6AOiF'><q id='6AOiF'></q></dir></style></legend>

                <bdo id='6AOiF'></bdo><ul id='6AOiF'></ul>
                <tfoot id='6AOiF'></tfoot>
                本文介绍了Chart.js 压缩条形图上的垂直轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个数据集,其中最后一个值总是很高.这导致我的条形图出现问题;如果不将鼠标悬停在它们上面,几乎所有其他值都很难感受.

                I have a dataset in which the last value is always very high. This causes an issue with my bar chart; almost all the other values are hard to get a feeling for without hovering over them.

                这是截图:

                这就是我想要达到的目标;

                This is what I am trying to achieve;

                所以我的问题;这在 vanilla Chart.js 中是否可行,还是我需要一个插件?如果是这样;有现成的插件还是我需要自己写一个?

                So my question; is this possible within vanilla Chart.js or do I need a plugin? And if so; is there an existing plugin or do I need to write one myself?

                我也愿意接受最初问题的替代解决方案.

                I am also open for alternative solutions to the initial problem.

                我已经在整个互联网上寻找类似的东西,但不幸的是没有太多运气.

                I've looked all over the internet for something like this but unfortunately without much luck.

                推荐答案

                可以使用logarithmic type yAxis

                默认:线性

                https://www.chartjs.org/docs/latest/axes/cartesian/logarithmic.html

                var ctx = document.getElementById('myChart');
                
                var myChart = new Chart(ctx, {
                  type: 'bar',
                  data: {
                    labels: ["January", "February", "March", "April", "May", "June"],
                    datasets: [{
                      label: "Series 1",
                      backgroundColor: "rgba(255,99,132,0.2)",
                      borderColor: "rgba(255,99,132,1)",
                      borderWidth: 2,
                      hoverBackgroundColor: "rgba(255,99,132,0.4)",
                      hoverBorderColor: "rgba(255,99,132,1)",
                      data: [65, 59, 43, 81, 56, 950],
                    }]
                  },
                  options: {
                    scales: {
                      yAxes: [{
                        type: 'logarithmic',
                        ticks: {
                          callback: function(tick, index, ticks) {
                            return tick.toLocaleString();
                          }
                        }
                      }]
                    }
                  }
                });

                <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
                <canvas id="myChart" width="400" height="200"></canvas>

                优化的报价

                50 * (Math.floor(i / 50)), // lower 50
                50 * (Math.ceil(i / 50)) // higer 50
                

                var ctx = document.getElementById('myChart');
                
                var myChart = new Chart(ctx, {
                  type: 'bar',
                  data: {
                    labels: ["January", "February", "March", "April", "May", "June"],
                    datasets: [{
                      label: "Series 1",
                      backgroundColor: "rgba(255,99,132,0.2)",
                      borderColor: "rgba(255,99,132,1)",
                      borderWidth: 2,
                      hoverBackgroundColor: "rgba(255,99,132,0.4)",
                      hoverBorderColor: "rgba(255,99,132,1)",
                      data: [65, 59, 43, 81, 56, 950],
                    }]
                  },
                  options: {
                    scales: {
                      yAxes: [{
                        type: 'logarithmic',
                        ticks: {
                          callback: function(tick, index, ticks) {
                            return tick.toLocaleString();
                          }
                        },
                        afterBuildTicks: function(pckBarChart) {
                          pckBarChart.ticks = pckBarChart.chart.data.datasets[0].data.flatMap(i => [
                            50 * (Math.floor(i / 50)), // lower 50
                            50 * (Math.ceil(i / 50)) // higer 50
                          ])
                        }
                      }]
                    }
                  }
                });

                <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
                <canvas id="myChart" width="400" height="200"></canvas>

                这篇关于Chart.js 压缩条形图上的垂直轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Chart.js 响应选项 - 图表未填充容器 下一篇:图表js工具提示如何控制显示的数据

                相关文章

                  <bdo id='auaGG'></bdo><ul id='auaGG'></ul>

                <legend id='auaGG'><style id='auaGG'><dir id='auaGG'><q id='auaGG'></q></dir></style></legend>

                <tfoot id='auaGG'></tfoot>
                <i id='auaGG'><tr id='auaGG'><dt id='auaGG'><q id='auaGG'><span id='auaGG'><b id='auaGG'><form id='auaGG'><ins id='auaGG'></ins><ul id='auaGG'></ul><sub id='auaGG'></sub></form><legend id='auaGG'></legend><bdo id='auaGG'><pre id='auaGG'><center id='auaGG'></center></pre></bdo></b><th id='auaGG'></th></span></q></dt></tr></i><div id='auaGG'><tfoot id='auaGG'></tfoot><dl id='auaGG'><fieldset id='auaGG'></fieldset></dl></div>

                1. <small id='auaGG'></small><noframes id='auaGG'>