javascript - 具有多个 y 轴和不同比例的 Chartjs

标签 javascript chart.js

如何使用 ChartJs 在 Y 轴上使用不同的刻度?

我有这个数据集:

[ 1450478,2645844,1840524,640057,1145844,1530500,1695844,1778654,1450478,1645844,1450478,1645844 ]

[ 3.14, 4.15, 3.09, 3.48, 4.05, 3.99, 4.39, 4.15, 4.10, 3.98, 3.54, 3.50 ]

https://jsfiddle.net/psycocandy/ad18Lc4u/18/

线条所代表的最小比例显然非常接近 0 的比例。如何在不按图例过滤的情况下使同一条线可见?

最佳答案

我发现了问题,更新了 fiddle :https://jsfiddle.net/psycocandy/fwncq25a/14/

要在数据集中使用 yAxes 设置相应的 ID,正确的方法是使用 yAxisID:

var chartData = [
                [1450478, 2645844, 1840524, 640057, 1145844, 1530500, 1695844, 1778654, 1450478, 1645844, 1450478, 1645844],
                [3.14, 4.15, 3.09, 3.48, 4.05, 3.99, 4.39, 4.15, 4.10, 3.98, 3.54, 3.50]
    ];

var dataSet = {
  labels: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho',
    'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'
  ],
  datasets: [{
                type: 'line',
                label: 'Preço Médio Mensal',
                yAxisID: 'y-axis-0',
                borderColor: '#17522f',
                fill: false,
                backgroundColor: '#17522f',
                borderWidth: 3,
                radius: 4,
                hoverBorderWidth: 4,
                rotate: 90,
                data: chartData[1],
                datalabels: {
                    display: true,
                    align: 'end',
                    anchor: 'end',
                    rotation: -90,
                    clamp: true,
                    clip: true,
                    color: '#17522f',
                    padding: 10,
                    formatter: function (value, context) {
                        return numeral(value).format('0,0.00');
                    },
                    font: {
                        weight: 'bold',
                    }
                }
            },{
                type: 'bar',
                label: 'Total Mensal',
                yAxisID: 'y-axis-1',
                backgroundColor: '#7579ef',
                borderColor: '#171951',
                borderWidth: 2,
                data: chartData[0],
                datalabels: {
                    display: true,
                    clamp: true,
                    anchor: 'start',
                    align: 'end',
                    offset: 10,
                    rotation: -90,
                    color: '#171951',
                    formatter: function (value, context) {
                        return numeral(value).format('0,0');
                    },
                    font: {
                        weight: 'bold',
                    }
                }
            }]
};

var chart = new Chart(document.getElementById('myChart'), {
  type: 'bar',
  data: dataSet,
  options: {
    title: {
      display: false
    },
    tooltips: {
      mode: 'label',
      callbacks: {
        label: function(tooltipItem, data) {
          var value = parseFloat(tooltipItem.value);
          var formatedValue;
          if(tooltipItem.datasetIndex > 0){
            formatedValue = numeral(value).format('$ 0,0.00');
          }else{
            formatedValue = numeral(value).format('$ 0,0.00');
          }
          return ' ' + formatedValue;
        },
      }
    },
    responsive: true,
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true,
        position: 'left',
        type: 'linear',
        scaleLabel: {
          display: true,
        },
        id: 'y-axis-1',
        ticks: {
          beginAtZero:true,
          callback: function (tick, index, ticks) {
            return numeral(tick).format('(0,0)');
          },
        }
      }, {
        stacked: false,
        position: 'right',
        type: 'linear',
        id: 'y-axis-0',
        ticks: {
          max: 10,
          stepSize: 1,
          display: true,
          beginAtZero: true,
          fontSize: 13,
          padding: 10,
          callback: function (tick, index, ticks) {
            return numeral(tick).format('$ 0,0');
          }
        }
      }]
    }
  }
});

关于javascript - 具有多个 y 轴和不同比例的 Chartjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498128/

相关文章:

javascript - Rails 'nil' 不是与 ActiveModel 兼容的对象。它必须实现 :to_partial_path. ajax

javascript - ajax PHP MySQL 查询

chart.js - Chart JS 3.0.2 中的时间序列比例。带来错误 "This method is not implemented: either no adapter can be found or an incomplete integration was ..."

chart.js - 使用 Chart.js 在条形图上显示标签

highcharts - Chartjs中highcharts的负条形图

javascript - 可以将附加功能绑定(bind)到函数吗?

javascript - jquery - 悬停在 li 宽度 id 上 - 显示匹配的 id div

javascript - VUE 3我如何检查元素何时在视口(viewport)中?

chart.js - 如何向 Ticks 添加次要/主要配置

javascript - ionic 不从变量返回数据