0

我正在使用 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
})
4

1 回答 1

2

是的,这是可能的。

在为 LegendBox 创建条目时

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 事件来隐藏对应的Axis:

// 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()
  }
})

但是,您不应该以这种方式处理默认轴,因为不能保证在您处理默认轴时会在其位置上放置任何其他轴。

于 2019-10-28T10:30:24.013 回答