0

I'm making a multiple panel chart, and I'm trying to hide the y-axis on the hide event of the axis serie.

I tried setting the axis height and redrawing it (didn't work), set extremes, nothing worked. I also tryed this solution but didn't work, I beleave it didn't work beacause I'm using highstock and the "solution" use Highcharts, does that make sense?

I also have to resize the others y-axis when one is hidden, but this is another problem. But if someone has a tip on how to do it automatically would be thankful

Here is my JSFiddle code.

$(function () {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {        
    var data1 = [ [100,0], [200,0], [300,1], [400,0], [500,1] ];
    var data2 = [ [100,1], [200,0], [300,1], [400,0], [500,0] ];      
    var data3 = [ [100,1], [200,1], [300,0], [400,0], [500,1] ];    
    var data4 = [ [100,0], [200,1], [300,1], [400,0], [500,0] ];

    // create the chart
    var chart = $('#container').highcharts('StockChart', {

        title: {
            text: 'AAPL Historical'
        },
        legend: {
            enabled: true
        },            

        plotOptions: {
            series: {
                events: {
                    hide: function (event) {
                        console.log(this.yAxis)
                        //Hide
                    },
                    show: function (event) {
                        console.log(this.yAxis)
                        //Display
                    }
                }
            }
        },

        tooltip: {
            pointFormatter: function() {
                var state = (this.y == 1 ? "Active" : "Inactive");
                var tooltip = '<span style="color:' + this.color + '">\u25CF</span> ' + this.series.name + ': <b>' + state + '</b><br/>'

                return tooltip;
            }
        },

        yAxis: [{
            height: '25%',
            offset: 0,
            lineWidth: 2,
            labels: {enabled: false}
        }, {
            top: '25%',
            height: '25%',
            offset: 0,
            lineWidth: 2,
            labels: {enabled: false},
            title : {
                text: "aaa"
            }
        }, {
            top: '50%',
            height: '25%',
            offset: 0,
            lineWidth: 2,
            labels: {enabled: false}
        }, {
            top: '75%',
            height: '25%',
            offset: 0,
            lineWidth: 2,
            labels: {enabled: false}
        }],

        series: [{
            name: 'Data1',
            data: data1,
            step: true,
            yAxis: 0
        }, {
            name: 'Data2',
            data: data2,
            step: true,
            yAxis: 1
        }, {
            name: 'Data3',
            data: data3,
            step: true,
            yAxis: 2
        }, {
            name: 'Data4',
            data: data4,
            step: true,
            yAxis: 3
        }]
    });
});

});

Community
  • 1
  • 1
Rafael Teles
  • 2,708
  • 2
  • 16
  • 32

1 Answers1

0

I worked more on solution and I found A way to hide the y-axis, by changing its height to 0% on the series hide event. I'm also increasing the axis height back to 25% in the series show event.

plotOptions: {
    series: {
        events: {
            hide: function (event) {
                this.yAxis.update({
                    height: '0%'
                });
            },
            show: function (event) {
                this.yAxis.update({
                    height: '25%'
                });
            }
        }
    }
},

Full code

Edit:

I found a way to resize the others y-axis when one of them is hidden or one the axis is displayed. You can check the full code.

$(function () {
    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function (data) {        
        var data1 = [ [100,0], [150,1], [150,0], [200,0], [300,1], [400,0], [500,1] ];
        var data2 = [ [100,1], [200,0], [300,1], [400,0], [500,0] ];      
        var data3 = [ [100,1], [200,1], [300,0], [400,0], [500,1] ];    
        var data4 = [ [100,0], [200,1], [300,1], [400,0], [500,0] ];

        // create the chart
        var chart = $('#container').highcharts('StockChart', {

            title: {
                text: 'AAPL Historical'
            },
            legend: {
                enabled: true
            },            

            plotOptions: {
                series: {
                    marker: {
                        enabled: true,
                        radius : 2
                    },
                    events: {
                        hide: function (event) {
                            var serieYAxis = this.yAxis;
                            serieYAxis.visivel = false;
                            serieYAxis.update({
                                height: '0%',
                                title: {
                                    style: {"display":"none"}
                                }
                            });

                            var axis = this.chart.yAxis.filter(
                                function (axis) {
                                    return axis.visivel == null || axis.visivel;
                                }
                            );
                            resizeAxis(axis);

                        },
                        show: function (event) {
                            this.yAxis.visivel = true;
                            this.yAxis.update({
                                title: {
                                    style: {"display":"initial"}
                                }
                            });

                            var axis = this.chart.yAxis.filter(
                                function (axis) {
                                    return axis.visivel == null || axis.visivel;
                                }
                            );


                            resizeAxis(axis);
                        }
                    }
                }
            },

            tooltip: {
                pointFormatter: function() {
                    var state = (this.y == 1 ? "Active" : "Inactive");
                    var tooltip = '<span style="color:' + this.color + '">\u25CF</span> ' + this.series.name + ': <b>' + state + '</b><br/>'

                    return tooltip;
                }
            },

            yAxis: [{
                height: '25%',
                offset: 0,
                lineWidth: 2,
                labels: {enabled: false},
                title : {
                    text: "y0"
                }
            }, {
                top: '25%',
                height: '25%',
                offset: 0,
                lineWidth: 2,
                labels: {enabled: false},
                title : {
                    text: "y1"
                }
            }, {
                top: '50%',
                height: '25%',
                offset: 0,
                lineWidth: 2,
                labels: {enabled: false},
                title : {
                    text: "y2"
                }
            }, {
                top: '75%',
                height: '25%',
                offset: 0,
                lineWidth: 2,
                labels: {enabled: false},
                title : {
                    text: "y3"
                }
            }],

            series: [{
                name: 'Data1',
                data: data1,
                step: true,
                yAxis: 0
            }, {
                name: 'Data2',
                data: data2,
                step: true,
                yAxis: 1
            }, {
                name: 'Data3',
                data: data3,
                step: true,
                yAxis: 2
            }, {
                name: 'Data4',
                data: data4,
                step: true,
                yAxis: 3
            }]
        });
    });


});
Community
  • 1
  • 1
Rafael Teles
  • 2,708
  • 2
  • 16
  • 32