虚线google chart json

时间:2013-07-12 04:24:21

标签: json google-visualization

我正在使用谷歌图表创建折线图。我在下面用谷歌搜索这些代码。 如何将线型(虚线)的属性添加到这些代码行?

$sth = mysql_query("SELECT * FROM table");

$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

    array('label' => 'Month', 'type' => 'string'),
    array('label' => 'a', 'type' => 'number'),
    array('label' => 'b', 'type' => 'number'),
    array('label' => 'c', 'type' => 'number'),
    array('label' => 'd', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();

    $temp[] = array('v' => (string) $r['Month']); 

    // Values of each slice
    $temp[] = array('v' => $r['a']);

    // Values of each slice
    $temp[] = array('v' => $r['b']);

    // Values of each slice
    $temp[] = array('v' => $r['c']);

    // Values of each slice
    $temp[] = array('v' => $r['d']); 
    $rows[] = array('c' => $temp);

}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;

2 个答案:

答案 0 :(得分:0)

使用certainty role可以实现虚线。根据你想要的线条破灭的方式,你只需要添加一个合适的" true / false"具有每个点的值的列,其中列角色被指定为'确定性'如下例所示。

  google.load('visualization', '1.1', {'packages':['corechart']});
  google.setOnLoadCallback(drawChart_C7);

  function drawChart_C7() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Year');
    data.addColumn('number', 'Sales');
    data.addColumn({type: 'boolean', role: 'certainty'});
    data.addColumn('number', 'Expenses');
    data.addColumn({type: 'boolean', role: 'certainty'});
    data.addRows([
      ['2004', 1000, false, 400, true],
      ['2005', 1170, true, 460, true],
      ['2006', 660, true, 1120, false],
      ['2007', 1030, true, 540, false]
    ]);

    var chart = new google.visualization.LineChart(document.getElementById('chart_C7'));
    chart.draw(data, {width: 400, height: 240, legend:'top'});
  }

答案 1 :(得分:0)

非常感谢!!我把我的PHP编码放在你的代码中,它可以正常工作。

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Month');
    data.addColumn('number', 'a');
    data.addColumn('number', 'b');
    data.addColumn({type: 'boolean', role: 'certainty'});
    data.addRows([

        <?php
        $sql="SELECT * FROM table";
        $result=mysql_query($sql);

        while($row=mysql_fetch_array($result)){

        $id= $row['id'];
        $month=$row['Month'];
        $a=$row['a'];
        $b=$row['b'];
        if ($a=="NULL"){$a = "";}
        if ($b=="NULL"){$b = "";}
        echo "['" . $month . "'," . $a . "," . $b . "," . "false" . "]";
        echo ",";

        }
        ?>  
    ]);

    var options = {
        title: 'Company Performance'
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }