我想用2个Yax做水平杆,其中一个杆可以具有正值或负值,而正侧具有yaxis,负侧具有其他yaxis,如图像



这是我的代码JSFiddle

我无法更改第二个Yaxes的名称

我添加具有相同值的数组,因为我想要两个yaxes,如果您知道我可以在其他地方使用的库,则不必使用“ chart.js”,请告诉我

    var canvas = document.getElementById('myChart');
    var extremo1=[-5, 3, 9, -11];
    var extremo2=[-5, 3, 9, -11];
    var data = {
    labels: ["Visua_Verbal", "Secuencial_Global", "Sensitivo_Intuitivo", "Reflexivo_Activo"],

    datasets: [
        {
         backgroundColor: 'rgba(63, 191, 191, 0.75)',
         borderColor: 'rgba(63, 191, 191, 0.75)',
         hoverBackgroundColor: 'rgba(191, 63, 63, 1)',
         hoverBorderColor: 'rgba(191, 63, 63, 1)',
                 data: extremo1
        },
        {
         backgroundColor: 'rgba(63, 191, 191, 0.75)',
         borderColor: 'rgba(63, 191, 191, 0.75)',
         hoverBackgroundColor: 'rgba(191, 63, 63, 1)',
         hoverBorderColor: 'rgba(191, 63, 63, 1)',
                 data: extremo1
        }
    ]
};

var option = {
      maintainAspectRatio: false,
      responsive: true,
        scales: {
               xAxes: [{
                 display: true,
                 ticks: {
                 maxTicksLimit: 12
                 }
               }],
               yAxes: [{
                  position: "left",
                  display: true,
                  ticks: {
                  callback:(label,index,labels)=>{
                  label = label.match(/_(\w*)/)[1];
                  return label;
                  }}
                  },
                  {
                   position: "right",
                   display: true,
                   ticks: {

                   callback:(label,index,labels)=>{
                   return label ;
                   }
                  }
                 }]
                 },
                 legend: {
                         display: false
                }

};
var myLineChart = new Chart(canvas,{
    type: 'horizontalBar',
    data:data,
  options:option
});

最佳答案

在下面的代码段中,我在y轴上设置了labelstypeoffset选项,以实现所需的结果。我还删除了不必要的属性。



var canvas = document.getElementById('myChart');
var extremo = [-5, 3, 9, -11];
var data = {
  datasets: [{
    backgroundColor: 'rgba(63, 191, 191, 0.75)',
    borderColor: 'rgba(63, 191, 191, 0.75)',
    hoverBackgroundColor: 'rgba(191, 63, 63, 1)',
    hoverBorderColor: 'rgba(191, 63, 63, 1)',
    data: extremo
  }]
};

var option = {
  maintainAspectRatio: false,
  responsive: true,
  scales: {
    xAxes: [{
      ticks: {
        maxTicksLimit: 12
      }
    }],
    yAxes: [{
        labels: ['Verbal', 'Global', 'Reflexivo', 'Sensitivo']
      },
      {
        position: 'right',
        labels: ['Visual', 'Secuencial', 'Activo', 'Intuitivo'],
        gridLines: {
          display: false
        },
        type: 'category',
        offset: true
      }
    ]
  },
  legend: {
    display: false
  }
};

var myLineChart = new Chart(canvas, {
  type: 'horizontalBar',
  data: data,
  options: option
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="myChart">

关于javascript - 带两个Yaxes图表JS的单杠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52749449/

10-11 06:09