简体   繁体   English

Apache echarts - 时间表样式图表布局

[英]Apache echarts - Schedule style chart layout

Does anyone know of a chart layout that functions like a schedule/timeline?有谁知道功能类似于时间表/时间线的图表布局?

I currently have a chart that shows the on/off status of 3 valves over time.我目前有一张图表,显示 3 个阀门随时间的开/关状态。 It's a stacked line/area chart, it's works ok, but it's not the easiest to follow.这是一个堆叠的线/面积图,它工作正常,但它不是最容易遵循的。

See linked screenshot of what I'm looking to achieve: Chart actual vs desired (Top is current chart, bottom is what I want to achieve).请参阅我想要实现的链接截图:图表实际与期望(顶部是当前图表,底部是我想要实现的)。

Is this possible with apache echarts? apache echarts可以做到这一点吗?

Thanks to @pthomson for pointing me in the right direction.感谢@pthomson 为我指明了正确的方向。

Here is what I've come up with: Codepen chart example这是我想出的: Codepen 图表示例

I'm still having some issues with the min/max values for the xAxis.我仍然对 xAxis 的最小/最大值有一些问题。 The min/max seems to be calculated on values in index 1, which is the end timestamp of each schedule piece, so not ideal.最小值/最大值似乎是根据索引 1 中的值计算的,这是每个时间表片段的结束时间戳,因此并不理想。

var dom = document.getElementById('chart-container');
var myChart = echarts.init(dom, null, {
  renderer: 'canvas',
  useDirtyRect: false
});
var app = {};

var option;


const valveColors = [
    "#f59527",
    "#00e200",
    "#2da8f3",
]

var data = [
  {
      "name": "Valve 1",
      "value": [
          1655647200000,
          1657980000000,
          0
      ]
  },
  {
      "name": "Valve 3",
      "value": [
          1657980000000,
          1659448800000,
          2
      ]
  },
  {
      "name": "Valve 1",
      "value": [
          1659448800000,
          1660526144467,
          0
      ]
  },
  {
      "name": "Valve 2",
      "value": [
          1655647200000,
          1660526144467,
          1
      ]
  }
];

option = {
            xAxis: {
                type: "time",
        //Min is getting values from index 1, not sure why
                min: range => range.min - (7 * 24 * 60 * 60 * 1000), //Subtract 7 days
            },
            yAxis: {
                type: "category",
                data: [ "Valve 3", "Valve 2", "Valve 1" ]
            },
            series: [
                {
                    type: "custom",
                    renderItem: (params, api) => {
                        var catIndex = api.value(2);
                        var timeSpan = [api.value(0), api.value(1)];
                        var start = api.coord([timeSpan[0], 2 - catIndex]);
                        var end = api.coord([timeSpan[1], 2 -catIndex]);
                        var size = api.size([0,1]);
                        var height = size[1] * 0.6;
                        var rect = echarts.graphic.clipRectByRect(
                            { x: start[0], y: start[1] - height / 2, width: end[0] - start[0], height: height},
                            { x: params.coordSys.x, y: params.coordSys.y, width: params.coordSys.width, height: params.coordSys.height}
                        );
                        return (
                            rect && {
                                type: "rect",
                                transition: ["shape"],
                                shape: rect,
                                style: {
                                    fill: valveColors[catIndex],
                                },
                            }
                        );
                    },
                    encode: {
                        x: [0,1],
                        y: 0,
                    },
                    data: data,
                }
            ],
            tooltip: {
                show: true,
                trigger: "item",
                formatter: params => {
                    return `${params.data.name}<br/> ${params.data.value[0]} - ${params.data.value[1]}` //Unix timestamps should be converted to readable dates
                }
            },
            dataZoom: [
                {
                    type: "slider",
                    filterMode: "none"
                },
            ],
        }

if (option && typeof option === 'object') {
  myChart.setOption(option);
}

window.addEventListener('resize', myChart.resize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM