【问题标题】:Change between different Google Charts using a drop down menu selection使用下拉菜单选择在不同的 Google 图表之间进行更改
【发布时间】:2016-07-12 18:59:51
【问题描述】:

我需要帮助编码我的 HTML 页面,以便当您从下拉菜单中选择“C152”选项时(参见下面的代码),Google 图表将更改为 C152 图表,而当您从下拉菜单,图表将变为 C172 图表。基本上,图表将根据下拉菜单中的选择而改变。 (每个图表都有一组不同的数据点和选项。)我已经做了一段时间的研究,但还没有找到我想要的东西来完成这项工作。我希望有人能指出我正确的方向。这是我的代码示例。提前致谢

<!DOCTYPE html>
<html>
<head>

<th>
<select name='select1' >
  <option selected disabled>Choose</option> 
  <option onclick="c152()" value="c152">C152</option>
  <option onclick="c172()" value="c172">C172</option>
</select></th>

<head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load("current", {packages:["corechart"]});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var datac152 = google.visualization.arrayToDataTable
            ([['X', 'Y'],
              [31, 1000],
              [31, 1350],
              [32.65, 1670],
              [36.5, 1670],
              [36.5, 1000]
        ]);
        var datac172 = google.visualization.arrayToDataTable
            ([['X', 'Y'],
              [35, 1500],
              [35, 1950],
              [38.65, 2300],
              [47.3, 2300],
              [47.3, 1500]
        ]);


        var optionsc152 = {
          legend: 'none',
          hAxis: {title: 'Center of Gravity (inches)', minValue: 30, maxValue: 38 },
          vAxis: {title: "Total Weight (lbs)"},
          axes: {
            y: {
                all: {
                    range: {
                        max: 1800,
                        min: 1000
                    }
                }
            }
        },

          colors: ['#009933'],
          pointSize: 0,
          title: 'Cessna 152 Load Limits',
          backgroundColor: '#eeeeee',
          pointShape: 'circle'
       };

        var optionsc172 = {
          legend: 'none',
          hAxis: {title: 'Center of Gravity (inches)', minValue: 34, maxValue: 48 },
          vAxis: {title: "Total Weight (lbs)"},
          axes: {
            y: {
                all: {
                    range: {
                        max: 2300,
                        min: 1500
                    }
                }
            }
        },

          colors: ['#009933'],
          pointSize: 0,
          title: 'Cessna 172 Load Limits',
          backgroundColor: '#eeeeee',
          pointShape: 'circle'
       };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(datac152, optionsc152);
    }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 963px; height: 500px;"></div>
  </body>
</body>

</html>

【问题讨论】:

    标签: javascript html drop-down-menu charts google-visualization


    【解决方案1】:

    在选择标签上使用 onchange 而不是 onclick 选项。
    获取选定的值并将其传递给 chart.draw()

    HTML:

    <html>
    
    <head>
    
      <th>
        <select id="chart" name='select1' onchange="change()">
      <option selected disabled>Choose</option> 
      <option value="c152">C152</option>
      <option value="c172">C172</option>
    </select></th>
    
      <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    
      </head>
    
      <body>
        <div id="chart_div" style="width: 963px; height: 500px;"></div>
      </body>
      </body>
    
    </html>
    

    JS:

      google.charts.load("current", {
        packages: ["corechart"]
      });
    
      function change() {
        var listbox = document.getElementById("chart");
        var selIndex = listbox.selectedIndex;
        var selValue = listbox.options[selIndex].value;
        var selText = listbox.options[selIndex].text;
        console.log(selValue);
    
        google.charts.setOnLoadCallback(drawChart);
    
        function drawChart(x, y) {
          var datac152 = google.visualization.arrayToDataTable([
            ['X', 'Y'],
            [31, 1000],
            [31, 1350],
            [32.65, 1670],
            [36.5, 1670],
            [36.5, 1000]
          ]);
          var datac172 = google.visualization.arrayToDataTable([
            ['X', 'Y'],
            [35, 1500],
            [35, 1950],
            [38.65, 2300],
            [47.3, 2300],
            [47.3, 1500]
          ]);
    
          var optionsc152 = {
            legend: 'none',
            hAxis: {
              title: 'Center of Gravity (inches)',
              minValue: 30,
              maxValue: 38
            },
            vAxis: {
              title: "Total Weight (lbs)"
            },
            axes: {
              y: {
                all: {
                  range: {
                    max: 1800,
                    min: 1000
                  }
                }
              }
            },
    
            colors: ['#009933'],
            pointSize: 0,
            title: 'Cessna 152 Load Limits',
            backgroundColor: '#eeeeee',
            pointShape: 'circle'
          };
    
          var optionsc172 = {
            legend: 'none',
            hAxis: {
              title: 'Center of Gravity (inches)',
              minValue: 34,
              maxValue: 48
            },
            vAxis: {
              title: "Total Weight (lbs)"
            },
            axes: {
              y: {
                all: {
                  range: {
                    max: 2300,
                    min: 1500
                  }
                }
              }
            },
    
            colors: ['#009933'],
            pointSize: 0,
            title: 'Cessna 172 Load Limits',
            backgroundColor: '#eeeeee',
            pointShape: 'circle'
          };
    
          var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
          if (selValue == 'c152') {
            x = datac152;
            y = optionsc152;
          }
          if (selValue == 'c172') {
            x = datac172;
            y = optionsc172;
          }
          chart.draw(x, y);
        }
    
      }
    

    Codepen-http://codepen.io/nagasai/pen/RRjLxL

    【讨论】:

    • 非常感谢,这非常有效!另外,感谢您的快速回复!
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多