1、安装库

npm install typings echarts --global

npm install ngx-echarts --save

npm install @types/echarts --save

2、app.module引入

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {ButtonModule} from 'primeng/primeng';
import { HeaderComponent } from './components/header/header.component'; // header components
import {NgxEchartsModule} from 'ngx-echarts';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
HeaderComponent,
// selfHttp
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule,
BrowserAnimationsModule,
ButtonModule,
NgxEchartsModule
],
providers: [httpInterceptorProviders, apiList,{provide: LocationStrategy, useClass: HashLocationStrategy}],
bootstrap: [AppComponent]
})
export class AppModule { }

3、具体文件使用

html代码

<div #myCharts echarts [options]="chartOption" class="demo-chart charstDiv" ></div>

ts代码

import { Component, OnInit, Input } from '@angular/core';
import { MessageService } from 'primeng/api';
import * as ec from 'echarts'; // 没有安装ypes/echarts就会报错
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.css'],
providers: [MessageService]
}) export class TableComponent implements OnInit { chartOption: any;
constructor(private messageService: MessageService) { } ngOnInit() {
this.selectedColumns = this.tablelist.header;
this.items = [
{
label: '查看', icon: 'pi pi-eye', command: (event) => this.showDialog()
},
{ label: '删除', icon: 'pi pi-trash', command: (event) => this.del() }
]; }
onRowSelect(event) {
this.data = {
labels: [],
datasets: []
};
this.displaySidebar = true;
// 组装图表
var data1 = [];
var data2 = [];
var data3 = [];
this.selectedList.forEach((element, index) => {
data1.push((element.month + '月'));
// 销量
data2.push(element.sale);
// 店铺数
data3.push(element.comNum);
});
this.chartOption = {
// title: {
// text: '堆叠区域图'
// },
backgroundColor: '#2c343c',
tooltip: {
trigger: 'axis'
},
legend: {
data: ['零售额', '店铺总数',],
textStyle: {//图例文字的样式
color: 'white',
fontSize: 16
}
},
toolbox: {
orient: 'vertical',
top: 'middle',
feature: {
// dataView: {//数据视图工具,可以展现当前图表所用的数据,编辑后可以动态更新。
// show: true,//是否显示组件。
// readOnly: false
// },
magicType: {//动态类型切换 示例:feature: { magicType: {type: ['line', 'bar', 'stack', 'tiled']}}
show: true,
type: ['line', 'bar']
},
restore: {//配置项还原。
show: true
},
saveAsImage: {}
}
},
dataZoom: {//dataZoom 组件 用于区域缩放,从而能自由关注细节的数据信息,或者概览数据整体,或者去除离群点的影响。
type: 'slider',//滑动条型数据区域缩放组件
start: 0,//起始位置0
end: 100//结束位置100
},
grid: {
left: '3%',
right: '4%',
bottom: '40px',
containLabel: true
},
xAxis: [
{
type: 'category',
boundaryGap: false,
axisLabel: {
interval: 0,
textStyle: {
color: '#c3dbff', //更改坐标轴文字颜色
fontSize: 14 //更改坐标轴文字大小
}
},
data: data1,
axisLine: {
lineStyle: {
color: 'white', //更改坐标轴颜色
}
}
}
],
yAxis: [
{
type: 'value',
axisLabel: {
interval: 0,
textStyle: {
color: '#c3dbff', //更改坐标轴文字颜色
fontSize: 14 //更改坐标轴文字大小
}
},
axisLine: {
lineStyle: {
color: 'white', //更改坐标轴颜色
}
}
}
],
series: [
{
name: '零售额',
type: 'line',
stack: '总量',
areaStyle: {normal: {
color:new ec.graphic.LinearGradient(0, 0, 0, 1, [ //随机颜色
{ offset: 0, color: 'red' },
{ offset: 0.5, color: 'pink' },
{ offset: 1, color: '#ddd' }
])
}},
itemStyle: {
normal: {
color: function () {
return '#' + Math.floor(Math.random() * 0xffffff).toString(16); // 随机颜色
}
}
},
data: data2
},
{
name: '店铺总数',
type: 'line',
stack: '总量',
areaStyle: {normal: {
color:new ec.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#9cd4d8' },
{ offset: 0.5, color: '#aec9fe' },
{ offset: 1, color: 'pink' }
])
}},
itemStyle: {
normal: {
color: function () {
return '#' + Math.floor(Math.random() * 0xffffff).toString(16);
}
}
},
data: data3
},
]
}
} }

注意:渲染数据的时机要选好,组装数据放在最后组装,如果把chartOption 定义在前面,再给series赋值图表是不会更新的,当然ec的方法里面可能会有,但是确挺麻烦的,优先考虑简单的。

有点colorful了,自己调整下。

angular cli 使用echarts的更多相关文章

  1. angular cli 反向代理实现跨域

    版本: 1.后端实现跨域(php) header("Access-Control-Allow-Origin: *"); // // 响应类型 // header('Access-C ...

  2. Angular环境准备和Angular cli

    Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...

  3. 迈向angularjs2系列(8):angular cli和angular2种子项目

    文章目录 1.angular cli快速搭建项目 2.angular2-seed 3.手动配置 题外话:如何更好的阅读本篇文章 一: angular cli的安装 Angular-cli(命令行界面, ...

  4. Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题

    前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...

  5. angular4.0 安装最新版本的nodejs、npm、@angular/cli的方法

    在使用ng项目的ui框架时,比如ng-zorro.angular Material,需要安装最新版本的@angular/cli: 配置ng-zorro框架 ng-zorro官网:https://ng. ...

  6. 使用Angular CLI生成 Angular 5项目

    如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angular/angular- ...

  7. Angular4---起步----环境配置安装@angular/cli

    学习angular,首先要搭建起angular的手脚架@angular/cli.首先需要NodeJS环境. 1.安装NodeJS 首先检查电脑是否安装了NodeJS环境,打开cmd命令行,运行node ...

  8. 使用Angular CLI进行单元测试和E2E测试

    第一篇文章是: "使用angular cli生成angular5项目" : http://www.cnblogs.com/cgzl/p/8594571.html 第二篇文章是: & ...

  9. 使用Angular CLI从蓝本生成代码

    第一篇文章是: "使用angular cli生成angular5项目" : http://www.cnblogs.com/cgzl/p/8594571.html 这篇文章主要是讲生 ...

随机推荐

  1. 整合多个maven工程时Spring配置加载JDBC问题

    问题叙述: 两个工程都通过JDBC访问mysql数据库,各自运行OK, 但合并成一个maven工程后,发现前一个工程访问数据库异常,貌似拿不到自己的DAO. 解决办法: 发现这两个工程的xml配置中, ...

  2. [TimLinux] JavaScript AJAX接收到的数据转换为JSON格式

    1. 接收数据 AJAX接收数据是通过xhr.responseText属性,这是一个属性不是一个方法,这个属性得到的数据为字符串. 2. 字符串内容 当服务器发送的是一个JsonResponse({' ...

  3. R语言绘制KS曲线

    更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 将代码封装在函数PlotKS_N里,Pred_Var是预测结果,可以是评分或概率形式:labels_Var是好坏标 ...

  4. 这十道经典Python笔试题,全做对算我输

    经常有小伙伴学了Python不知道是否能去找工作,可以来看下这十道题检验你的成果: 1.常用的字符串格式化方法有哪些?并说明他们的区别 a. 使用%,语法糖 print("我叫%s,今年%d ...

  5. ubuntu用户帐号

    与用户帐号相关的有几个非常重要的文件/ect/passwd,/etc/shadow,/etc/group /etc/passwd 执行 head -n 5 /ect/passwd显示前5行,内容如下: ...

  6. springboot中实现kafa指定offset消费

    kafka消费过程难免会遇到需要重新消费的场景,例如我们消费到kafka数据之后需要进行存库操作,若某一时刻数据库down了,导致kafka消费的数据无法入库,为了弥补数据库down期间的数据损失,有 ...

  7. Electron node integration enabled 设置

    解决办法 参考博客:https://blog.csdn.net/hwytree/article/details/103167175

  8. JS基础-DOM

    DOM DOM 事件的级别 DOM 事件模型 DOM 事件流 DOM 事件捕获的具体流程 Event 对象的常见应用 自定义事件 DOM概述 | MDN DOM | MDN DOM操作 DOM事件级别 ...

  9. 分布式事务解决方案,中间件 Seata 的设计原理详解

    作者:张乘辉 前言 在微服务架构体系下,我们可以按照业务模块分层设计,单独部署,减轻了服务部署压力,也解耦了业务的耦合,避免了应用逐渐变成一个庞然怪物,从而可以轻松扩展,在某些服务出现故障时也不会影响 ...

  10. Spring与Shiro整合 加载权限表达式

    Spring与Shiro整合 加载权限表达式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 如何加载权限表达式  我们在上章内容中画了一张图,里面有三个分项,用户 角色 权限: 那 ...