使用Vue-echarts在单行中显示事件

7z5jn7bk  于 7个月前  发布在  Vue.js
关注(0)|答案(2)|浏览(111)

有人能告诉我用vue-echarts创建图表的方向吗?
我有一组表示24小时内事件的数据。这些数据将从API中获取,它是一个对象数组:

[
  {
    start: "2023-11-01T01:47:09.930Z"
    end: "2023-11-01T02:47:09.930Z"
    duration: 60 // in minutes
  },
  {
    start: "2023-11-01T02:47:09.930Z"
    end: "2023-11-01T02:57:09.930Z"
    duration: 10
  },
  {
    start: "2023-11-01T02:57:09.930Z"
    end: "2023-11-01T03:27:09.930Z"
    duration: 30
  },
  {
    start: "2023-11-01T03:27:09.930Z"
    end: null
    duration: 30
  },

字符串
日期中没有间隔,最新事件的结尾可能为null。
我想创建一个只有一行的图表,其中事件从最早到最新一个接一个地堆叠,x轴标签显示日期+时间。
Similar to this image
我按照这个例子echarts stacked horizontal bars创建了一个条形图,但是我不能让标签工作。
我可以尝试使用不同的软件包,如果它更容易实现我的目标。
备注:

  • 我需要一个图表,用户可以互动
  • vue-echarts包已经在应用程序中使用
  • 我会在图表中添加更多的功能,但我知道如何
  • 事件持续时间可以是1秒或数小时
  • 图表可以包含数百个事件
  • 该页面也可以在手机和平板电脑屏幕上显示
pzfprimi

pzfprimi1#

我不太清楚你对echarts的问题是什么,但这里有一个小想法可能会有所帮助:
而不是使用堆叠条形图,您可以尝试使用具有放大宽度的折线图。Here是一个简单的例子,它可能看起来像:

const data = [
  {
    start: "2023-11-01T01:47:09.930Z",
    end: "2023-11-01T02:47:09.930Z",
    duration: 60, // in minutes
  },
  {
    start: "2023-11-01T02:47:09.930Z",
    end: "2023-11-01T02:57:09.930Z",
    duration: 10,
  },
  {
    start: "2023-11-01T02:57:09.930Z",
    end: "2023-11-01T03:27:09.930Z",
    duration: 30,
  },
  {
    start: "2023-11-01T03:27:09.930Z",
    end: null,
    duration: 30,
  },
]

seriesList = data.map(function(event) {
  let start = event.start;
  let end = event.end;
  if (event.end === null) {
    end = start;  // set end to current time here?
  }
  return {
    type: 'line',
    data: [[start, 1], [end, 1]],
    symbol: 'none',
    lineStyle: {
      width: 60
    }
  };
})

option = {
  xAxis: {
    type: 'time',
  },
  yAxis: {
    type: 'category',
    show: false,
  },
  series: seriesList
};

字符串

gopyfrb3

gopyfrb32#

要在Vue表中显示提供的数据(假设您指的是基本表,而不是任何特定的组件,如Vuetify的v-data-table),您可以使用以下代码:
首先,在您的脚本部分中,定义您提供的数据:

<script>
export default {
  data() {
    return {
      timeEntries: [
        {
          start: "2023-11-01T01:47:09.930Z",
          end: "2023-11-01T02:47:09.930Z",
          duration: 60 // in minutes
        },
        {
          start: "2023-11-01T02:47:09.930Z",
          end: "2023-11-01T02:57:09.930Z",
          duration: 10
        },
        {
          start: "2023-11-01T02:57:09.930Z",
          end: "2023-11-01T03:27:09.930Z",
          duration: 30
        },
        {
          start: "2023-11-01T03:27:09.930Z",
          end: null,
          duration: 30
        },
      ]
    };
  }
};
</script>

字符串
然后,在模板部分中,使用表格显示数据:

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>Start Time</th>
          <th>End Time</th>
          <th>Duration (minutes)</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="entry in timeEntries" :key="entry.start">
          <td>{{ entry.start }}</td>
          <td>{{ entry.end ? entry.end : 'N/A' }}</td>
          <td>{{ entry.duration }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>


这将生成一个包含“Start Time”、“End Time”和“Duration”列的表。表的每行将显示timeEntries数组中的相应值。如果end值为null,则它将在“End Time”列中显示“N/A”。

相关问题