即使没有可用的系列数据,如何显示顶点图表图例(ReactJS)

即使没有可用的系列数据,如何显示顶点图表图例(ReactJS),reactjs,apexcharts,Reactjs,Apexcharts,我在ReactJs中使用线条顶点图。如果没有可用数据,则Y轴的图例不可见。如果数据可用,它们就会出现。即使序列中没有数据,我也希望显示图例。 我尝试过的备选方案:如果DB中没有数据,则发送带有0值的seriesData,它将在图形上显示带有0值的点,并且图例将可用。但是,我想摆脱这一点 getOptions() { return { chart: { height: 200, type: "line", st

我在ReactJs中使用线条顶点图。如果没有可用数据,则Y轴的图例不可见。如果数据可用,它们就会出现。即使序列中没有数据,我也希望显示图例。 我尝试过的备选方案:如果DB中没有数据,则发送带有0值的seriesData,它将在图形上显示带有0值的点,并且图例将可用。但是,我想摆脱这一点

getOptions() {
    return {
      chart: {
        height: 200,
        type: "line",
        stacked: false,
      },
      colors: this.themes[this.props.theme],
      dataLabels: {
        enabled: false,
      },
      stroke: {
        width: [3.5, 3.5],
      },
      xaxis: {
        categories: this.props.xCategories,
        labels: {
          style: {
            color: "#263238",
          },
        },
      },
      yaxis: [
        {
          axisTicks: {
            show: true,
          },
          axisBorder: {
            show: false,
          },
          labels: {
            style: {
              colors: this.themes[this.props.theme][0],
              fontWeight: "bold",
            },
          },
          tooltip: {
            enabled: false,
          }
        },
        {
          seriesName: "Revenue",
          opposite: false,
          axisTicks: {
            show: true,
          },
          axisBorder: {
            show: false,
          },
          labels: {
            style: {
              colors: this.themes[this.props.theme][1],
              fontWeight: "bold",
            },
          },
        },
      ],
      legend: {
        position: "top",
        horizontalAlign: "left",
        offsetX: -15,
        fontWeight: "bold",
      }
    }
  }
下面是渲染方法:

render() {
    return (
      <div>      
        <Chart
          options={this.getOptions()}
          series={this.props.seriesData}
          type='line'
        />
      </div>
    );
  }
render(){
返回(
);
}

有几个属性可确定图例是否应在不同情况下显示。中有可用的信息,但这里有一个概述:


showForSingleSeries

默认值:false

这将确定即使图形上仅显示一个系列,是否也应显示图例


showForNullSeries

默认值:true

这将确定是否隐藏仅包含空值的序列


showForZeroSeries

默认值:true

这将确定是否隐藏仅包含零值的序列


在您的情况下,首先要确保
showForSingleSeries
设置为
true
,以确保始终显示图例。其次,您可以返回空数组或包含
null
元素的数组,而不是返回包含零值的序列

最后,为了在使用代码时显示图例,我必须向
选项
对象添加一个
系列
属性。这是因为除非图例有任何系列名称要显示,否则图例不会显示-除非您指定,否则图例不知道您将为其提供的系列的名称

var options = {
  ...
  series: [
    {
      name: "Revenue",
      data: []
    }
  ]
  ...
}

谢谢你的回答,我一定会试试这个!!!