gvisScatterChart动态定义系列 [英] gvisScatterChart define series dynamically

查看:167
本文介绍了gvisScatterChart动态定义系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地创建了几个 gvisScatterChart s。我想定义每行的颜色,我可以使用series和color属性来完成这些操作。没有什么可以预定颜色的订单或固定编号。所以我想创建一个与我的颜色平行的属性数组,并将它放在 series = myColors



问题是gVis需要一个字符串,例如:

  series =[{color:'black',visibleInLegend:false}] ,

只要我使用粘贴函数gVis不再接受它们,只是将空白页面显示为图表。 (即使标记为使用\)



这是一个错误还是我做错了什么?

解决方案

这不是一个bug,就像在帮助中所说的那样,它需要一个JSON字符串,因此您需要构建JSON字符串。



<使用 RJSONIO ,您可以使用 toJSON



来构建JSON选项

  library(googleVis)
library(RJSONIO)
myColor< - 'grey'##我的动态颜色,在这里我修好了,但你可以阅读它,例如
##从图表配置文件或任何你想要的
isLegend< - TRUE ## a布尔值

myseriesOptions< - toJSON(list(list(color =例如

  Scatter2 < -  gvisScatterChart(women,
options = list(legend =none,
lineWidth = 2,pointSize = 0,
title =Women,vAxis ={title:'weight(lbs)'},
hAxis ={title:'height(in)'},
width = 300,height = 300,
series = myseriesOptions))

plot(Scatter2)



< PS:我们可以使用 fromJSON 来获取要构造的字符串的R形式,例如

  fromJSON({title:'mytitle'})## ouptut是一个列表
$ itl
NULL

cat(toJSON(list (title ='mytitle')))##我构建我的列表,并使用toJSON
##我得到我的原始json格式

{
title:mytitle
}


I am dynamically creating a couple of gvisScatterCharts. I want to define the colors of each line, which I can do using series and the color attribute. There is nothing like an order or a fix number by which I can predefine the colors. So I want to create an array of attributes parallel to my colors and just place it at series=myColors.

The problem is that gVis expects a string like:

   series="[{color: 'black', visibleInLegend: false}]", 

As soon as I create a string using the paste function gVis doesn't accept them any more and just shows a blank page as chart. (Even when marking the " using \")

Is this a bug or am I doing something wrong?

解决方案

This is not a bug, just as said in the help it expects a JSON string so you need to build JSON string.

Using RJSONIO you can build the JSON option using toJSON

library(googleVis)
library(RJSONIO)
myColor <- 'grey'   ## my dynamic color, here I fix but you can read it ,e.g
                    ## from a chart config file or whatever you want
isLegend <- TRUE    ## a boolean value 

myseriesOptions <- toJSON(list(list(color=myColor),list(visibleInLegend=isLegend)))

For example

Scatter2 <- gvisScatterChart(women, 
                             options=list(legend="none",
                                          lineWidth=2, pointSize=0,
                                          title="Women", vAxis="{title:'weight (lbs)'}",
                                          hAxis="{title:'height (in)'}", 
                                          width=300, height=300,
                                          series = myseriesOptions    ))

 plot(Scatter2)

PS : We can use fromJSON to get the R form of the string to construct, e.g

fromJSON("{title:'mytitle'}")        ## the ouptut is a list 
$itl
NULL

cat(toJSON(list(title='mytitle')))   ## I construct my list and I use toJSON
                                     ## I get my origin json form

{
 "title": "mytitle" 
}

这篇关于gvisScatterChart动态定义系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆