【问题标题】:How to resize charts to fit the parent container with Google API如何使用 Google API 调整图表大小以适应父容器
【发布时间】:2016-04-09 19:31:44
【问题描述】:

所以我使用 Google 的可视化 API 创建了一个组合图表,但是我在调​​整所述图表的大小以适合其父容器时遇到问题

这就是它在网站上的样子,父容器的宽度是悬停的那个。我希望图表填充整个父母的宽度。

我有一个面板系统,其中每个选项卡都有不同的图表。第一个像魅力一样工作,我对它正确填充父容器宽度的那个没有问题,但第二个是我遇到问题的那个。

这是 HTML

 <div class="tab-pane fade in active" id="anuales">
    <div id ="anual-bar-chart" height ="500px" ></div>
 </div>
 <div class="tab-pane fade in" id="semestre-1">
    <div id ="semester-1-chart" height="500px"></div>
 </div>

这是绘制图表的js文件

google.load("visualization", "1", {packages:["corechart", 'bar']});
google.setOnLoadCallback(drawChart);
function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Año', 'Nacimiento', 'Reconocimiento', 'Adopción Simple', 'Matrimonios', 'Divorcios', 'Defunciones', 'Sentencias', 'Actas Foráneas', 'Promedio'],
        ['1997',  39520,732,39,10332,489,6033,88,0,7154],
        ['1998',  39113,728,61,9908,607,6261,82,0,7095],
        ['1999',  41007,825,58,10476,611,6103,74,0,7394],
        ['2000',  40337,898,30,10479,685,6198,80,0,7338],
        ['2001',  38132,847,10,9856,849,6288,78,0,7008],
        ['2002',  36933,856,7,9532,826,6425,96,0,6834],
        ['2003',  38688,858,63,9600,915,6731,139,0,7124],
        ['2004',  39612,919,40,9088,962,6674,199,0,7187],
        ['2005',  40985,1053,6,8785,1037,6874,215,0,7369],
        ['2006',  38863,1031,28,9023,1063,6954,164,0,7141],
        ['2007',  42757,1226,0,9318,1177,7169,3,0,8596],
        ['2008',  41310,1268,1,8842,1224,7676,1,0,7540],
        ['2009',  41155,1227,4,8185,1136,7757,5,0,7434],
        ['2010',  10867,1258,3,8268,1200,8250,3,330,7522],
        ['2011',  41760,1314,2,8977,1356,8077,5,987,7810],
        ['2012',  41553,1386,4,9240,1400,8622,7,782,7874],
        ['2013',  40953,1415,0,9726,1603,9107,11,622,7930],
        ['2014',  40981,1305,0,9713,1516,9349,5,619,7936],
        ['2015',  27017,887,0,6342,1227,3085,3,398,5245],
    ]);

var options = {

    titleTextStyle: {color:'white'},
    backgroundColor: {fill: 'transparent'},
    chartArea: {width:'85%',height:'65%'},
    vAxis: {textStyle:{color:'white'}},
    legend:{textStyle: {color: 'white'}, position: 'bottom'},
    height: 350,
    hAxis: {textStyle:{color:'white'}},
    seriesType: 'bars',
    series: {8 : {type: 'line'}}
};

var chart = new google.visualization.ComboChart(document.getElementById('anual-bar-chart'));
chart.draw(data, options);
var data2 = google.visualization.arrayToDataTable([
    ['Mes', 'Nacimiento', 'Defunciones', 'Matrimonios', 'Divorcios', 'Reconocimientos', 'Adopción Simple', 'Sentencias', 'Actas Foráneas', 'Promedio'],
    ['Ene-15',3865,897,586,130,138,0,0,38,0],
    ['Feb-15',3322,793,818,166,143,0,0,62,0],
    ['Mar-15',3314,802,745,156,88,0,0,52,0],
    ['Abr-15',3289,714,653,159,106,0,1,35,0],
    ['May-15',3153,718,662,155,20,0,0,37,0],
    ['Jun-15',3349,728,901,162,103,0,0,42,0],
    ['Jul-15',3254,697,797,168,10,0,2,70,0],
    ['Ago-15',3462,736,1182,131,123,0,0,62,0],

]);

var options2 = {

    titleTextStyle: {color:'white'},
    backgroundColor: {fill: 'transparent'},
    chartArea: {width:'85%',height:'65%'},
    vAxis: {textStyle:{color:'white'}},
    legend:{textStyle: {color: 'white'}, position: 'bottom'},
    height: 350,

    hAxis: {textStyle:{color:'white'}},
    seriesType: 'bars',
    series: {8 : {type: 'line'}}
};

var chart2 = new google.visualization.ComboChart(document.getElementById('semester-1-chart'));
chart2.draw(data2, options2);
}

底部名为 char2 的图表是我遇到问题的图表。我把另一个给你看,我使用的是相同的配置,但不知何故,它显示的图表不同。

谁能告诉我我能做什么,因为我一直在环顾四周,什么都没有。我尝试调整 api 提到的“chartArea”配置的大小,但这只会取出标签,但不适合父容器。

【问题讨论】:

    标签: javascript html charts google-visualization


    【解决方案1】:

    Google 图表不会自动调整大小。当事物调整大小(显示/隐藏是调整大小)时,必须重新绘制它们。 我相信下面的代码,我撕掉了this question 应该可以解决你的问题(你也需要 jquery):

    //create trigger to resizeEnd event     
    $(window).resize(function() {
        if(this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function() {
            $(this).trigger('resizeEnd');
        }, 500);
    });
    
    //redraw graph when window resize is completed  
    $(window).on('resizeEnd', function() {
        drawChart(data);
    });
    

    【讨论】:

    • 有没有办法做到相同但负载均匀而不是调整大小?
    • 如果我正确理解您的评论,document.ready 就是您正在寻找的东西。看看jquery documentation on that
    • 是的,我试过了,但没有成功……不过我想出了办法……谢谢!
    【解决方案2】:

    我想知道我的建议是否能解决您的问题...您不应该分别调用两次来绘制图形吗?也许您的第一个条形图正在影响您的第二个,因为您将它们捆绑到一个函数调用中。对于我的一个项目,我需要提供饼图和地理图,因此我创建了一个单独的函数来单独调用它们。

    请试试这个:

    function drawCharts(){
        anualBarChart();
        semester1Chart();
    }       
    function anualBarChart(){
        var data = google.visualization.arrayToDataTable([
            ['Año', 'Nacimiento', 'Reconocimiento', 'Adopción Simple', 'Matrimonios', 'Divorcios', 'Defunciones', 'Sentencias', 'Actas Foráneas', 'Promedio'],
            ['1997',  39520,732,39,10332,489,6033,88,0,7154],
            ['1998',  39113,728,61,9908,607,6261,82,0,7095],
            ['1999',  41007,825,58,10476,611,6103,74,0,7394],
            ['2000',  40337,898,30,10479,685,6198,80,0,7338],
            ['2001',  38132,847,10,9856,849,6288,78,0,7008],
            ['2002',  36933,856,7,9532,826,6425,96,0,6834],
            ['2003',  38688,858,63,9600,915,6731,139,0,7124],
            ['2004',  39612,919,40,9088,962,6674,199,0,7187],
            ['2005',  40985,1053,6,8785,1037,6874,215,0,7369],
            ['2006',  38863,1031,28,9023,1063,6954,164,0,7141],
            ['2007',  42757,1226,0,9318,1177,7169,3,0,8596],
            ['2008',  41310,1268,1,8842,1224,7676,1,0,7540],
            ['2009',  41155,1227,4,8185,1136,7757,5,0,7434],
            ['2010',  10867,1258,3,8268,1200,8250,3,330,7522],
            ['2011',  41760,1314,2,8977,1356,8077,5,987,7810],
            ['2012',  41553,1386,4,9240,1400,8622,7,782,7874],
            ['2013',  40953,1415,0,9726,1603,9107,11,622,7930],
            ['2014',  40981,1305,0,9713,1516,9349,5,619,7936],
            ['2015',  27017,887,0,6342,1227,3085,3,398,5245],
        ]);
    
        var options = {
            titleTextStyle: {color:'white'},
            backgroundColor: {fill: 'transparent'},
            chartArea: {width:'85%',height:'65%'},
            vAxis: {textStyle:{color:'white'}},
            legend:{textStyle: {color: 'white'}, position: 'bottom'},
            height: 350,
            hAxis: {textStyle:{color:'white'}},
            seriesType: 'bars',
            series: {8 : {type: 'line'}}
        };
    
        var chart = new google.visualization.ComboChart(document.getElementById('anual-bar-chart'));
        chart.draw(data, options);
    }
    function semester1Chart(){
        var data2 = google.visualization.arrayToDataTable([
            ['Mes', 'Nacimiento', 'Defunciones', 'Matrimonios', 'Divorcios', 'Reconocimientos', 'Adopción Simple', 'Sentencias', 'Actas Foráneas', 'Promedio'],
            ['Ene-15',3865,897,586,130,138,0,0,38,0],
            ['Feb-15',3322,793,818,166,143,0,0,62,0],
            ['Mar-15',3314,802,745,156,88,0,0,52,0],
            ['Abr-15',3289,714,653,159,106,0,1,35,0],
            ['May-15',3153,718,662,155,20,0,0,37,0],
            ['Jun-15',3349,728,901,162,103,0,0,42,0],
            ['Jul-15',3254,697,797,168,10,0,2,70,0],
            ['Ago-15',3462,736,1182,131,123,0,0,62,0],
        ]);
    
        var options2 = {
            titleTextStyle: {color:'white'},
            backgroundColor: {fill: 'transparent'},
            chartArea: {width:'85%',height:'65%'},
            vAxis: {textStyle:{color:'white'}},
            legend:{textStyle: {color: 'white'}, position: 'bottom'},
            height: 350,
            hAxis: {textStyle:{color:'white'}},
            seriesType: 'bars',
            series: {8 : {type: 'line'}}
        };
    
        var chart2 = new google.visualization.ComboChart(document.getElementById('semester-1-chart'));
        chart2.draw(data2, options2);       
    }
    google.load("visualization", "1", {packages:["corechart","bar"]});
    google.setOnLoadCallback(drawCharts);
    

    我想得越多,这应该没什么区别。让我知道它是否破产,我会删除它而不是减少投票。

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2015-12-21
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多