2014-12-27 7 views
7

내가 제공 한 예는 유혹 차트 enter image description here어떻게 주석

결과

library(dygraphs) 

dygraph(presidents, main = "Presidential Approval") %>% 
dyAxis("y", valueRange = c(0, 100)) %>% 
dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>% 
dyAnnotation("1965-1-1", text = "B", tooltip = "Vietnam") 

입니다 dygraphs

의 R 구현을 사용하려고하지 않고 dygraphs에 표시 툴팁을 얻을 수 있습니다 'A'는 '한국'이라는 도구 설명을 생성합니다.

나는 모든 점에 대해 툴팁을 사용하고 싶습니다. 요구 사항 - 최소 높이/너비 값으로 텍스트를 ""(으)로 설정하더라도 충분할 수 있습니다. 또한, 어떻게 그렇다면, 날짜와 툴팁 열

df <- data.frame(date=as.Date(c("1950-7-1","1965-1-1")),tooltip=c("Korea","Vietnam")) 

이 가능하고, 가진 파일에서 프로그래밍 dyAnnotations을 첨부 할 것인가? TIA

답변

2

좋아, 약속대로, 우리가 정보에 대한 전설을 사용하는 방법에 시작이다 달성 할 수있는 한 가지 방법입니다. 전설을 엄하게 덮어 씁니다. 이 동작은 범례를 원한다면 훨씬 더 정중하게 만들 수 있습니다. 또한 data.frame이라는 객체/해시를 제공하여 x을 조회하고 유익한 설명을 제공 할 수 있습니다.

크롬에 디버거를 열면 어떤 일이 일어나고 있는지 확인할 수 있습니다. debugger을 추가했습니다. 다음은

library(dygraphs) 

dyG = dygraph(presidents, main = "Presidential Approval") %>% 
    dyAxis("y", valueRange = c(0, 100)) 

# explore the legend route 
dyG %>% 
    dyCallbacks(
     highlightCallback = sprintf(
'function(e, x, pts, row) { 

// added to illustrate what is happening 
// remove once satisfied with your code 
debugger; 

var customLegend = %s 

// should get our htmlwidget 
e.target.parentNode.parentNode 
    .querySelectorAll(".dygraph-legend")[0] 
    .innerText = customLegend[row] + row; 
}' 
      ,# supply a vector or text that you would like 
      jsonlite::toJSON(rep('something here',length(as.vector(presidents)))) 
     ) 
    ) 

, 나는 전설에 추가하는 대신 대체하기 위해 변경되었습니다.

# explore the legend route 
# add to legend rather than replace 
dyG %>% 
    dyCallbacks(
    highlightCallback = sprintf(
     'function(e, x, pts, row) { 

     // added to illustrate what is happening 
     // remove once satisfied with your code 
     debugger; 

     var customLegend = %s 


     // should get our htmlwidget 
     var legendel = e.target.parentNode.parentNode 
     .querySelectorAll(".dygraph-legend")[0]; 

     // should get our htmlwidget 
     legendel.innerHTML = legendel.innerHTML + "<br>" + customLegend[row]; 
     }' 
      ,# supply a vector or text that you would like 
     jsonlite::toJSON(rep('something here',length(as.vector(presidents)))) 
    ) 
) 
+0

조금 생각한 후'innerHTML'을 쉽게 사용할 수 있었고 '
'을 바꾸는 대신 전설에 추가 할 수 있었기 때문에 제 답변에 – timelyportfolio

+0

을 추가했습니다. 특히 추가 라인을 쉽게 추가 할 수있는 옵션이있는 경우 – pssguy

2

Dygraphs 사용자 지정의 대부분은 CSS 스타일링에서 발생합니다. 예를 들어 here is how은 기본 툴팁 동작을 변경할 수 있습니다. 이를 염두에두고 Dygraphs annotation documentation의 도움을 받아 첫 번째 질문에 대해 이와 같은 작업을 수행 할 수 있습니다. 두 번째 질문에 대한

# answers Stack Overflow question 
# http://stackoverflow.com/questions/27671576/how-can-i-get-tooltips-showing-in-dygraphs-without-annotation 
# on how to customize annotations 

library(dygraphs) 

# question is two parts - let's handle part 1 first 
dygraph(presidents, main = "Presidential Approval") %>% 
    dyAxis("y", valueRange = c(0, 100)) %>% 
    dyAnnotation("1950-7-1", text = "Korea", tooltip = "" 
       # this is not necessary but think it better to be specific 
       ,cssClass = "specialAnnotation") %>% 
    # will leave this as before 
    dyAnnotation("1965-1-1", text = "Vietnam", tooltip = "") -> dyG 

#this is a hack to set css directly 
# dyCSS designed to read a text css file 
dyG$x$css = " 
    /* if cssClass not assigned use .dygraphDefaultAnnotation */ 
    /* !important is critical for the rules to be applied */ 
    .specialAnnotation { 
    overflow: visible !important; 
    width: initial !important; 
    } 
" 

은, 우리가이

# now for part 2 
dyG = dygraph(presidents, main = "Presidential Approval") %>% 
    dyAxis("y", valueRange = c(0, 100)) 

tooltips = list(
    list(x = "1950-7-1", tooltip = "", text = "Korea") 
    ,list(x = "1965-1-1", tooltip = "", text = "Vietnam") 
) 

annotator <- function(x,y){ 
    d = do.call(dyAnnotation,modifyList(list(dygraph=x),y)) 
    return(d) 
} 

dyG = Reduce(annotator, tooltips, init=dyG) 

#this is a hack to set css directly 
# dyCSS designed to read a text css file 
dyG$x$css = " 
    /* if cssClass not assigned use .dygraphDefaultAnnotation */ 
    /* !important is critical for the rules to be applied */ 
    .dygraphDefaultAnnotation { 
    overflow: visible !important; 
    width: initial !important; 
    border: none !important; 
    font-size: 200% !important; 
    } 
" 

dyG 
+0

대단히 감사드립니다. 포인트의 커플. a) 텍스트가 아닌 툴팁을 통해이 작업을 수행 할 수 있습니까? 이 예제에서는별로 차이가 없지만 실제 게임에서는 농구 경기와 같은 무언가가있을 것이며 점수와 경기에 대한 툴팁이 표시됩니다. 나는 그것들이 한꺼번에 모든 것을 보여주기를 원하지 않는다. b) x = c ("1950-7-1", "1965-1-1")와 text = c ("Korea" "Vietnam")을 툴팁 – pssguy

+0

의 목록 형식으로 바꾸려면 dygraphs에서 사용하는 uncustomizable'title' 메소드가 아닌 툴팁을 사용하고 싶습니까? 툴팁이 발생할 수있는 마커/표시기는 무엇입니까? 전설이 더 나은 선택인지 궁금합니다. – timelyportfolio

+1

전설은 확실히 좋은 대안이 될 것입니다. 예를 들어 농구에서는 "PHO 33 MIN 35 6:43 Goran Dragic이 팁 샷을합니다." 경기의 6 분 43 초에 라인상의 한 지점 위로 마우스를 가져 가면. 그렇다면 텍스트 또는 툴팁을 표시 할 이유가 없을 것입니다 – pssguy