简体   繁体   English

echarts为数据添加自定义注释

[英]echarts add custom notes to data

this is the code of an example of geomap on js echarts这是js echarts上geomap的一个例子的代码

    var data = [
  { name: '海门', value: 115 },
  { name: '鄂尔多斯', value: 111 },
  
];
var geoCoordMap = {
  海门: [121.15, 31.89],
  鄂尔多斯: [109.781327, 39.608266]
};
var convertData = function (data) {
  var res = [];
  for (var i = 0; i < data.length; i++) {
    var geoCoord = geoCoordMap[data[i].name];
    if (geoCoord) {
      res.push({
        name: data[i].name,
        value: geoCoord.concat(data[i].value),
        text: geoCoord.concat(data[i].text)
      });
    }
  }
  return res;
};
function renderItem(params, api) {
  var coords = [
    [116.7, 39.53],
    [103.73, 36.03],
    [112.91, 27.87],
    [120.65, 28.01],
    [119.57, 39.95]
  ];
  var points = [];
  for (var i = 0; i < coords.length; i++) {
    points.push(api.coord(coords[i]));
  }
  var color = api.visual('color');
  return {
    type: 'polygon',
    shape: {
      points: echarts.graphic.clipPointsByRect(points, {
        x: params.coordSys.x,
        y: params.coordSys.y,
        width: params.coordSys.width,
        height: params.coordSys.height
      })
    },
    style: api.style({
      fill: color,
      stroke: echarts.color.lift(color)
    })
  };
}
option = {
  backgroundColor: 'transparent',
  title: {
    text: '全国主要城市空气质量',
    subtext: 'data from PM25.in',
    sublink: 'http://www.pm25.in',
    left: 'center',
    textStyle: {
      color: '#fff'
    }
  },
  tooltip: {
    trigger: 'item'
  },
  bmap: {
    center: [104.114129, 37.550339],
    zoom: 5,
    roam: true,
    mapStyle: {
      styleJson: [
        {
          featureType: 'water',
          elementType: 'all',
          stylers: {
            color: '#044161'
          }
        },
        {
          featureType: 'land',
          elementType: 'all',
          stylers: {
            color: '#004981'
          }
        },
        {
          featureType: 'boundary',
          elementType: 'geometry',
          stylers: {
            color: '#064f85'
          }
        },
        {
          featureType: 'railway',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'highway',
          elementType: 'geometry',
          stylers: {
            color: '#004981'
          }
        },
        {
          featureType: 'highway',
          elementType: 'geometry.fill',
          stylers: {
            color: '#005b96',
            lightness: 1
          }
        },
        {
          featureType: 'highway',
          elementType: 'labels',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'arterial',
          elementType: 'geometry',
          stylers: {
            color: '#004981'
          }
        },
        {
          featureType: 'arterial',
          elementType: 'geometry.fill',
          stylers: {
            color: '#00508b'
          }
        },
        {
          featureType: 'poi',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'green',
          elementType: 'all',
          stylers: {
            color: '#056197',
            visibility: 'off'
          }
        },
        {
          featureType: 'subway',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'manmade',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'local',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'arterial',
          elementType: 'labels',
          stylers: {
            visibility: 'off'
          }
        },
        {
          featureType: 'boundary',
          elementType: 'geometry.fill',
          stylers: {
            color: '#029fd4'
          }
        },
        {
          featureType: 'building',
          elementType: 'all',
          stylers: {
            color: '#1a5787'
          }
        },
        {
          featureType: 'label',
          elementType: 'all',
          stylers: {
            visibility: 'off'
          }
        }
      ]
    }
  },
  series: [
    {
      name: 'pm2.5',
      type: 'scatter',
      coordinateSystem: 'bmap',
      data: convertData(data),
      encode: {
        value: 2
      },
      symbolSize: function (val) {
        return val[2] / 10;
      },
      label: {
        formatter: '{b}',
        position: 'right'
      },
      itemStyle: {
        color: '#ddb926'
      },
      emphasis: {
        label: {
          show: true
        }
      }
    },
    {
      name: 'Top 5',
      type: 'effectScatter',
      coordinateSystem: 'bmap',
      data: convertData(
        data
          .sort(function (a, b) {
            return b.value - a.value;
          })
          .slice(0, 6)
      ),
      encode: {
        value: 2
      },
      symbolSize: function (val) {
        return val[2] / 10;
      },
      showEffectOn: 'emphasis',
      rippleEffect: {
        brushType: 'stroke'
      },
      hoverAnimation: true,
      label: {
        formatter: '{b}',
        position: 'right',
        show: true
      },
      itemStyle: {
        color: '#f4e925',
        shadowBlur: 10,
        shadowColor: '#333'
      },
      zlevel: 1
    },
    {
      type: 'custom',
      coordinateSystem: 'bmap',
      renderItem: renderItem,
      itemStyle: {
        opacity: 0.5
      },
      animation: false,
      silent: true,
      data: [0],
      z: -10
    }
  ]
};

and here is how it shows这是它的显示方式

在此处输入图像描述

now I want to add some random string after value of each city, for instance:现在我想在每个城市的值之后添加一些随机字符串,例如:

Top 5
鄂尔多斯 111
many green trees

what property could I add in echarts data part? echarts数据部分可以添加什么属性? I see that only number is available on data part, and in which method could I add the string "many green trees" into the visualization?我看到数据部分只有数字可用,我可以用哪种方法将字符串“many green trees”添加到可视化中?

I have found a method to contain words in the box, in case you want to know.我找到了一种在方框中包含单词的方法,以防你想知道。

  1. create a new dictionary to store your words创建一个新词典来存储您的单词

    var stringmap = { 海门: ['many red trees'], 鄂尔多斯: ['many green trees'] }; var stringmap = { 海门: ['许多红树'], 鄂尔多斯: ['许多绿树'] };

  2. Add the string into values, store in the last element like 3 (because map.geo recognize 0,1 as coordinate and 2 as size of symbols), add these within convertData For loop将字符串添加到值中,存储在最后一个元素中,例如 3 (因为 map.geo 将 0,1 识别为坐标,将 2 识别为符号大小),将这些添加到convertData For 循环中

    var stringname=stringmap[data[i].name]; var stringname=stringmap[数据[i].name]; var new=[];变种新= []; new.push(data[i].value) new.push(stringmap) new.push(数据[i].value) new.push(stringmap)

and replace geocoord.concat into并将 geocoord.concat 替换为

   value: geoCoord.concat(new)
  1. show these words in tooltip, change formatter在工具提示中显示这些词,更改格式化程序

    tooltip:{ trigger:'item', confine:true formatter: function(params){ tip=params.name+ ":" + "<br/>"+params.value[3] return tip } }

then u got it然后你明白了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM