8
votes

I'm trying to switch of graph type using jquery.

I found the way to change dinamically the chart type (without recreating a new chart) by using the function :

series[i].update({ type: chartType});

My first question will be : There is a way to change the all chart and not just the series ? If not keep reading :)

But with this function i'm not able to make the 'bar' chart working. It's acting like a column chart. As you can see the pie example is working as excepted.

bar : http://www.highcharts.com/demo/bar-basic

How it works(sample example) :

<div id="top10_" style="float:left">
    <button id="set_column">column</button>
    <button id="set_bar">bar</button>
    <button id="set_pie">pie</button>
</div>
<div id="top10" style="min-width: 400px; height: 400px; margin: 0 auto;"></div>
$('#set_column').click(function () {
    var chart = $(this).parent('div').attr('id');
    chart = chart.replace('_', '');
    $('#' + chart).highcharts().series[0].update({
        type: "column"
    });
});
$('#set_bar').click(function () {
    var chart = $(this).parent('div').attr('id');
    chart = chart.replace('_', '');
    $('#' + chart).highcharts().series[0].update({
        type: "bar"
    });
});
$('#set_pie').click(function () {
    var chart = $(this).parent('div').attr('id');
    chart = chart.replace('_', '');
    $('#' + chart).highcharts().series[0].update({
        type: "pie"
    });
});

Highcharts creation :

$('#top10').highcharts({
    chart: {
        type: 'column',
        margin: [50, 50, 100, 80]
    },
    title: {
        text: 'TOP10'
    },
    subtitle: {
        text: ' '
    },
    credits: {
        enabled: false
    },
    xAxis: {
        categories: ['1', '2', '3', '4'],
        labels: {
            rotation: -45,
            align: 'right',
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Ilość'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.x + '</b><br/>' + 'Ilość: ' + this.y;
        }
    },
    series: [{
        name: 'Ilość zgłoszeń, TOP10',
        data: [1, 2, 3, 43],
        dataLabels: {
            enabled: true,
            rotation: -90,
            color: '#FFFFFF',
            align: 'right',
            x: 4,
            y: 10,
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    }]
});

Here is a fiddle example : http://jsfiddle.net/supergg/zqvNq/4/

Thanks,

4

4 Answers

12
votes

You can use inverted parameter in chart and update yAxis. http://jsfiddle.net/n9xkR/8/

$('#bar').click(function () {


    chart.inverted = true;
    chart.xAxis[0].update({}, false);
    chart.yAxis[0].update({}, false);
    chart.series[0].update({
        type: 'column'
    });

});
3
votes

This works for me:

$.each(chart.series, function (key, series) {
    series.update(
        {type: 'bar'},
        false
    );
}
0
votes

Here what works for me:

function changeType() {
  var series  = chart.series[0];
  var newType = your_condition_here ? 'column' : 'bar';
  var dataArray = [],
      points = series.data;

  series.chart.addSeries({
      type: newType,
      name: series.name,
      color: series.color,
      data: series.options.data
  }, false);
  series.remove();
}

It creates new series using the new type and removes the old one.

Free to add more options as you need.

Hope it helps someone :)

-1
votes

Not sure you find an answer yet or not but I found one link which is 101% useful to you even for future reference. So can you have a look please. I was searching for something else and I found this link and also got your question.

   [http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-update/][1]

Let me know or accept my answer if this will help to you in any manner. Cheers:)