dongzhe6287 2015-10-08 16:03
浏览 60

在Google Charts中使用带有JSON表的setColumns()

Is there a function in the Google Charts API similar to setColumns() that will work on JSON data?

I am trying to minimize PHP calls by combining my data into one array and then specifying to Google Charts which columns to use.

Using the code below I get the error that the function does not exist:

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

EDIT:

Here are the relevant portions of each file.

Javascript File:

// 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);

Relevant PHP Code:

// 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

So in a nutshell, I want to tell the Charts API to only select certain fields from the JSON table

  • 写回答

2条回答 默认 最新

  • doudi8298 2015-10-09 13:05
    关注

    Instead of data.setColumns([0,1,2]);you should use setView.

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

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行