从0到有,教你实现渐变色echarts 折线图

5 篇文章 0 订阅

 先在模板中写一个div盒子

<template>

<div id="trendChart"></div>

</template>

 可以在生命周期函数mounted里面加载,也可以封装为一个函数,然后在mounted里面调用;

var chartDom = document.getElementById("trendChart");    //获取节点

var myChart = echarts.init(chartDom);    //初始化图表

var option;

option = {

xAxis: {

axisTick: { show: false },

type: "category",

data:['11-1','11-2','11-3','11-4','11-5','11-6',],

axisLabel: {

       // 设置x轴文字颜色、字体大小、字体风格

    color: "#b3b3b3",

    fontSize:  11,

    fontFamily: "SourceHanSansCN-Light",

},

axisLine: {

//设置x轴线条的粗细

    lineStyle: {

        height: 1,

     },

},

},

yAxis: {

type: "value",

data: moneyArr,

axisLabel: {

    // 设置y轴文字颜色

    color: "#b3b3b3",

    fontSize: 11,

    fontFamily: "SourceHanSansCN-Light",

},

axisLine: {

    lineStyle: {

        height: 1,

    },

},

// 设置y轴网格线

splitLine: {

    show: true,

    lineStyle: {

        color: ["rgb(235,235,235)"],

        width: 1,

        type: "solid",

    },

},

},

grid: {

//设置表格偏移量

top: "2.5%",                      

left: "5%",

right: "8%",

bottom: "7%",

containLabel: true,

},

series: [

{

data:[50,-500,500,500,-1500,1200],

type: "line",

symbol: "circle",

symbolSize: 6,

itemStyle: {

     color: "blue", //折点的颜色

     shadowColor: "blue",

     normal: {

          lineStyle: {

         // 系列级个性化折线样式

                width:  2, //线条宽度

                type: "solid",

                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                {  offset: 0,  color: "rgb(83,194,61)" },

                {  offset: 0.5,  color: "rgb(147,168,14)"},

                {  offset: 1,  color: "rgb(239,38,36)" },

            ]), 

       },

   },

},

},

],

tooltip: {

show: true,

trigger: "axis", //触发方式

axisPointer: {

    type: "line",

},

textStyle: {

    align: "left",

    fontSize: 12,

},

formatter: "日期: {b0}<br />收益: {c0}",

position: "left",

show: true,

},

visualMap: {

// lt(小于,less than),

// gt(大于,greater than),

// lte(小于等于 less than or equals),

// gte(大于等于,greater than or equals)

//大于0的为绿色,小于0的为红色

show: false,

pieces: [

{  gt: 0,  color: "rgb(83,194,61)" },

{  lt: -0, color: "rgb(239,38,36)"},

],

seriesIndex: 0,

},

};

//将数据点显示为光圈

let effectScatter = {

type: "effectScatter",

coordinateSystem: "cartesian2d",

data: effectScatterData, //2d坐标系

symbol: "circle",

symbolSize: 6,

showEffectOn: "render",

rippleEffect: {

    brushType: "stroke",

},

hoverAnimation: true,

itemStyle: {

    normal: {

       shadowBlur: 10,

       color: function (params) {

            return colors[params.dataIndex];

        },

    },

},

// zlevel: 1,

};

for (let item in option.series) {

let data = option.series[item].data;

for (let i = 0; i < data.length; i++) {

    effectScatterData.push([option.xAxis.data[i], data[i]]);

    if (data[i] >= 0) {

         colors[i] = "rgb(83,194,61)";

        symbolAdd++;

    } else {

        colors[i] = "rgb(239,38,36)";

       symbolReduce++;

    }

}

}

if (symbolAdd == 0) {

//全为负数,颜色为红色

option.series[0].itemStyle = {

    normal: {

         lineStyle: {

         // 系列级个性化折线样式

        width: 2, //线条宽度

        type: "solid",

        color: "rgb(239,38,36)",

    },

},

   };

} else if (symbolReduce == 0) {

//全为正数,颜色为绿色

option.series[0].itemStyle = {

     normal: {

         lineStyle: {

         // 系列级个性化折线样式

         width:  2, //线条宽度

         type: "solid",

         color: "rgb(83,194,61)",

       },

    },

};

}

option.series.push(effectScatter);

option && myChart.setOption(option);

//根据窗口大小自动调整图表大小

window.addEventListener("resize", function () {

myChart.resize();

})

显示效果如下:

当数据都为正数时,全为绿色

当数据有正有负时为红绿渐变

当数据全为负数时,全为红色

以上就是全部分享结果了,如有疑问,欢迎留言!!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值