isivis
isivis

Reputation: 25

Horizontal bar with two yaxes chart js

I want to do an horizontal bar with 2 yaxes, Where a bar can have a positive or negative value and positive side has a yaxis and the negative side has other yaxis like the image

chart

This is my code JSFiddle.

I can't change the names of the second yaxes

I add arrays with the same values because i would want two yaxes, It is not necessary to use "chart.js" if you knows other library where i can it use, please tell me

    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
});

Upvotes: 2

Views: 1494

Answers (1)

timclutton
timclutton

Reputation: 13004

In the snippet below I've set the options labels, type, offset on the y-axes to achieve the result you want. I've also removed unnecessary properties.

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">

Upvotes: 2

Related Questions