Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Sort stackbar chart in highchart based on size of each stackbar

https://jsfiddle.net/vasjav1/ywspnocd

I wants to sort the stackbar chart from min to max, i.e. smallest bar should occurs first, and largest should occur at the end. I didn't find any solution being present anywhere else related to stackbar chart. there is solution present here https://www.highcharts.com/docs/advanced-chart-features/data-sorting, but it's mostly fitting for the barchart, but I couldn't fit it with the stack bar. Can someone please help me to sort stackbarchart?

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total fruit consumption'
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
    },
    tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 3, 4, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 3, 2, 1]
    }, {
        name: 'Joe',
        data: [3, 4, 4, 2, 5]
    }]
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You would first have to sort the data and then feed it to highcharts. For instance, if your input data can be represented as this:

{
    "Apples":  { "John": 5, "Jane": 2, "Joe": 3 },
    "Oranges": { "John": 3, "Jane": 2, "Joe": 4 },
    "Pears":   { "John": 4, "Jane": 3, "Joe": 4 },
    "Grapes":  { "John": 7, "Jane": 2, "Joe": 2 },
    "Bananas": { "John": 2, "Jane": 1, "Joe": 5 },
};

...then you could sort that, and then translate it to the argument you want to pass to highcharts:

// The original input data in one object (or array):
let data = {
    "Apples":  { "John": 5, "Jane": 2, "Joe": 3 },
    "Oranges": { "John": 3, "Jane": 2, "Joe": 4 },
    "Pears":   { "John": 4, "Jane": 3, "Joe": 4 },
    "Grapes":  { "John": 7, "Jane": 2, "Joe": 2 },
    "Bananas": { "John": 2, "Jane": 1, "Joe": 5 },
};

// Sort the data by sum of values:
let sorted = Object.entries(data).map(([cat, obj]) =>
    [cat, obj, Object.values(obj).reduce((a, b) => a + b)]
).sort((a, b) => a[2] - b[2]);

// Now generate the argument for the chart:
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        categories: sorted.map(([cat]) => cat)
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total fruit consumption'
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
    },
    tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
            }
        }
    },
    series: Object.keys(sorted[0][1]).map(name => ({
            name, data: sorted.map(([_, {[name]: val}]) => val)
    }))
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Currently, the data-sorting feature works only with individual series.

You can use dependent sorting as described here (example: https://jsfiddle.net/BlackLabel/3f8x7j5w/)

series: [{
  id: 'mainSeries',
  dataSorting: {
    enabled: true
  },
  data: [...]
}, {
  linkedTo: 'mainSeries',
  data: [...]
}, {
  linkedTo: 'mainSeries',
  data: [...]
}, {
  linkedTo: 'mainSeries',
  data: [...]
}]

or sort each series individually (example: https://jsfiddle.net/BlackLabel/dsm0bz3k/), but the best way will be to use the solution from trincot's answer.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda