简体   繁体   English

使用JavaScript更改Highcharts的高度。 默认情况下,仅在窗口重新调整大小时重新调整大小

[英]Change height of Highcharts with JavaScript. By default only re-sizes on window re-size

We're using HighCharts in our app, and I've added a function to expand the chart fullsize. 我们在我们的应用程序中使用HighCharts ,并且我添加了一个扩展图表fullsize的功能。 I change the styles as well as use Javascript to change the height of the div. 我更改样式以及使用Javascript来更改div的高度。

However nothing changes until you actually resize the browser window. 但是,在实际调整浏览器窗口大小之前,没有任何变化 Anyone else run into this issue? 其他人遇到这个问题?

在此输入图像描述

<section id="highchart-container" ng-class="{'high-chart-expanded' : highChartMax}">
    <highchart id="chart1" config="chartConfig" style="height:auto"></highchart>
</section>

ChartHeader scope ChartHeader范围

function expandChartPanel() {
    vm.chartMaxed = !vm.chartMaxed;
    highChart     = ScopeFactory.getScope('highChart');

    if (vm.chartMaxed) {
        highChart.highChartMax = true;
    }
    else {
        highChart.highChartMax = false;
    }

    highChart.toggleChartSize();
}

HighChartDirective scope HighChartDirective范围

function toggleChartSize() {
    var chart1 = document.getElementById("chart1");

    if (vs.highChartMax) {
        chart1.style.height = "100%";
    } else {
        chart1.style.height = "400px";
    }
}

Styles (SASS) 样式(SASS)

.high-chart-expanded {
    min-height: 100% !important;
    max-height: 100% !important;
    width: 100% !important;
    height: 100% !important;

    #highcharts-6,
    #chart1,
    .highcharts-background,
    .highcharts-container {
        min-height: 100% !important;
        max-height: 100% !important;
        width: 100% !important;
        height: 100% !important;
    }
}

HighChart chartConfig HighChart chartConfig

ApiFactory.quotes(buildFullUrl(url)).then(function (data) {
    var quote_data = formatQuotes(data, 'quotes');

    // Create the chart
    vs.chartConfig = {
        options: {
            legend: { 
                itemStyle: { 
                    color: "#333333", 
                    cursor: "pointer", 
                    fontSize: "10px", 
                    fontWeight: "normal" 
                },
                enabled: true, 
                floating: true, 
                align: 'left', 
                verticalAlign: 'top', 
                x: 60 
            },
            chart : {
                zoomType: 'x',
                events: {
                    load: function () {
                        // HighChart loaded callback:
                        broadcastChartloaded();
                    }
                }
            },

This is what I see when I console out the chartConfig 这是我在chartConfig时看到的chartConfig

console.log('highChart.chartConfig = ', highChart.chartConfig);

在此输入图像描述

Try chart.setSize(width, height) ? 试试chart.setSize(width, height)

Here's a working example 这是一个有效的例子

UPDATE : for angular directive 更新:用于角度指令

To Pull out the chart object from directive you can just go the jQuery route: 要从指令中拉出图表对象,您可以转到jQuery路径:

var chart = $('#theChart').highcharts();
chart.setSize(width, height);

Specifically for ng-highcharts users, here's how its recommended to pull out the high-charts object by the author of the directive. 特别是对于ng-highcharts用户,这里建议如何通过该指令的作者提取高图表对象。 The above method will work just fine too. 上面的方法也可以正常工作。

var chart = this.chartConfig.getHighcharts();
chart.setSize(width, height);

Although you can do it anywhere in your controller/directive/service, I would recommend you create a new service that returns this object , and then inject it in your controller if you are strict about following Angular design pattern, but if not just those two lines should work fine anywhere that you have access to chartsConfig object. 虽然您可以在控制器/指令/服务中的任何地方执行此操作,但我建议您创建一个返回此对象的新服务,如果您严格遵循Angular设计模式,则将其注入控制器,但如果不是这两个在您有权访问chartsConfig对象的任何地方,行都可以正常工作。

To reset the chart to being responsive again check this answer . 要将图表重置为再次响应 ,请检查此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM