1

この例では、JavaScript を使用して rPlot にツールチップを追加する方法 を示しました 。ライブラリ/ハイチャート/examples.R

rPlot に hPlot と同様の on.click イベントを実行させたいのですが、rPlot/polycharts を使用してそれを割り当てる適切な方法を見つけられませんでした。

ポリチャートの例 (ツールチップの適用に成功):

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test1, 
       type = 'point',
      point = list(events = list(click = "#!function(item){ alert( 'x: ' + item.x + 
       ' y: ' + item.y + ' id: ' + item.id); }!#")),
       tooltip = "#!function(item){ return 'x: ' + item.x + 
       ' y: ' + item.y + ' id: ' + item.id }!#")
p

HighCharts の例 (アラーム ポップアップの作成に成功):

 require(rCharts)
 a <- hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex','Exer')), type = 'bar', group = 'Sex', group.na = 'NA\'s')
a$plotOptions(bar = list(cursor = 'pointer', point = list(events = list(click = "#! function() { alert ('Category: '+ this.category +', value: '+ this.y); } !#"))))
a

以下は、プロットするがクリックイベントをトリガーしない現在のコードです。

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test1, 
       type = 'point',
       point = list(events = list(click = "#! function() {alert('testMessagePleaseWork');} !#")),
       tooltip = "#!function(item){ return 'x: ' + item.x + ' y: ' + item.y + ' id: ' + item.id }!#")
p

現在 rCharts v0.4.2 を使用しています: パッケージ: rCharts タイプ: パッケージ タイトル: Polycharts.js を使用したインタラクティブ チャート バージョン: 0.4.2 日付: 2013-04-09

4

1 に答える 1

2

すべての JavaScript チャート ライブラリには、クリック イベントなどを処理するための独自のメカニズムがあります。したがって、一般に、あるライブラリから別のライブラリにアプローチをコピーしようとしてもうまくいきません。幸いなことに、polychartクリック ハンドラーをサポートするメカニズムがあります。これは最小限の例です。私は基本的にafterScript、ハンドラーをチャートに追加することを使用してjavascriptスニペットを追加しています。インタラクション ハンドラーのポリチャートのドキュメントは非常に薄いため、より意味のあることを行うには、ソース コードに飛び込むか、サンプルを確認する必要があります。

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, 
  data = test1, 
  type = 'point',
  tooltip = "#!function(item){ return 'x: ' + item.x + ' y: ' + item.y + ' id: ' + item.id }!#"
)
p$set(dom = 'chart1')
p$setTemplate(afterScript = "
  <script>
   graph_chart1.addHandler(function(type, e){
      var data = e.evtData
      if (type === 'click'){
        alert('You clicked on' + data.x.in[0] + ',' + data.y.in[0])
      }
   })
  </script>    
")

これを機能させるには、のdevブランチをインストールする必要がありますrCharts

install.packages('base64enc') # dependency
devtools::install_github("ramnathv/rCharts@dev")
于 2014-04-18T14:21:50.820 に答える