问题
I'm trying to set extremes on a time series stock chart that corresponds to a user pushing a button. Here's the breakdown:
User clicks on a button on the top of the chart (I have edited the "All", "1M", "3M" buttons typically at the top)
When the button is clicked, a custom area on the xAxis (2 months) is zoomed in on. For example, October 1st through December first. Right now, the zoom goes to the end of the graph.
It's very similar to the below link.
X Axis Set Extremes
My R code right now for the button is the following:
hc_rangeSelector(buttons=list(list(type='month', text='New', count=2)))
This says I am looking for a month interval zoom, the text is "New", and it shows 2 months. I've seen that setExtremes is the function i'm looking for but I haven't seen it implemented using R.
回答1:
You could place a JavaScript function in chart.events.load option in Highcharter
. Using Renderer you could add a button that will use setExtremes function on click.
Demo in JSFiddle (without Highcharter
nor `R, data is different, but functionality of the button is the same): http://jsfiddle.net/e69eLm6q/
Code to run in R
:
library("quantmod")
usdjpy <- getSymbols("USD/JPY", src = "oanda", auto.assign = FALSE)
eurkpw <- getSymbols("EUR/KPW", src = "oanda", auto.assign = FALSE)
hc <- highchart(type = "stock") %>%
hc_title(text = "Charting some Symbols") %>%
hc_add_series(data = usdjpy, id = "usdjpy", pointInterval = 36000000) %>%
hc_add_series(data = eurkpw, id = "eurkpw", pointInterval = 36000000) %>%
hc_rangeSelector(buttons=list(list(type='month', text='New', count=2))) %>%
hc_chart(
events = list(
load = JS("function(){
var chart = this;
chart.renderer.button('do stuff',200, 100)
.attr({
zIndex: 3
})
.on('click', function () {
chart.xAxis[0].setExtremes(Date.UTC(1970, 4, 1), Date.UTC(1970, 6, 1));
})
.add();
}")
)
)
hc
来源:https://stackoverflow.com/questions/42235455/highcharter-setextremes-function-in-r