React 中使用Echarts實(shí)現(xiàn)多系列漸變色柱狀圖

ReactEcharts 基本用法
相信很多新手對(duì)于 echarts 在React 中的使用會(huì)有許多疑惑,本文一起學(xué)習(xí)下,實(shí)現(xiàn)多系列漸變色柱狀圖!fighting!!
echarts 十分強(qiáng)大,能夠完美做到適配多重屏幕尺寸,不得不感慨,這些程序員用心做事情,值得我們學(xué)習(xí)、致敬!


多系列漸變色柱狀圖.png

一、導(dǎo)入

import ReactEcharts from 'echarts-for-react';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/bar';// 引入柱狀圖
import 'echarts/lib/component/tooltip';// 引入提示框和標(biāo)題組件
import 'echarts/lib/component/title';// 引入標(biāo)題組件

二、參數(shù)配置

  //柱狀圖 圖表樣式 數(shù)據(jù)參數(shù)配置
  const getOption = () => {
    return {
      legend: {//圖例配置
        right: "10%",
        icon: 'circle',
        textStyle: {
          color: 'rgba(161,169,183,1)',
          fontSize: 12,
        },
        itemGap: 3,//間距
      },

      //表頭配置
      title: {
        text: "區(qū)域統(tǒng)計(jì)",
        subtext: '單位:(個(gè))',
        subtextStyle: {
          fontSize: 11,
          color: '#939fbd',
        },
        textStyle: {
          fontSize: 14,
          align: "left",
          color: '#435372',
        },
      },
      dataset: {
         source: [
          ['可為空', '在線11數(shù)量', '離線11數(shù)量', '在線22數(shù)量', '離線22數(shù)量'],
          ['上海', 665, 62, 958,94],
          ['北京', 202, 22, 880,60],
          ['浙江', 583,50, 300, 55],
          ['廣州', 816,80, 550, 40],
        ],
      },
      tooltip: {},//配置指示器
      xAxis: [{
        type: 'category',
        nameTextStyle: {
          color: "rgba(147,159,189,1)",//這個(gè)是無效的,默認(rèn)取axisLine.lineStyle的顏色
          fontSize: 12,
        },
        axisLine: {
          show: false,
          lineStyle: {
            color: "rgba(147,159,189,1)"
          }
        },
        axisTick: {
          show: false
        }
      }],
      yAxis: [{
        show: true,
        nameTextStyle: {
          color: "rgba(147,159,189,1)",
          fontWeight: "normal",
          fontSize: 12,
        },
        axisLine: {
          show: false,
          lineStyle: {
            color: "rgba(147,159,189,1)"
          }
        },
        axisTick: {
          show: false
        }
      }],
      series: [
        {
          type: 'bar',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                {offset: 0, color: '#c6b3fc'},
                {offset: 1, color: '#9c7bf7'}
              ]
            ),
          },
          barWidth: 12,
          barCategoryGap: '2%',
          barGap: '40%',
        },
        {
          type: 'bar',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                {offset: 0, color: '#57d2ff'},
                {offset: 1, color: '#33a8ff'}
              ]
            ),
          },
          barWidth: 12,
          barCategoryGap: '2%',
          barGap: '40%',
        },
        {
          type: 'bar',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                {offset: 0, color: '#76f4ad'},
                {offset: 1, color: '#11d2cd'}
              ]
            ),
          },
          barWidth: 12,
          barCategoryGap: '2%',
          barGap: '40%',
        },

        {
          type: 'bar',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                {offset: 0, color: '#FFB5C5'},
                {offset: 1, color: '#EEA9B8'}
              ]
            ),
          },
          barWidth: 12,
          barCategoryGap: '2%',
          barGap: '40%',
        }
      ]
    };
  }

三、布局引入

return <div>
  <div className={styles.bar}>
          <ReactEcharts
            option={getOption()}
            notMerge={true}
            lazyUpdate={true}
            style={{width: '100%', height: '100%'}}
          />
        </div>
</div>

四、css樣式

.bar {
  margin-top: 15px;
  border-radius: 4px;
  background-color: #ffffff;
  /* width: calc(100vw * 3 / 5); */
  width: 100%;
  @media screen and (max-width: 1280px) {
    width: 90vw;
  }
  height: calc(34 / 100 * 100vh);
  padding: 19px 0px 0px 24px;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容