Tuesday, January 30, 2018

Python: Read data from Highcharts after setExtreme

Leave a Comment

I'm trying to get the data from a Highcharts chart using Selenium. The issue I have with this is that the setExtremes function does not work with .options.data. How can I read data after using setExtremes using purely Python-based methods?

My code:

capabilities = webdriver.DesiredCapabilities().FIREFOX capabilities["marionette"] = True driver = webdriver.Firefox(capabilities=capabilities, executable_path=gecko_binary_path) driver.get(website) time.sleep(5)  temp = driver.execute_script('return window.Highcharts.charts[0].series[0]'                              '.xAxis[0].setExtremes(Date.UTC(2017, 0, 7), Date.UTC(2017, 0, 8))'                              '.options.data'                             )  data = [item for item in temp] print(data) 

1 Answers

Answers 1

The problem is setExtremes(min, max) method returns undefined, so you can not chain options. Solution is to wrap this method and pass on context, for example:

(function(H) {   H.wrap(H.Axis.prototype, 'setExtremes', function (proceed) {     proceed.apply(this, Array.prototype.slice.call(arguments, 1);     return this; // <-- context for chaining   }); })(Highcharts); 

Now we can use:

return window.Highcharts.charts[0].xAxis[0].setExtremes(min, max).series[0].options.data; 

Note: The snippet can be placed in a separate file and used like any other Highcharts plugin (simply load after Highcharts library).

Important

Axis object has references only to series that are bound to this axis. If you want to access any series on the chart use:

return window.Highcharts.charts[0].xAxis[0].setExtremes(min, max).chart.series[0].options.data; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment