Gráfico de Barras Horizontal

Cómo crear un gráfico de barras horizontal en Chart.js. En esta publicación, cubriremos cómo crear un gráfico de barras horizontal. Crear un gráfico de barras horizontal en Chart.js no es muy diferente en comparación con un gráfico de barras. En esencia, es un gráfico de barras con un ángulo de 90 grados. Mira este video:

Gráfico de Barras Horizontal en Chart.js

Gráfico de Barras Horizontal en Chart.js

Para crear un gráfico de barras horizontal, es importante entender lo que realmente sucede. Utilizaremos el espacio de nombres (namespace) `indexAxis` para intercambiar los valores del eje x al eje y y viceversa. Al intercambiarlos, el gráfico de barras cambia básicamente a un ángulo de 90 grados. El eje y obtiene las etiquetas y las barras comienzan en la izquierda y se desplazan hacia la derecha dependiendo de sus valores.

En esencia, un gráfico de barras horizontal en Chart.js es simplemente un gráfico de barras en el que se han intercambiado las escalas o ejes x e y. Comprender esto te proporciona una imagen clara de cómo funciona Chart.js bajo el capó.

¿Fue útil esta página?

2 respuestas a «Horizontal Bar Chart»

Hi, I’ve been following your videos on youtube for quite some time and I have to say they are quite helpful, thank you. I’m just wondering, is it possible to fix the bar size on a single horizontal stacked bar so long as the difference from i+1 and i is the same? Let’s say we have a dataset of 20 values from 10 to 200, obviously i+1 -i is a difference of 10 for all values. So plotting it on a stacked horizontal bar, I want it to maintain fixed bar size. What’s happening at the moment is the bars start out thinner and eventually increase in size despite being the difference.

Hi James, thank you for your kind words. Regarding to your question if I understand it correctly is as you add more bars the bars are becoming thinner. In essence they have to become narrow due to the fixed size in the canvas.

I’m not sure if you are familiar with how the chart js canvas calculates height and width. But in your case how it is build is working against to what you want to achieve. While might have some idea but not sure if it work. I will make a video about this.

But here is currently my speculation:
1. Disable the maintainAspectRatio settings in Chart JS. This is your core issue working against your desired result.
2. The height of your canvas should have a min height but also a dynamic height based on how many horizontal bars you have. This could be achieves with the JS arrays as we can read the JS objects of Chart JS. That needs to be a height formula that would work once the min-height would be surpassed.

For example: height = each bar * 20px.

I will explore this and once I have the answer post a video about this and notify you as well.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *