ApexCharts.js:如何为折线图中的轴标签设置固定值

Dav*_*ida 4 javascript charts apexcharts

我目前正在使用 ApexChart 来显示折线图。

问题:
我想知道是否有办法设置固定轴标签值。


我的研究努力

例如:
假设我想要一个折线图,显示 24 小时内每小时的一些计数,并在 x 轴上的 [00:00, 06:00, 12:00, 18:00] 小时标记处显示刻度(这部分就是我想要的)。

所以我们的图表将有 24 个数据点 [00:00, 01:00, ..., 23:00]。每小时一次。
x 轴上有时间 (hh:mm)。
在 y 轴上我们有计数。

如果我只是简单地插入数据集,我会得到如下所示的图表。
如您所见,ApexCharts 自动设置 x 轴刻度值。

没有刻度线的图表

遗憾的是,这不是我想要的...设置tickAmount也没有得到我想要的结果,因为ApexChart只是将范围(在本例中为0-23)除以tickAmount来获得其刻度。遗憾的是,无法通过划分轴来获得我想要的结果。

我还认为我可以将 x 轴类型设置为类别并仅显示每个第 n 个标签,但该选项似乎也不存在。

以下是我传递给 apexcharts 的选项

const options = {
  chart: {
    type: 'line',
  },
  series: {
    name: 'count',
    data, // data as type [number, number][], first number is date, second number is count. 24 data points. one for every hour.
  },
  xaxis: {
    tickAmount, // setting this didn't help
    labels: {
      show: true,
      formatter: (val: string) => formatDateToHHmm(val), // just formats date to hh:mm format
    },
  },
}
Run Code Online (Sandbox Code Playgroud)

更新 1:我尝试了以下更改,但无济于事,我只得到了 24 个 xaxis 标签...

  • 将 xaxis 类型更改为category
  • 添加类别
  • 改变tickAmount
  • 更改数据类型( [number, number][], { x: number, y: number}[], number[])
const options = {
  chart: {
    type: 'line',
  },
  series: {
    name: 'count',
    // data as...
    // type [number, number][], first number is date, second number is count.
    // type { x: number, y: number }[], x is date, y is count.
    // type number[], number is count.
    // 24 data points, one for every hour
    // I tried all data formats and nothing changed
    data, 
  },
  xaxis: {
    type: 'category',
    categories, // ['00:00', '01:00', '02:00', ..., '23:00'],
    tickAmount, // setting this didn't help
    labels: {
      show: true,
      formatter: (val: string) => formatDateToHHmm(val), // just formats date to hh:mm format
    },
  },
}
Run Code Online (Sandbox Code Playgroud)

And*_*bie 6

如果您知道 x 轴标签应该是什么,则可以将它们作为数组包含在属性中categories

var options = {
  series: [{
    data: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120]
  }],
  chart: {
    height: 350,
    type: 'line',
    zoom: {
      enabled: false
    }
  },
  dataLabels: {
    enabled: false
  },
  xaxis: {
    categories: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
    tickAmount: 10  // optional tickAmount value
  }
};

var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<div id="chart"></div>
Run Code Online (Sandbox Code Playgroud)

即使标签并不总是 00:00 - 23:00,您也可以根据数据进行小时计算,将它们推送到数组,然后将其分配给属性categories

例如:

let timestamps = [1599675360368, 1599678960368, 1599682560368]; // using only 3 timestamps for this, but in your data you may have up to 24
let hours = timestamps.map(i => new Date(i).getHours().toString() + ":00"); // this would become your categories array
console.log(hours);
Run Code Online (Sandbox Code Playgroud)