先奉上官网地址,一切以官网为主
Echrts官网
Echarts的基本使用
cnpm i echarts -S
- 全局使用
在main.js中引入,然后用变量,将其挂载到vue的原型上
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
在组件中,就可以直接使用
this.$echarts.init()
稍后会说按需引入
下面来说一个我在项目中的总结
先看这个图 折柱混合
- 首先是对数据的处理
我们首先是要在页面画这么一个图,那么肯定是在mounted中执行的,但是画你得有数据,所有就把请求图表数据的方法,放在请求整体页面方法之后,然后把数据,当参数整体传给画图方法
- 下面就是使用map方法,对传入的数据,进行填充渲染
weeklyEcharts(data) { let myEchartWeekly = this.$echarts.init(document.getElementById("weeklyEcharts"));let optionWeekly = {tooltip: {trigger: "axis", },grid: {left: "3%",right: "4%",bottom: "8%",containLabel: true, }, legend: {x: "center",y: "bottom",itemHeight: 8,itemWidth: 8,}, color: ["#218AFF", "#47B8D9"], xAxis: [ {type: "category",data: data.map((item) => item.date), axisPointer: { type: "line", lineStyle: {color: "#6b6b6b", type: "solid", },},axisTick: { show: false,},axisLine: { show: true,lineStyle: {color: "#818286",},},},],yAxis: [ { type: "value",axisLabel: { formatter: function (value) {return value + "MH/s";},},axisTick: {show: false,}, splitLine: {show: true,lineStyle: {type: 'dashed'}}, axisLine: {show: false, lineStyle: {color: "#989898", },},},{type: "value",axisTick: {show: false,},axisLine: {show: false,lineStyle: {color: "#989898",},},splitLine: {show: true,lineStyle: {type: 'dashed'}},axisLabel: {formatter: function (value) {return value;},},},],series: [ {name: "算力",type: "bar", color: "#1362FE",barWidth: 40, itemStyle: { emphasis: {barBorderRadius: 30,},normal: {barBorderRadius: [6, 6, 0, 0],marginBottom: 20,},},data: data.map((item) => (item.hashrate / 1000000).toFixed(2)), },{name: "收益",color: "#FB7829",type: "line", smooth: true, symbol: "none",yAxisIndex: 1, data: data.map((item) =>(item.amount / 1000000000000000000).toFixed(5)), },],};myEchartWeekly.setOption(optionWeekly); },
- 以上就是对折柱绘制的总结。我最欣赏的,不是他的参数配置,而是图标数据,作为整个参数传递。最后在图表中,利用map去一个一个取值
双折线图
earnEcharts(data) {let myEchartLine = this.$echarts.init(document.getElementById("earnEcharts"));let optionLine = {tooltip: {trigger: "axis",},legend: {x: "center",y: "bottom",padding: [0, 0, 0, 0],data: [{ name: "收益", icon: "rect" },{ name: "均值", icon: "rect" }, ],itemHeight: 8, itemWidth: 8,},color: ["rgba(17, 99, 255)", "rgba(243, 122, 44)"],grid: {left: "3%",right: "4%",bottom: "7%",containLabel: true,},xAxis: {type: "category",boundaryGap: false,data: data.map((item) => { let date = new Date(item.time * 1000); return this.$moment(date).format("MM-DD hh:00");}),axisLabel: { interval: 50,formatter: (value, idx) => {var date = new Date(value);return this.$moment(date).format("MM-DD hh:00");},},axisTick: {show: false,},axisLine: {show: false,lineStyle: {color: "#989898",},},},axisTick: {show: false,},axisLine: {show: false,lineStyle: {color: "#989898",},},},yAxis: {type: "value",axisLine: {show: false,lineStyle: {color: "#989898",},},axisTick: {show: false,},},series: [{name: "收益",type: "line",stack: "总量",smooth: true,symbol: "none", areaStyle: {color: "rgba(17, 99, 255, 0.2)"},data: data.map((item) => {return item.amount;}),itemStyle: {normal: {lineStyle: {color: "#218AFF",width: 0.5},},},},{name: "均值",type: "line",stack: "总量", smooth: true,symbol: "none", areaStyle: {color: "rgba(243, 122, 44, 0.2)"}, data: data.map((item) => {return item.meanAmount;}),itemStyle: {normal: {lineStyle: {color: "#47B8D9",width: 0.5},},},},],};myEchartLine.setOption(optionLine);},
双折现图的难点在于对数据的处理,后端返回的数据是这样的
他要实时的时间所对应的数据,来描绘图标,但是图标展示却是这样的
- 接下来就直接看对数据的处理了,
- 要求是只要格式是 月-日 时:00 ,我这里用了moment,
- this.$moment(date).format(“MM-DD hh:00”) 这样就可以转化成自己想要的格式
axisLabel: {interval: 50, formatter: (value) => {var date = new Date(value);return this.$moment(date).format("MM-DD hh:00");},},
我这是整体用的插件
安装 momen
cnpm i moment -S
-在main.js中导入,并且挂载到vue原型上
import echarts from 'echarts'
Vue.prototype.$moment = moment;
均值是0.01所以这个图应该是这样的
下面再来看一个图,这个图也是两根折现,但是他是年月日
切换的
- 图跟下面日期就不多上了,上一个图已经详细说明。这个图将说说年月日切换这个
- 首先要明白,年月日切换也是调接口,所以现在问题就变得异常简单,点击年月日,传不同的参数给图表接口,那倒数据,渲染即可
上代码 - 点击切换日期代码
<div class&#61;"data-main-nyr"><p&#64;click&#61;"getIsActive(1, 1)":class&#61;"[isactive &#61;&#61; 1 ? &#39;data-main-nyr-p&#39; : &#39;&#39;]">日</p><p&#64;click&#61;"getIsActive(2, 1)":class&#61;"[isactive &#61;&#61; 2 ? &#39;data-main-nyr-p&#39; : &#39;&#39;]">周</p><p&#64;click&#61;"getIsActive(3, 1)":class&#61;"[isactive &#61;&#61; 3 ? &#39;data-main-nyr-p&#39; : &#39;&#39;]">月</p></div>
getIsActive(i, index) {if (index &#61;&#61; 1) {this.isactive &#61; i;} else {this.isactive1 &#61; i;}if (i &#61;&#61; 1) {this.getStatisticsList("d", index);} else if (i &#61;&#61; 2) {this.getStatisticsList("w", index);} else {this.getStatisticsList("m", index);}},getStatisticsList(index, i) {getStatistics({ zoom: index },(res) &#61;> {if (i &#61;&#61; 1) {this.manyLine(res.data.data, index);} else if (i &#61;&#61; 0) {this.manyLine1(res.data.data, index);} else {this.manyLine(res.data.data, index);this.manyLine1(res.data.data, index);}},(err) &#61;> {console.log(err);});},
饼图
drawChart(datalist) {const myEchart &#61; echarts.init(document.getElementById("main"));const option &#61; {tooltip: {trigger: "item", },color: ["rgba(30, 208, 160, 1)", "rgba(247, 122, 41, 1)", "red"],series: [{name: "份额",type: "pie",radius: ["54%", "70%"],avoidLabelOverlap: false,label: {show: false,position: "center",},labelLine: {show: false,},data: [{ value: datalist.valid_shares24h, name: "有效份额" },{ value: datalist.stale_shares24h, name: "延迟份额" },{ value: datalist.invalid_shares24h, name: "无效份额" },],},],};myEchart.setOption(option);},
- 这个饼图也没啥说的&#xff0c;就是中间的那个
状态
。我们是根据右侧的数据&#xff0c;来展示饼图里面显示的文字。所以需要做个数据处理,并且样式需要定位
.share-main-photo {position: absolute;left: 60px;top: 110px;display: flex;flex-direction: column;align-items: center;justify-content: center;
}
<p>健康度</p><p>{{(datalist.valid_shares24h / datalist.all) * 100 >&#61; 90? "极好": (datalist.valid_shares24h / datalist.all) * 100 >&#61; 70? "良好": (datalist.valid_shares24h / datalist.all) * 100 >&#61; 50? "较差": "极差"}}</p>
横条展示&#xff0c;类似于百分比&#xff0c;这个是自己实现的
首先实现一个全红的样式横条&#xff0c;然后里面的样式&#xff0c;通过计算来实现
<div class&#61;"line-red"><pclass&#61;"line-green":style&#61;"{ width: parseInt((2 / (2 &#43; 8)) * 410) &#43; &#39;px&#39; }"></p></div>
.line-red {margin-top: 6px;margin-right: 20px;height: 26px;background: #f71664;
}
.line-green {height: 26px;background: #21ce9d;
}