Google Charts ComboChart双纵轴min/max设置无效问题求助
解决Google Charts ComboChart双纵轴viewWindow不生效的问题
问题出在你使用了单数的vAxis来配置双纵轴,Google Charts中针对多纵轴的配置需要用复数的vAxes,单数的vAxis仅用于设置所有纵轴的通用属性,无法单独指定每个轴的viewWindow。
修改后的代码如下,核心是把vAxis替换为vAxes:
google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('date', 'Time'); data.addColumn('number', 'Power (kW)'); data.addColumn('number', 'Rate (p/kWh)'); data.addRows([ [new Date(2022,11,12,0,0,0), 0.081, 41.33], [new Date(2022,11,12,0,30,0), 0.079, 41.33], [new Date(2022,11,12,1,0,0), 0.066, 11.43], [new Date(2022,11,12,1,30,0), 0.776, 11.43], [new Date(2022,11,12,2,0,0), 0.825, 11.43], [new Date(2022,11,12,2,30,0), 0.061, 11.43], [new Date(2022,11,12,3,0,0), 0.091, 11.43], [new Date(2022,11,12,3,30,0), 0.057, 11.43], [new Date(2022,11,12,4,0,0), 0.077, 11.43], [new Date(2022,11,12,4,30,0), 0.097, 11.43], [new Date(2022,11,12,5,0,0), 0.057, 41.33], [new Date(2022,11,12,5,30,0), 0.078, 41.33], [new Date(2022,11,12,6,0,0), 0.067, 41.33], [new Date(2022,11,12,6,30,0), 0.064, 41.33], [new Date(2022,11,12,7,0,0), 0.138, 41.33], [new Date(2022,11,12,7,30,0), 0.191, 41.33], [new Date(2022,11,12,8,0,0), 0.167, 41.33], [new Date(2022,11,12,8,30,0), 0.276, 41.33], [new Date(2022,11,12,9,0,0), 0.238, 41.33], [new Date(2022,11,12,9,30,0), 0.199, 41.33], [new Date(2022,11,12,10,0,0), 0.155, 41.33], [new Date(2022,11,12,10,30,0), 0.205, 41.33], [new Date(2022,11,12,11,0,0), 0.156, 41.33], [new Date(2022,11,12,11,30,0), 0.154, 41.33], [new Date(2022,11,12,12,0,0), 0.145, 41.33], [new Date(2022,11,12,12,30,0), 0.261, 41.33], [new Date(2022,11,12,13,0,0), 0.222, 41.33], [new Date(2022,11,12,13,30,0), 0.192, 41.33], [new Date(2022,11,12,14,0,0), 0.135, 41.33], [new Date(2022,11,12,14,30,0), 0.165, 41.33], [new Date(2022,11,12,15,0,0), 0.163, 41.33], [new Date(2022,11,12,15,30,0), 0.168, 41.33], [new Date(2022,11,12,16,0,0), 0.225, 41.33], [new Date(2022,11,12,16,30,0), 0.2, 41.33], [new Date(2022,11,12,17,0,0), 0.19, 41.33], [new Date(2022,11,12,17,30,0), 0.209, 41.33], [new Date(2022,11,12,18,0,0), 1.282, 41.33], [new Date(2022,11,12,18,30,0), 0.361, 41.33], [new Date(2022,11,12,19,0,0), 0.138, 41.33], [new Date(2022,11,12,19,30,0), 0.122, 41.33], [new Date(2022,11,12,20,0,0), 0.154, 41.33], [new Date(2022,11,12,20,30,0), 0.181, 41.33], [new Date(2022,11,12,21,0,0), 0.147, 41.33], [new Date(2022,11,12,21,30,0), 0.189, 41.33], [new Date(2022,11,12,22,0,0), 0.17, 41.33], [new Date(2022,11,12,22,30,0), 0.14, 41.33], [new Date(2022,11,12,23,0,0), 0.163, 41.33], [new Date(2022,11,12,23,30,0), 0.134, 41.33], [new Date(2022,11,13,0,0,0), 0.197, 41.33] ] ); var options = { title: 'Company Performance', backgroundColor: '#EAEEFA', // 把vAxis改为vAxes(复数) vAxes: { 0: {viewWindow:{min: 0.0, max: 15}}, 1: {viewWindow:{min: 0.0, max: 50}} }, series: { 0: {type: 'bars', targetAxisIndex:0 }, 1: {type: 'line', targetAxisIndex:1 } } }; var chart = new google.visualization.ComboChart(document.getElementById('chart_div')); chart.draw(data, options); }
修改后,vAxes的索引和series里的targetAxisIndex一一对应,就能分别控制两个纵轴的最值范围,不需要使用已弃用的viewWindowMode:'explicit'。
内容的提问来源于stack exchange,提问作者Hugh Foster
相关产品推荐
相关产品推荐

