JS----按小时统计,小时为空,用0补充

按小时统计,小时为空,用0补充

初始化
line: function (xAxisData, seriesData, legendData) {
	return  {
		title: {
			text: "",
			subtext: "",
			x: 'center',
		},
		tooltip: {
			show: true,
			trigger: 'axis',
			formatter: "{b} <br/>{a} : {c}",
		},
		legend: {
			orient: 'vertical', //垂直:vertical; 水平 horizontal
			left: 'right',
			data: legendData || []
		},
		xAxis: [
			{
				type : 'category',
				data: xAxisData || [],
				axisLabel :{
					// rotate: -20
				}
			}
		],
		yAxis: [
			{
				type : 'value',
			}
		],
		series: [
			{
				name: '数量',
				type: 'line',
				data: seriesData || []
			},
		]
	};
},
initChart: function (id, option) {
			
			var chart = echarts.getInstanceByDom(document.getElementById(id)), timer = null;

			if (chart == null) {
				chart = echarts.init(document.getElementById(id), layui.echartsTheme);
			}

			chart.setOption(option);

			$(window).on('resize', function () {
					timer && clearTimeout(timer)
					timer = setTimeout(function() {
						chart && chart.resize()
					}, 400);
				}
			);

			return chart;
		},
使用
function initRenderChart(data, id) {
    admin.req({
        url,
        data,
        success: function (res) {
            if (res.code === 0 && res.data.length > 0) {
                renderChart(layui.myChart.dataFormat.getData(res.data, 'day', 'value'), id);
            } else {
                layui.myChart.set.empty(id);
            }
        }
    })
}

function renderChart (data, id) {
    var option = layui.myChart.option.line(data.dateList, data.valueList);
    layui.myChart.initChart(id, option)
}
格式化数据
dataFormat: {
	getData(data, dateLabel, valueLabel, legendLabel) {
		var dateList = [], valueList = [], legendList = [];
		dateLabel = dateLabel || 'category';
		valueLabel = valueLabel || 'value';
		legendLabel = legendLabel || 'group'

		data.map((item) => {
			dateList.push(item[dateLabel] || '');
			valueList.push(item[valueLabel] || 0);

			if (item[legendLabel] && !legendList.contains(item[legendLabel])) {
				legendList.push(item[legendLabel]);
			}
		})

		return {dateList, valueList};
	}
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值