简体   繁体   English

Python:在 setExtreme 之后从 Highcharts 读取数据

[英]Python: Read data from Highcharts after setExtreme

I'm trying to get the data from a Highcharts chart using Selenium.我正在尝试使用 Selenium 从 Highcharts 图表中获取数据。 My issue is that the setExtremes function does not work with .options.data .我的问题是setExtremes函数不适用于.options.data How can I read data after using setExtremes using purely Python-based methods?使用基于 Python 的方法使用setExtremes后如何读取数据?

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)

I am trying to execute your code on Highcharts demo page我正在尝试在 Highcharts演示页面上执行您的代码
The problem is with xAxis[0] , xAxis is not an array but a dictionary, so you must supply a string value there inside those [] .问题在于xAxis[0]xAxis不是数组而是字典,因此您必须在这些[]提供一个字符串值。

check xAxis in the docs检查文档中的xAxis
I am guessing you're looking for xAxis.events.setExtremes我猜你正在寻找xAxis.events.setExtremes

Edit编辑

I see now that xAxis can be an array, but you're most likely missing those events so my solution should be changed to xAxis[0].events.setExtremes我现在看到 xAxis 可以是一个数组,但是您很可能错过了这些events因此我的解决方案应该更改为xAxis[0].events.setExtremes

The problem is setExtremes(min, max) method returns undefined , so you can not chain options.问题是setExtremes(min, max)方法返回undefined ,因此您不能链接选项。 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).注意:该代码段可以放在一个单独的文件中,并像任何其他 Highcharts 插件一样使用(只需在 Highcharts 库之后加载)。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM