react前端如何使用echart仪表盘

使用三个仪表盘展示数据,并且在同一行排列

import { EChartsOption ,SeriesOption} from 'echarts';

import EChartsReact from 'echarts-for-react';

1、定义仪表盘

const pancreatic: EChartsOption[] = [

        {

          renderer: 'svg',

          title: {

            text: '',

            left: 'center',

          },

          tooltip: {},

          series: [

            {

              name: '',

              type: 'gauge',

              startAngle: -180,

              endAngle: 0,

              max: 1000,        // 最大的数值

              splitNumber: 2,

              axisLabel: {      // 刻度标签。

                show: true,       // 是否显示标签,默认 true。

                formatter: '{value}%',  // 刻度标签的内容格式器,支持字符串模板和回调函数两种形式。 示例:// 使用字符串模板,模板变量为刻度默认标签 {value},如:formatter: '{value} kg'; // 使用函数模板,函数参数分别为刻度数值,如formatter: function (value) {return value + 'km/h';}

              },

              itemStyle: {      // 仪表盘指针样式。

                color: "red",     // 指针颜色,默认(auto)取数值所在的区间的颜色

              },

              detail: { formatter: '{value}%' },

              data: [{value:0}],

            },

          ],

        },

        {

          renderer: 'svg',

          title: {

            text: '',

            left: 'center',

          },

          tooltip: {},

          series: [

            {

              name: '',

              type: 'gauge',

              startAngle: -180,

              endAngle: 0,

              max: 1000,        // 最大的数据值,默认 100 。映射到 maxAngle。

              splitNumber: 2,

              axisLabel: {      // 刻度标签。

                show: true,       // 是否显示标签,默认 true。

                formatter: '{value}%',  // 刻度标签的内容格式器,支持字符串模板和回调函数两种形式。 示例:// 使用字符串模板,模板变量为刻度默认标签 {value},如:formatter: '{value} kg'; // 使用函数模板,函数参数分别为刻度数值,如formatter: function (value) {return value + 'km/h';}

              },

              itemStyle: {      // 仪表盘指针样式。

                color: "green",     // 指针颜色,默认(auto)取数值所在的区间的颜色

              },

              detail: { formatter: '{value}%' },

              data: [{value:0}],

            },

          ],

        },

        {

          renderer: 'svg',

          title: {

            text: '',

            left: 'center',

          },

          tooltip: {},

          series: [

            {

              name: '',

              type: 'gauge',

              startAngle: -180,

              endAngle: 0,

              max: 1000,        // 最大的数据值,默认 100 。映射到 maxAngle。

              splitNumber: 2,

              axisLabel: {      // 刻度标签。

                show: true,       // 是否显示标签,默认 true。

                formatter: '{value}%',  // 刻度标签的内容格式器,支持字符串模板和回调函数两种形式。 示例:// 使用字符串模板,模板变量为刻度默认标签 {value},如:formatter: '{value} kg'; // 使用函数模板,函数参数分别为刻度数值,如formatter: function (value) {return value + 'km/h';}

              },

              itemStyle: {      // 仪表盘指针样式。

                color: "#FFA500",     // 指针颜色,默认(auto)取数值所在的区间的颜色

              },

              detail: { formatter: '{value}%' },

              data: [{value:0}],

            },

          ],

        },

      ];

    const [pancreaticoption,setpancreaticoption]=useState<EChartsOption[]>(pancreatic)

2、从后端获取数据

const response = await linedata(id);

const pancreaticOptions = pancreaticoption.map((opt,index) => {

            const series=(opt.series as SeriesOption[])

            const title=opt.title

            if(index===0){

              return {...opt,title:{...title,text:response.data.pancreaticInsulin.insulinAssessment.insulinBaseConclusion},series:[{...series[0], data:response.data.pancreaticInsulin.basic}]}

            }

            else if(index===1){

              return {...opt,title:{...title,text:response.data.pancreaticInsulin.insulinAssessment.insulinPeakConclusion},series:[{...series[0], data:response.data.pancreaticInsulin.peak}]}

            }

            else if(index===2){

              return {...opt,title:{...title,text:response.data.pancreaticInsulin.insulinAssessment.insulinSlopeConclusion},series:[{...series[0], data:response.data.pancreaticInsulin.slope}]}

            }

            return opt

          });

          setpancreaticoption(pancreaticOptions);

后端返回数据格式为:

"data": {

      "pancreaticInsulin":{

                "basic":[{value:60}],

                "peak":[{value:100}],

                "slope":[{value:200}]

            },

}

3、把仪表盘渲染在页面

<div style={{ display: 'flex',height:'300px' }}>

                    {pancreaticoption.map((option, index) => (

                      <div key={index} style={{flex: 1,   margin: '20px 10px'  }}>

                        <EChartsReact option={option} />

                      </div>

                    ))}

                  </div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值