将json文件中的数据格式化为树形图值图表

时间:2016-09-27 07:22:02

标签: javascript json

也许不久前,我已经问过这个了。但我仍然会收到错误并混淆如何解决这个问题。

我有代码,我想将其更改为

var data = google.visualization.arrayToDataTable([
      ['Location', 'Parent', 'Size', 'Color'],
      ['Jawa', null, 0, 0],
      ['Jawa Tengah', 'Jawa', 0, 0],
      ['Jawa Timur', 'Jawa', 0, 0],
      ['Jawa Barat', 'Jawa', 0, 0],
      ['SAL01', 'Jawa Barat', 89, 50],
      ['SAL02', 'Jawa Tengah', 77, 40],
      ['SAL03', 'Jawa Tengah', 71, 28]
    ]);

这是我的代码

function drawChart(){
        var dataFromPHP = <?php echo file_get_contents('clientdata.json'); ?>;
        $dataFromPHP = file_get_contents('clientdata.json');
        $json = json_decode($dataFromPHP, TRUE);

        // console.log(dump_dataFromPHP);
        $provinsi = 'SELECT provinsi , LEFT(provinsi, 4) AS lala FROM data_client';
        $query = 'SELECT provinsi ,kode_sales, gaji, count(kode_sales) AS T_nasabah FROM data_client GROUP BY kode_sales';
                    $exec = mysqli_query($con,$query);
        $execProv = mysqli_query($con,$provinsi);                           
                    var data = google.visualization.arrayToDataTable([
        ['Location', 'Parent' , 'Size', 'Color'],   

        echo "['".$row['lala']."', null, 0, 0],";
        while($row = mysqli_fetch_array($execProv)){ 
        echo "['".$row['provinsi']."','".$row['lala']."',0,0],";
        }
        while($row = mysqli_fetch_array($exec)){ 
        echo "['".$row['kode_sales']."','".$row['provinsi']."',".$row['gaji'].",".$row['T_nasabah']."],";
        }];

这是我的clientdata.json文件

[
{
    "kode_client": "CLI01",
    "nama": "Edo",
    "tanggal_lahir": "01\/05\/1967",
    "alamat": "Nusa Hijau",
    "telp": "085634234",
    "produk": "Titanium",
    "gaji": "5000000",
    "cabang": "Bandung",
    "provinsi": "Jawa Barat",
    "kode_sales": "SAL01"
},
{
    "kode_client": "CLI02",
    "nama": "Santoso Imam",
    "tanggal_lahir": "15\/12\/1979",
    "alamat": "Padasuka",
    "telp": "08513087645",
    "produk": "Investra Link",
    "gaji": "4500000",
    "cabang": "Cimahi",
    "provinsi": "Jawa Tengah",
    "kode_sales": "SAL02"
},
{
    "kode_client": "CLI03",
    "nama": "Mukidi",
    "tanggal_lahir": "01\/01\/1991",
    "alamat": "Mujihat",
    "telp": "085213123",
    "produk": "Titanium",
    "gaji": "12500000",
    "cabang": "Bandung",
    "provinsi": "Jawa Tengah",
    "kode_sales": "SAL03"
}
  ]

我想用Google图表输入我的Treemap图表的格式值。在此之前,请帮助我:&#39;)

1 个答案:

答案 0 :(得分:0)

是的,最后我的树形图显示在浏览器中!!

var dataFromPHP = <?php echo file_get_contents('clientdata.json'); ?>;
var newData = [];
function drawChart(){
                    var data = google.visualization.arrayToDataTable([
                        ['Location', 'Parent' , 'Size', 'Color'],
                        newData[0] = [$provinsi, null , 0, 0],

                    ]);

        console.log(data);

        tree = new google.visualization.TreeMap(document.getElementById('chart_div'));

        tree.draw(data, {
          minColor: '#f00',
          midColor: '#ddd',
          maxColor: '#0d0',
          headerHeight: 15,
          fontColor: 'black',
          showScale: true

        });
        chart.draw(data, options);      
        }

        });

但是,值的格式仍然不完全正确。