使用下拉菜单选项在不同的 Google 图表之间切换

Change between different Google Charts using a drop down menu selection

本文关键字:Google 之间 下拉菜单 选项      更新时间:2023-09-26

>我需要帮助编写我的HTML页面,以便当您从下拉菜单中选择选项"C152"时(请参阅下面的代码),Google图表将更改为C152图表,当您从下拉菜单中选择"C172"时,图表将更改为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>

不是点击选项,而是在选择标签上使用onchange。
获取所选值并将其传递给 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);
    }
  }

代码笔 http://codepen.io/nagasai/pen/RRjLxL

相关文章: