我正在使用LightningChartJS创建一个图表。单击图例框中的系列描述时,系列消失,但轴保留。是否可以将轴与系列一起隐藏?
我的轴和系列如下:-
const axis1 = chart.getDefaultAxisY()
.setStrokeStyle(axisYStrokeStyles[0])
.setOverlayStyle(axisYStylesHighlight[0])
.setNibOverlayStyle(axisYStylesHighlight[0])
.setTickStyle(visibleTick => visibleTick
.setLabelFillStyle(axisLabelStyle)
.setGridStrokeStyle(emptyLine)
)
const axis2 = chart.addAxisY(false)
.setTitle('No of units produced')
.setStrokeStyle(axisYStrokeStyles[1])
.setOverlayStyle(axisYStylesHighlight[1])
.setNibOverlayStyle(axisYStylesHighlight[1])
.setTickStyle(visibleTick => visibleTick
.setLabelFillStyle(axisLabelStyle)
.setGridStrokeStyle(emptyLine)
)
const splineSeries1 = chart.addSplineSeries({
xAxis: axisX,
yAxis: axisY1
})
const splineSeries2 = chart.addSplineSeries({
xAxis: axisX,
yAxis: axisY2
})
是的,这是可能的。
向图例框创建条目时
const legendBox = chart.addLegendBox( LegendBoxBuilders.HorizontalLegendBox )
// Add a single entry to the LegendBox. Since we know there is only one Entry, we can
// cast the returned entry as one to avoid checking for Arrays.
const entry = legendBox.add( series, true, splineSeries2.getName() ) as LegendBoxEntry
您可以订阅onSwitch 事件以隐藏相应的轴:
// Subscribe to the onSwitch event.
entry.onSwitch( ( _, state ) => {
// The state parameter is the state of the CheckBox
// If the CheckBox was just switched off, dispose of the Axis. Else, restore it.
if ( !state ) {
AxisY2.dispose()
} else {
AxisY2.restore()
}
})
但是,您不应以这种方式处置默认轴,因为无法保证在处置它时会有任何其他轴。