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 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退