轴#0的数据列不能是谷歌图表中的字符串错误类型

时间:2012-02-08 14:16:27

标签: json google-visualization

我尝试使用PHP在服务器端填充谷歌图表数据表。我正确地获得了JSON文件,但图表不显示在客户端应用程序中。我收到错误 - 轴#0的数据列不能是字符串。我的编码在这里。

从数据库中获取数据后

$colarray=array(array("id"=>"","label"=>"userid","pattern"=>"","type"=>"number"),array("id"=>"","label"=>"name","pattern"=>"","type"=>"string"));

  $final=array();
    for($i=0;$i<$rows;$i++) 
    {
     $id[$i]=pg_fetch_result($res1,$i,'id');
     $name[$i]=pg_fetch_result($res1,$i,'name');
     $prefinal[$i]=array("c"=>array(array("v"=>$name[$i]),array("v"=>$name[$i])));
     array_push($final,$prefinal[$i]);
    }


    $table['cols']=$colarray;
    $table['rows']=$final;
    echo json_encode($table);

我的输出Json:

{
  "cols":[
    {"id":"","label":"userid","pattern":"","type":"number"},
    {"id":"","label":"name","pattern":"","type":"string"}
   ],
  "rows":[
    {"c":[{"v":"101"},{"v":"Aircel"}]},
    {"c":[{"v":"102"},{"v":"Srini"}]},
    {"c":[{"v":"103"},{"v":"Tamil"}]},
    {"c":[{"v":"104"},{"v":"Thiyagu"}]},
    {"c":[{"v":"105"},{"v":"Vasan"}]},
    {"c":[{"v":"107"},{"v":"Senthil"}]},
    {"c":[{"v":"108"},{"v":"Sri"}]},
    {"c":[{"v":"109"},{"v":"Docomo"}]},
    {"c":[{"v":"106"},{"v":"Innodea"}]}
    ]
}

如何解决这个问题?

6 个答案:

答案 0 :(得分:10)

延长@ sajal的准确答案:更改代码的最后一行:

echo json_encode($table);

为:

echo json_encode($table, JSON_NUMERIC_CHECK);

这将告诉json_encode识别数字并禁止将它们用引号括起来(自PHP 5.3.3起可用)。 http://php.net/manual/en/json.constants.php#constant.json-numeric-check

答案 1 :(得分:6)

您将userid的类型指定为number ...但是传递字符串..这会导致问题。

我因为相反的问题而浪费了30分钟......

您的输出json应如下所示: -

{
  "cols":[
    {"id":"","label":"userid","pattern":"","type":"number"},
    {"id":"","label":"name","pattern":"","type":"string"}
   ],
  "rows":[
    {"c":[{"v":101},{"v":"Aircel"}]},
    {"c":[{"v":102},{"v":"Srini"}]},
    {"c":[{"v":103},{"v":"Tamil"}]},
    {"c":[{"v":104},{"v":"Thiyagu"}]},
    {"c":[{"v":105},{"v":"Vasan"}]},
    {"c":[{"v":107},{"v":"Senthil"}]},
    {"c":[{"v":108},{"v":"Sri"}]},
    {"c":[{"v":109},{"v":"Docomo"}]},
    {"c":[{"v":106},{"v":"Innodea"}]}
    ]
}

答案 2 :(得分:1)

在BarChart上,其中一列(第二列)必须是数字。这可能会导致此错误消息。

答案 3 :(得分:1)

在你的drawChart()函数中,你可能正在使用google.visualization.arrayToDataTable,这不允许任何空值。请明确使用addColumn函数

答案 4 :(得分:0)

如果数据格式应该是:

data: [
    ["string", "string"], //first Column
    ["string1", number],
    ["string2", number],
    ["string3", number],
]

然后你可以克服这个错误。

答案 5 :(得分:0)

当您从控制器传递数据时,您需要这样做:举个例子,我有控制器,我通过 group by 通过它发送数据。

控制器: \DB::statement("SET SQL_MODE=''");//这是在查询之前使用它的技巧

private int[] rxData = new int[0xFFFFFF];

private int rxLength = 0;

public void socketReceived() 
{       
    byte[] rxBuffer = new byte[0xFFFFFF];
    
    int startLen = 0, dataLen = 0;

    while (isConnected) 
    {
        try
        {          
            startLen = getSeqeunce(START_CODE);
    
            Console.WriteLine("startLen:" + startLen);
    
            if (startLen != -1)
            {
                Console.WriteLine("rx start");
                this.Invoke(new Function(updateTextBox), "Measuring");
                readFully(rxBuffer, 4); 
                dataLen = (rxBuffer[0] << 24) + (rxBuffer[1] << 16) + (rxBuffer[2] << 8) + rxBuffer[3];
                //rxLength = dataLen / 2;
                rxLength  = Convert.ToInt32(tbIP1.Text);

                Console.WriteLine("rxLength:" + rxLength);
                readFully(rxBuffer, dataLen);

                this.Invoke(new Function(updateTextBox), "Converting");
                for (int i = 0; i < rxLength; i++)
                {                      
                    rxData[i] = (short)((rxBuffer[i * 2]) + rxBuffer[i * 2 + 1] * 256);
                        
                    Console.WriteLine("{0}: {1}", i, rxData[i]);

                    curveSensor.AddPoint(i, rxData[i]);
                }
    
                this.Invoke(new Function(updateTextBox), "Displaying"); 
    
                zgSensor.AxisChange();
    
                zgSensor.Invalidate();
    
                this.Invoke(new Function(updateTextBox), "Measurement completed"); 
    
                Console.WriteLine("rx end");
                    
                this.Invoke(new Action(delegate ()
                {
                    btnStart.Enabled = true;
                    btnSave.Enabled = true;
                    btnCustom.Enabled = true;
                }));
    
                isComplete = true;
            }
        }
        catch
        {
        }
    }
}

private void updateTextBox(string str)
{
    lbGraphMessage.Text = str;
}

private void sendPacket(byte[] data, int size) 
{
    byte[] buffer = new byte[256];
    
    int checksum = 0, index = 0;

    try
    {
        if (isConnected)
        {
            buffer[index++] = 0x42;
            buffer[index++] = 0x4D;
            buffer[index++] = (byte)((size + 2) >> 8); 
            buffer[index++] = (byte)((size + 2) & 0xFF);
            for (int i = 0; i < size; i++)
            {
                checksum += data[i];
                buffer[index++] = data[i];
            }
            buffer[index++] = (byte)(checksum >> 8);
            buffer[index++] = (byte)(checksum & 0xFF);
            for (int i = 0; i<index; i++)
        {
            Console.Write("{0:X} ", buffer[i]);
            Console.Write("\n");
            ns.Write(buffer, 0, index);
        }
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
    }
}

private void readFully(byte[] buffer, int size) 
{
    int offset = 0;
    int readBytes;
    do
    {
        readBytes = ns.Read(buffer, offset, size - offset);
        offset += readBytes;
    } while ((readBytes > 0) && (offset < size));
}

private int getSeqeunce(int[] sequence) 
{
    int seqIndex = 0; 
    int c;
    for (int i = 0; i < 1024; i++) 
    {
        c = ns.ReadByte();

        //Console.WriteLine("readbyte:" + c);
        if (c == sequence[seqIndex])
        { 
            seqIndex++;
            if (seqIndex == sequence.Length) return i + 1;
        }
        else
        {
            seqIndex = 0;
        }
        return -1;
    }
}

如果没有 JSON_NUMERIC_CHECK,那么数据将是字符串数组,如果有 json check,数据将被转换为数字数组。

在 JSON 检查数据之前:

$Rspatients = DB::table('reports')
->select(
    DB::raw("day(created_at) as day"),
    DB::raw("Count(*) as total_patients"))

->orderBy("created_at")
->groupBy(DB::raw("day(created_at)"))
->get();


$result_patients[] = ['day','Patients'];
foreach ($Rspatients as $key => $value) {
$result_patients[++$key] = [$value->day,$value->total_patients];
}

    return view('Dashboard.index')
  ->with('result_patients',json_encode($result_patients,JSON_NUMERIC_CHECK));

JSON 检查数据后:

4: (2) ["24", "413"]
5: (2) ["25", "398"]