当前是否可以使用图例项目在刷新时保存其状态?例如,我有一个包含3条线的图形:a,b和c。我关闭了c行,因此它不会显示在图形上。当前,刷新页面时,a,b和c将再次显示在图形上。有没有办法让它记住关闭c?
答案 0 :(得分:2)
使用localStorage
功能存储图表系列可见性的当前状态。单击图例项目时,需要更改本地存储位置的状态,因此只需使用plotOptions.series.events.legendItemClick
处理程序,然后在每个图表load
事件上,根据存储的信息来设置系列可见性。
chart: {
events: {
load: function() {
var series = this.series;
// If there is no legend items visibility flags saved in local storage, save them.
if (!localStorage.legendItems) {
let legend = []
series.forEach(function(series) {
legend.push(series.visible)
})
localStorage.legendItems = legend.toString()
//
} else {
let legend = localStorage.legendItems.split(',')
let legendBooleans = []
legend.forEach(function(elem) {
var isTrueSet = (elem === 'true')
legendBooleans.push(isTrueSet)
})
legendBooleans.forEach(function(state, i) {
series[i].update({
visible: state
})
})
}
}
}
},
plotOptions: {
series: {
events: {
legendItemClick: function() {
var series = this.chart.series
var index = this.index
var legend = localStorage.legendItems.split(',')
var legendBooleans = []
legend.forEach(function(elem) {
var isTrueSet = (elem === 'true')
legendBooleans.push(isTrueSet)
})
// toggle series visibility flag and override it in local storage
legendBooleans[index] = !legendBooleans[index]
localStorage.legendItems = legendBooleans.toString()
}
}
}
},
如果有任何疑问,请问。
实时示例:https://jsfiddle.net/os961gke/
API参考:https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick https://api.highcharts.com/highcharts/chart.events.load