echart——时间点和时间段综合显示

效果图

效果图


html  代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 1.先添加一个容器存放图表  -->
    <div id="main" style="width: 600px;height: 400px"></div>

</body>
<script type="text/javascript" src="echarts.min.js"></script>
<script type="text/javascript" src="dateview.js"></script>
</html>

 

dateview.js  代码

js里所有的时间为手动填上去的测试时间,后期项目里应该都是从后来拿的数据


// 2.1 初始化echars对象
var myChart = echarts.init(document.getElementById('main'));

// 计算保修有多少天
var date_waranty_start = new Date('2017-01-01');
var date_waranty_end = new Date('2022-01-01');
var days = parseInt(date_waranty_end - date_waranty_start) / 1000 / 60 / 60 / 24;
// console.log(days);

// 2.2 设置图表的配置参数
var option = {

    title: {
        text: "日期维度统计图",
        textStyle: {
            textShadowColor: "#677c81",
            textShadowBlur: 4,
            textShadowOffsetX: 2,
            textShadowOffsetY: 2,
        },
        bottom: 'bottom',
        right: 'center',

    },

    tooltip: {
        trigger: 'axis',
        axisPointer: {            // 坐标轴指示器,坐标轴触发有效
            type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
        },
        // 自定义提示框显示内容,若为空则不显示
        formatter: function (params) {
            var res = '';
            for (var i = 0; i < params.length; i++) {
                if (params[i].data != null) {
                    if (params[i].name == '保修时间') {
                        res += '保修开始时间:' + params[i].data + '<br>' + '保修截止时间:' + params[i + 1].data + '<br>';
                        return res
                    } else {
                        var icon = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + ';"></span>';
                        res += icon + params[i].name + ':' + params[i].data + '<br>'
                    }
                }
            }
            return res;
        },
    },
    legend: {
        data: ['购置日期', '安装日期', '保修时间', '最后在线时间']
    },
    grid: {
        left: '5%',
        right: '5%',
        bottom: '10%',
        containLabel: true
    },
    xAxis: {
        type: 'time'
    },
    yAxis: {
        type: 'category',
        axisTick: {show: false},
        data: ['购置日期', '安装日期', '保修时间', '最后在线时间'],

    },
    series: [

        {
            name: '购置日期',
            type: 'scatter',
            symbolSize: 20,
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{c}',
                }
            },
            itemStyle: {
                normal: {
                    color: '#80BC9E'
                }
            },
            data: ['2016-01-01']
        },
        {
            name: '安装日期',
            type: 'scatter',
            symbolSize: 20,
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{c}',
                }
            },
            itemStyle: {
                normal: {
                    color: '#243542'
                }
            },
            data: [null, '2017-01-01']
        },
        {
            name: '保修开始时间',
            type: 'bar',
            stack: '保修',
            label: {
                normal: {
                    show: false
                }
            },
            itemStyle: {
                normal: {
                    color: "rgba(0,0,0,0)"
                },
            },

            barCategoryGap: "50%",  //类目间柱形距离,默认为类目间距的20%,可设固定值
            data: [null, null, '2017-01-01']
        },

        {
            name: '保修时间',
            type: 'bar',
            stack: '保修',
            label: {
                normal: {
                    show: true,
                    formatter: '保修时间:' + days + '天'
                },

            },
            itemStyle: {
                normal: {
                    color: "#508F98"
                },
            },
            barCategoryGap: "50%",  //类目间柱形距离,默认为类目间距的20%,可设固定值
            data: [null, null, '2022-01-01']
        },
        {
            name: '最后在线时间',
            type: 'scatter',
            symbolSize: 20,
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{c}',
                }
            },
            itemStyle: {
                normal: {
                    color: '#BC731E'
                }
            },
            data: [null, null, null, '2018-09-01'],
            markLine: {
                lineStyle: {
                    normal: {
                        type: 'dashed',
                        width: 2,
                        color: '#b62314',
                    },
                },
                label: {
                    normal: {
                        formatter: '当前时间:' + '2018-10-11'
                    },
                },
                data: [
                    {xAxis: '2018-10-11'},
                ]
            },
        },


    ]
};

// 3.3 渲染图表
myChart.setOption(option);

echart3下载地址

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
echarts中,时间显示可以通过配置xAxis来实现。通常情况下,xAxis的type属性需要设置为'category',表示x轴的数据类型是类目型。同,可以通过axisLabel属性来设置时间显示格式和间隔。 如果需要以小为单位显示时间,可以将xAxis的data属性设置为一个包含每个小的数组,例如['00:00', '01:00', '02:00', ... '23:00']。通过设置axisLabel的interval属性为11,可以实现每隔11个小显示一个刻度,这样就能够显示出24个小的范围。另外,可以通过设置formatter属性来自定义时间显示格式,比如只显示部分。 如果需要以秒为单位显示时间,可以将xAxis的data属性设置为一个包含每个秒的数组,例如['00:00:00', '00:00:01', '00:00:02', ... '23:59:59']。同样地,通过设置axisLabel的interval属性和formatter属性来控制刻度的显示间隔和格式。 总结起来,通过配置xAxis的type属性为'category',设置axisLabel的interval属性和formatter属性,可以实现在echarts中对时间的灵活显示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [echarts y轴显示时间time](https://blog.csdn.net/sdaulee/article/details/120995014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [echart以秒为单位的动态时间轴](https://download.csdn.net/download/u013720726/9965163)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [解决echart时间显示问题](https://blog.csdn.net/qq_21179679/article/details/84973555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值