使用echarts.js实现折线图,实现x轴可以滑动

13 篇文章 0 订阅

echart折线图,x轴滑动

在使用echart开发折线图,因为x轴的展示数据太多,想默认展示一段时间的数据
在这里插入图片描述

<div id="flowDay" class="index_height index_width" style="width: 1100px; height: 560px"></div>
var dayFlowEcharts = echarts.init(document.getElementById('flowDay'));
    dayFlowEcharts.setOption(optionTemp);
    getDayFlow(dayFlowEcharts, listData.iotMsisdn);
function getDayFlow(dayFlowEcharts, iotMsisdn) {
    $.ajax({
        url: pjUrl + "iot/cardDetail/flowDay",
        dataType: 'json',
        contentType: "application/json",
        type: 'post',
        xhrFields: {
            withCredentials: true
        },
        data: JSON.stringify({"iotMsisdn": iotMsisdn}),
        success: function (data) {
            if (data.status == "200") {
                if (data.data && data.data != null && data.data.length > 0) {
                    var num = 0.000
                    var legendData = []
                    var cateData = []
                    data.data.forEach(item => {
                        legendData.push(item.flow)
                        cateData.push(item.time)
                    });
                    legendData.forEach(item => {
                        num = (num + parseInt(item))
                    });
                    dayFlowEcharts.setOption({
                        title: {text: '总计:' + num + '(MB)'},
                        xAxis: {data: cateData},
                        series: [{data: legendData}]
                    });
                }
            }
        },
        error: function (d) {
            closeIndexPopUp(index_user_load);
            alertErrorMsg("基础信息初始化失败,请重试");
        }
    });
}
/**
 * 卡详情 折线图
 */
var optionTemp = {
    title: {
        text: '总计:' + 0 + '(MB)'
    },
    tooltip: {
        trigger: 'axis'
    },
    color: '#2db7f5',
    grid: {
        left: '5%',
        right: '3%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        type: 'category',
        boundaryGap: true,
        data: ['2020-01']
    },
    yAxis: {
        name: '使用量(MB)',
        type: 'value',
        // axisLine: {
        //     lineStyle: {
        //         // 设置y轴颜色
        //         color: '#c9c9c9'
        //     }
        // },
    },
    dataZoom: [{
        type: 'inside',
        start: 40,
        end: 100
    }, {
        start: 0,
        end: 10,
        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
        handleSize: '100%',
        handleStyle: {
            color: '#fff',
            shadowBlur: 4,
            shadowColor: 'rgba(0, 0, 0, 0.6)',
            shadowOffsetX: 2,
            shadowOffsetY: 2
        }
    }],
    series: [
        {
            name: '使用量(MB)',
            type: 'line',
            stack: '总量',
            data: [0],
            symbolSize: 8,
            itemStyle: {
                normal: {
                    // 拐点上显示数值
                    label: {
                        show: true
                    },
                    borderColor: '#2db7f5',  // 拐点边框颜色
                    lineStyle: {
                        width: 5,  // 设置线宽
                        type: 'solid'  //'dotted'虚线 'solid'实线
                    }
                }
            }
        }
    ]
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Vue 和 Echarts 实现两条折线图的基本步骤: 1. 安装 Echarts 在 Vue 项目中使用 Echarts 需要先安装 Echarts,可以使用 npm 或 yarn 进行安装: ```bash npm install echarts ``` 或 ```bash yarn add echarts ``` 2. 引入 Echarts 在需要使用 Echarts 的组件中引入 Echarts: ```javascript import echarts from 'echarts' ``` 3. 创建 Echarts 实例 在组件的 `mounted` 钩子函数中创建 Echarts 实例,并绑定到 Vue 实例的 `chart` 属性上: ```javascript mounted() { // 基于准备好的dom,初始化echarts实例 const chartDom = this.$refs.chart const myChart = echarts.init(chartDom) // 将echarts实例绑定到Vue实例的chart属性上 this.chart = myChart } ``` 4. 渲染折线图 定义数据和配置项,然后在组件的 `mounted` 钩子函数中使用 Echarts 的 `setOption` 方法来渲染折线图: ```javascript mounted() { const chartDom = this.$refs.chart const myChart = echarts.init(chartDom) this.chart = myChart // 定义数据和配置项 const data = { xData: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], seriesData1: [120, 200, 150, 80, 70, 110, 130], seriesData2: [80, 120, 110, 90, 60, 100, 80] } const option = { tooltip: { trigger: 'axis' }, legend: { data: ['数据1', '数据2'] }, xAxis: { type: 'category', boundaryGap: false, data: data.xData }, yAxis: { type: 'value' }, series: [{ name: '数据1', type: 'line', data: data.seriesData1 }, { name: '数据2', type: 'line', data: data.seriesData2 }] } // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option) } ``` 5. 在模板中渲染 Echarts 实例 最后,在模板中使用 `ref` 属性来引用 Echarts 容器,然后将容器渲染到页面中: ```html <template> <div> <div ref="chart" style="width: 100%; height: 300px"></div> </div> </template> ``` 这样就可以实现在 Vue 中使用 Echarts 渲染两条折线图了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值