繁体   English   中英

在Google Charts中将setColumns()与JSON表一起使用

[英]Using setColumns() in Google Charts with a JSON table

Google Charts API中是否有类似于setColumns()的函数可用于JSON数据?

我试图通过将数据组合到一个数组中,然后向Google图表指定要使用的列,来最大程度地减少PHP调用。

使用下面的代码,我得到该函数不存在的错误:

var data = new google.visualization.DataTable(jsonData);
data.setColumns([0,1,2]);

编辑:

这是每个文件的相关部分。

Javascript文件:

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
    var jsonData = $.ajax({
      url: "getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);

data.setColumns([0,1,2]);

var options = {
    width: chartWidth,
    height: chartHeight,
    backgroundColor: '#FFFFFF',
    legend:{
        textStyle:{color:'black'}
    },

    hAxis: {
      title: 'Time (EST)',
      baselineColor: 'black',
      textStyle:{color:'black',fontSize:'9'},
      slantedText: true,
      slantedTextAngle:45,
      titleTextStyle:{color:'black'}
    },

    vAxis: {
      title: 'Temperature(F)',
      baselineColor: 'black',
      textStyle:{color:'black'},
      titleTextStyle:{color:'black'}
    },

    colors: ['#FF0000', '#005F00']

  };

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.LineChart(document.getElementById('Tchart_div'));
  chart.draw(data, options);

相关的PHP代码:

// Build an array thats Google Charts Readable ... ... ...
$table=array();
$table['cols'] = array(
    // Chart Labels (i.e. column headers)
    array('label' => 'dateTime', 'type' => 'string'),
    array('label' => 'Temp', 'type' => 'number'),
    array('label' => 'Dew', 'type' => 'number'),
    array('label' => 'Heat Index', 'type' => 'number'),
    array('label' => 'Wind Chill', 'type' => 'number'),
    array('label' => 'Pressure', 'type' => 'number'),
    array('label' => 'Radiation', 'type' => 'number'),
    array('label' => 'UV Index', 'type' => 'number'),
    array('label' => 'WindSpeed', 'type' => 'number'),
    array('label' => 'WindGustSpeed', 'type' => 'number')   
);

$rows = array();

while($r = mysql_fetch_assoc($query))
{
    $mdat = $r['dateTime'];
    if ( ($mdat % 900) == 0) // If it's within our interval, save the data
    {
        // Date/Time
        $tdat = new DateTime("@$mdat");
        $tdat->setTimeZone(new DateTimeZone('America/New_York'));
        $rdat = $tdat->format('H:i');

        //Temp/Dew
        $Tdat = $r['outTemp'];
        $Tdat = number_format($Tdat, 2, '.', '');
        $Ddat = $r['dewpoint'];
        $Ddat = number_format($Ddat, 2, '.', '');

        //Heat Index/Wind Chill
        $HIdat = $r['heatindex'];
        $HIdat = number_format($pdat, 2, '.', '');
        $WCdat = $r['windchill'];
        $WCdat = number_format($wdat, 2, '.', '');

        //Pressure
        $Pdat = $r['barometer'];
        if ($Pdat == 0)
        {
            $Pdat = 29.92;
        }
        $Pdat = $Pdat * 33.8637526;
        $Pdat = number_format($Pdat, 2, '.', '');

        //Solar
        $RADdat = $r['radiation'];
        $RADdat = number_format($RADdat, 2, '.', '');
        $Udat = $r['UV'];

        //Wind
        $Wdat = $r['windSpeed'];
        $Wdat = number_format($Wdat, 2, '.', '');
        $Gdat = $r['windGust'];

        // Now save everything in an array...
        $temp = array();
        $temp[] = array('v' => (string) $rdat);  //Time
        $temp[] = array('v' => (float) $Tdat);   //Temp
        $temp[] = array('v' => (float) $Ddat);   //Dewp
        $temp[] = array('v' => (float) $HIdat);  //Heat Index
        $temp[] = array('v' => (float) $WCdat);  //Wind Chill
        $temp[] = array('v' => (float) $Pdat);   //Pressure
        $temp[] = array('v' => (float) $RADdat); //Solar Radiation
        $temp[] = array('v' => (float) $Udat);   //UV Index
        $temp[] = array('v' => (float) $Wdat);   //Wind Speed
        $temp[] = array('v' => (float) $Gdat);   //Wind Gusts


        // Make the previous array a row in this new array
        $rows[] = array('c' => $temp);
    }
}

$table['rows'] = $rows; // Build a table out of the rows array
$jsonTable = json_encode($table); //encode the table into the json format

简而言之,我想告诉Charts API仅从JSON表中选择某些字段

代替data.setColumns([0,1,2]); 您应该使用setView

data.setView({columns:[0,1,2]});

您可能正在寻找DataView Class setColumns函数 在这种情况下,以下示例演示了如何在Google图表中设置可见列:

 google.load("visualization", "1.1", { packages: ["table"] }); google.setOnLoadCallback(drawTable); function drawTable() { getWeatherData(function (data) { var table = new google.visualization.Table(document.getElementById('table_div')); table.draw(data, { showRowNumber: true, width: '100%', height: '100%' }); }); } function getWeatherData(complete) { $.getJSON('https://gist.githubusercontent.com/vgrem/2d1436388ae8872f149d/raw/7193623c50cf2e093989391182aa67aaf8fdad2b/WeatherData.json', function (json) { var dataTable = new google.visualization.DataTable(json); var dataView = new google.visualization.DataView(dataTable); dataView.setColumns([0,1,2]); complete(dataView); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <div id="table_div"></div> 

WeatherData.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM