angular 引入echarts的具体方法及使用

第一步:安装依赖

npm install echarts --save

npm install ngx-echarts --save

 

第二步:在Module中引入

import { NgxEchartsModule } from 'ngx-echarts';

@NgModule({

 imports: [

 ...,

// 引入module

 NgxEchartsModule

 ],

})

export class AppModule { }


第三步:在angular-cli中添加js引入

添加:node_modules/echarts/dist/echarts.min.js

页面中使用:

html:

<div style="padding: 20px;background: #fff;" echarts [options]="currentDayPay" class="broken-line"></div>

ts 动态加载

import { EChartOption } from 'echarts';

声明:currentDayPay;

currentPay() {
        this.StatisticsService.currentPay().subscribe((res: any) => {
            if (res.Code == 200) {
                let data = res.Data.TodaySum;
                let timeArr = [], valueArr = [];
                data.forEach(item => {
                    timeArr.push(item.date);
                    valueArr.push(item.incomeCnt);
                });
                this.currentDayPay = {
                    title: {
                        text: '当日付费'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    legend: {
                        height: 50,
                        left: 'center',
                        top: 'bottom',
                        data: ['今日付费']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: timeArr
                        }
                    ],

                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [

                        {
                            name: '今日付费',
                            type: 'line',
                            stack: '总量',
                            areaStyle: {},
                            data: valueArr
                        },
                        // {
                        //     name: '昨日付费',
                        //     type: 'line',
                        //     stack: '总量',
                        //     areaStyle: {},
                        //     data: [820, 932, 901, 934, 1290, 1330, 1320]
                        // }
                    ]
                }
            }

        });
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 echarts 拖动区域进行放大,可以使用 echarts 提供的 dataZoom 组件。具体实现方法如下: 1. 在 HTML 添加 echarts 组件的容器: ```html <div id="chartContainer" style="width: 100%; height: 500px;"></div> ``` 2. 在 TypeScript 引入 echarts,并初始化 echarts 实例: ```typescript import * as echarts from 'echarts'; const chart = echarts.init(document.getElementById('chartContainer')); ``` 3. 定义数据和配置项: ```typescript const data = { xData: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], yData: [820, 932, 901, 934, 1290, 1330, 1320], }; const option = { xAxis: { type: 'category', data: data.xData, }, yAxis: { type: 'value', }, series: [{ data: data.yData, type: 'line', }], }; ``` 4. 添加 dataZoom 组件: ```typescript option.dataZoom = [{ type: 'inside', start: 0, end: 100, }, { start: 0, end: 100, handleSize: '80%', handleStyle: { color: '#fff', shadowBlur: 3, shadowColor: 'rgba(0, 0, 0, 0.6)', shadowOffsetX: 2, shadowOffsetY: 2, }, textStyle: { color: '#fff', }, }]; chart.setOption(option); ``` 5. 最后,监听 dataZoom 组件的事件,获取当前的缩放范围,并更新数据: ```typescript chart.on('dataZoom', (params: any) => { const start = params.batch[0].start; const end = params.batch[0].end; const startIndex = Math.round(data.xData.length * start / 100); const endIndex = Math.round(data.xData.length * end / 100); const newData = { xData: data.xData.slice(startIndex, endIndex), yData: data.yData.slice(startIndex, endIndex), }; chart.setOption({ xAxis: { data: newData.xData, }, series: [{ data: newData.yData, }], }); }); ``` 这样,当用户拖动 dataZoom 区域时,就会触发 dataZoom 事件,根据当前的缩放范围更新数据,并重新渲染 echarts 图表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值