highcharts多条数据线设置不同的数据单位

在使用Highcharts展示数据时,遇到不同数据线单位不一致的问题。常规设置会导致所有数据单位相同。解决方法是将tooltip和formatter属性分别应用于每个数据系列,以定制各自的单位显示,确保鼠标悬浮提示和数据显示的标签正确反映出各自的数据单位。

用highcharts显示数据时,会有不同数据的单位不一样的问题。如下设置数据单位时会把所有数据的单位(标签)设置成一样的:

$('#charts').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: "传感器数据"
        },
        tooltip: {
          valueSuffix: '°C'
        },
        plotOptions:{
          column:{
            dataLabels:{
              enabled:true, //是否显示数据标签
              formatter: function() {
                return this.y + '°C';
              }
            }
          }
        },
        subtitle: {
            text: null,
        },
        xAxis: {
          categories: [1,2,3,4]
        },
        yAxis: {
            title: {
                text: '值'
            }
        },
        series: [{
            name: '温度',
            data: [23,21,24,20]
          },{
              name: '湿度',
              data: [32,40,45,33]
          }]
      });</span>

显示效果:


此时应将tooltip,formatter(其中tooltip用于鼠标悬浮显示的标签,formatter用于显示出来的数据的标签)设置到每个数据项中:

$('#charts').highcharts({
          chart: {
              type: 'column'
          },
          title: {
              text: "传感器数据"
          },
          plotOptions:{
            column:{
              dataLabels:{
                enabled:true //是否显示数据标签
              }
            }
          },
          subtitle: {
              text: null,
          },
          xAxis: {
              //categories: sensorUnit
            categories: [1,2,3,4]
          },
          yAxis: {
              title: {
                  text: '值'
              }
          },
          series: [{
              name: '温度',
              data: [23,21,24,20],
              tooltip: {
                valueSuffix: '°C'
              },
              dataLabels:{
                enabled:true, //是否显示数据标签
                formatter: function() {
                  return this.y + '°C';
                }
              }
          },{
              name: '湿度',
              data: [32,40,45,33],
              tooltip: {
                valueSuffix: '%'
              },
              dataLabels:{
                enabled:true, //是否显示数据标签
                formatter: function() {
                  return this.y + '%';
                }
              }
          }]
      });</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值