我的数据有一个不规则时间 xAxis highchart。我试图做到这一点,以便当单击一个点时,我可以在另一个窗口中打开一个新的 URL。我能够在基本图表(没有不规则的 xAxis 间隔)上实现此功能,但是当我更改数据格式时,我无法获得实际的 URL。
下面是我的带有虚拟数据的代码
$(function () {
$('#container').highcharts({
chart: {
type: 'spline',
zoomType: 'x'
},
title: {
text: ' frequency'
},
subtitle: {
text: 'Irregular time data in Highcharts JS'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%b %y, %Y',
year: '%Y'
},
title: {
text: 'Date Published'
}
},
yAxis: {
title: {
text: 'Price ($USD)'
},
min: 0
},
credits: {
enabled: false
},
tooltip: {
// headerFormat: '<b>{series.name}</b><br>',
// pointFormat: '{point.x:%b %e, %Y}: {point.y:.2f} m'
enabled: false
},
series: [{
name: 'Car Price',
color: '#CC0000',
URLs: ['www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
'www.google.com',
],
// Define the data points. All series have a dummy year
// of 1970/71 in order to be compared on the same x axis. Note
// that in JavaScript, months start at 0 for January, 1 for February etc.
data: [
[Date.UTC(2012, 10, 9), 37000],
[Date.UTC(2012, 11, 1), 39000],
[Date.UTC(2012, 11, 1), 10500],
[Date.UTC(2012, 11, 13), 20000],
[Date.UTC(2012, 11, 18), 33000],
[Date.UTC(2013, 5, 22), 24995],
[Date.UTC(2013, 5, 31), 15000],
[Date.UTC(2013, 6, 13), 19000],
[Date.UTC(2013, 8, 20), 4000],
[Date.UTC(2013, 9, 1), 17900],
[Date.UTC(2013, 9, 30), 2500],
[Date.UTC(2013, 11, 18), 25000],
[Date.UTC(2014, 4, 14), 15900],
[Date.UTC(2014, 5, 16), 6900],
],
point: {
events: {
click: function() {
x = this;
var someURL = this.series.userOptions.URLs[this.x];
// if (someURL)
window.open('http://'+someURL);
}
}
}
}]
});
});
摆弄上面的虚拟数据。 http://jsfiddle.net/5kyzkps5/1/
您能否推荐将 URL 与每个点关联的最佳方式?如果URL信息无法与数据表整合,您能帮忙获取正确的xAxis id号吗?如果可行的话,我至少应该能够构建一个数组,我可以在单击函数中调用 URL。提前致谢
最佳答案
最好将 url 保留在点对象中,例如:
data: [{x:Date.UTC(2012, 10, 9), y:37000,url:'http://www.google.com'},
[Date.UTC(2012, 11, 1), 39000]]
示例:http://jsfiddle.net/5kyzkps5/3/
点击第一个点,url 就可以工作了。
关于javascript - 点击URL不规则highcharts图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25436493/