中心对齐Google Gauge Chart

时间:2017-03-21 12:58:02

标签: css charts google-visualization flexbox gauge

我正在努力让Google Gauge Chart对齐。

我想要一个带有居中标尺字符的行和一个带有居中折线图的行。

我尝试了各种选项并使用“display:inline-block”但不起作用。 带折线图的行符合预期。

<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

  <script type="text/javascript">
      google.charts.load('current', {'packages':['gauge', 'corechart']});
      google.charts.setOnLoadCallback(drawCharts);

      function drawCharts() {
        drawChart();
        drawGauges();
      }

      function drawGauges() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Memory', 80],
          ['CPU', 55],
        ]);

        var w = $( window ).width();
        var x = Math.floor(w * 0.3);
        console.log("width: " + w + ", x = " + x);
        var h = $( window ).height();
        var y = Math.floor(h * 0.3)
        console.log("height: " + h + ", y = " + y);

        var options = {
            redFrom: 90, redTo: 100,
            yellowFrom:75, yellowTo: 90,
            minorTicks: 5,
            width: x,
            height: y,
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

        chart.draw(data, options);
      }

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }

    $(window).resize(function(){
        drawCharts();
    });
    </script>

    <style>
    .container {
            display: flex;
            flex-flow: row wrap;
        justify-content: center;
        align-items: center;
    }

    .row {
            height:30%;
        width: 100%;
    }

        #chart_div {
        display: inline-block
        margin: 0 auto;
    }
   </style>

  </head>
  <body>
    <div class="container">
        <div class="row">
            <div id="chart_div"></div>
        </div>
        <div class="row">
            <div id="curve_chart" ></div>
        </div>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

text-align: center;添加到.container

.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  align-items: center;
  text-align: center;
}

请参阅以下工作代码段...

&#13;
&#13;
google.charts.load('current', {
  callback: drawCharts,
  packages:['corechart', 'gauge']
});

function drawCharts() {
  drawChart();
  drawGauges();
}

function drawGauges() {

  var data = google.visualization.arrayToDataTable([
    ['Label', 'Value'],
    ['Memory', 80],
    ['CPU', 55],
  ]);

  var w = $( window ).width();
  var x = Math.floor(w * 0.3);
  console.log("width: " + w + ", x = " + x);
  var h = $( window ).height();
  var y = Math.floor(h * 0.3)
  console.log("height: " + h + ", y = " + y);

  var options = {
      redFrom: 90, redTo: 100,
      yellowFrom:75, yellowTo: 90,
      minorTicks: 5,
      width: x,
      height: y,
  };

  var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

  chart.draw(data, options);
}

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Year', 'Sales', 'Expenses'],
    ['2004',  1000,      400],
    ['2005',  1170,      460],
    ['2006',  660,       1120],
    ['2007',  1030,      540]
  ]);

  var options = {
    title: 'Company Performance',
    curveType: 'function',
    legend: { position: 'bottom' }
  };

  var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

  chart.draw(data, options);
}

$(window).resize(function(){
    drawCharts();
});
&#13;
.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.row {
  height: 30%;
  width: 100%;
}

#chart_div {
  display: inline-block;
  margin: 0 auto;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="container">
    <div class="row">
        <div id="chart_div"></div>
    </div>
    <div class="row">
        <div id="curve_chart" ></div>
    </div>
</div>
&#13;
&#13;
&#13;

相关问题