dongzhan9100 2014-01-13 19:37
浏览 61
已采纳

无法获得角色:在Google Charts中使用JSON时,使用柱形图的“样式”

By following Google's documentation, I have been able to get a column chart to work fine with static data. However all the columns are the same color and I want to use role: 'style' to set the color as described here:

var data = google.visualization.arrayToDataTable([
['Element', 'Density', { role: 'style' }],
    ['Copper', 8.94, '#b87333'],            // RGB value
['Silver', 10.49, 'silver'],            // English color name
['Gold', 19.30, 'gold'],
['Platinum', 21.45, 'color: #e5e4e2' ], // CSS-style declaration
  ]); 

The problem I am having is that I can't figure out how to get this to work with JSON where the data is dynamic (it is pulled from MySQL using a script).

This is my code that does not display anything:

echo <<< END
{
"cols": [
  {"id":"","label":"Topping","pattern":"","type":"string"},
  {"id":"","label":"Slices","pattern":"","type":"number"},
  {type:'string', role:'style'}
],
"rows": [ 
END;

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

  echo <<< END
  {"c":[{"v":"
  END;
  echo $row['DEVICEID'];
  echo <<< END
    ","f":null},{"v":
  END;
  echo $row['minutecount'];
  echo <<< END
    ,"f":null},{"v":"gold","f":null}]},
  END;
}

These are the two things I have added to try and get it to work:

{type:'string', role:'style'}
...
,{"v":"gold","f":null}

I can't seem to find a JSON example so the above declarations may be incorrect. Right now I have all them hardcoded to 'gold', but once that works I will be changing the colors dynamically.

Without these, the chart displays fine (with dynamic data) but with all the same color.

  • 写回答

1条回答 默认 最新

  • doumeng1897 2014-01-13 19:46
    关注

    I figured out the problem, it was this line:

     {type:'string', role:'style'}
    

    When I changed it to the following, everything worked.

     {"type":"string", "role":"style"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000