美文网首页可视化专题
echart 自定义 SVG 图各部分的对应关系

echart 自定义 SVG 图各部分的对应关系

作者: 小潤澤 | 来源:发表于2022-06-23 11:56 被阅读0次

    前言

    以官网给出的例子牛的图片为例子,这里主要解释一下后台返回到前端的代码如何映射到SVG图的各个部分

    学习链接:registerMap

    例子链接:例子

    SVG图片:


    JS部分:

    var echarts = require('echarts');
    
    var ROOT_PATH =
      'https://fastly.jsdelivr.net/gh/apache/echarts-website@asf-site/examples';
    
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option;
    
    $.get(ROOT_PATH + '/data/asset/geo/Beef_cuts_France.svg', function (svg) {
      echarts.registerMap('Beef_cuts_France', { svg: svg });
      option = {
        tooltip: {},
        visualMap: {
          left: 'center',
          bottom: '10%',
          min: 5,
          max: 100,
          orient: 'horizontal',
          text: ['', 'Price'],
          realtime: true,
          calculable: true,
          inRange: {
            color: ['#dbac00', '#db6e00', '#cf0000']
          }
        },
        series: [
          {
            name: 'French Beef Cuts',
            type: 'map',
            map: 'Beef_cuts_France',
            roam: true,
            emphasis: {
              label: {
                show: false
              }
            },
            selectedMode: false,
    
    ## 后台返回的数据
            data: [
              { name: 'Queue', value: 15 },
              { name: 'Langue', value: 35 },
              { name: 'Plat de joue', value: 15 },
              { name: 'Gros bout de poitrine', value: 25 },
              { name: 'Jumeau à pot-au-feu', value: 45 },
              { name: 'Onglet', value: 85 },
              { name: 'Plat de tranche', value: 25 },
              { name: 'Araignée', value: 15 },
              { name: 'Gîte à la noix', value: 55 },
              { name: "Bavette d'aloyau", value: 25 },
              { name: 'Tende de tranche', value: 65 },
              { name: 'Rond de gîte', value: 45 },
              { name: 'Bavettede de flanchet', value: 85 },
              { name: 'Flanchet', value: 35 },
              { name: 'Hampe', value: 75 },
              { name: 'Plat de côtes', value: 65 },
              { name: 'Tendron Milieu de poitrine', value: 65 },
              { name: 'Macreuse à pot-au-feu', value: 85 },
              { name: 'Rumsteck', value: 75 },
              { name: 'Faux-filet', value: 65 },
              { name: 'Côtes Entrecôtes', value: 55 },
              { name: 'Basses côtes', value: 45 },
              { name: 'Collier', value: 85 },
              { name: 'Jumeau à biftek', value: 15 },
              { name: 'Paleron', value: 65 },
              { name: 'Macreuse à bifteck', value: 45 },
              { name: 'Gîte', value: 85 },
              { name: 'Aiguillette baronne', value: 65 },
              { name: 'Filet', value: 95 }
            ]
          }
        ]
      };
      myChart.setOption(option);
    });
    
    option && myChart.setOption(option);
    

    这就带来一个问题,后台返回的数据是如何映射到SVG图的各个部分上的呢?

    通过查看 SVG 的html代码:


    其实后台传来的数据的 name,需要与SVG图片html代码相应部位的name 的内容相对应

    data: [
              { name: 'Queue', value: 15 },
              { name: 'Langue', value: 35 },
              { name: 'Plat de joue', value: 15 }
    ]
    
    ## 对应 SVG 图
    
    <path name="Queue"
       style="fill:#921319;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:square;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
       d="m 79.1875,42.625 -28.28125,0.71875 -16.25,7.0625 -19.09375,26.875 -5.65625,26.84375 -0.84375,105.25 10.03125,-0.28125 2.125,-108.5 3.34375,-7.9375 -0.15625,-0.0625 c 0.127935,-0.148353 0.252038,-0.334229 0.375,-0.5 l 4.625,-10.90625 c 1.160782,-3.331585 2.453475,-6.881357 4.25,-10.15625 l 1.6875,-4 1.21875,-0.3125 c 3.89521,-4.622798 9.81761,-8.067743 12.75,-9.5625 L 73.5,44.84375 l 3.5625,0 2.125,-2.21875 z"
       transform="matrix(0.6362812,0,0,0.6362812,7.6936433,-10.065285)"
       id="path3704"
       inkscape:connector-curvature="0" />
    
    <path name="Langue"
       style="fill:#921319;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:square;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
       d="m 536.375,106.46875 c 5.2885,14.34201 10.52945,33.1769 13.78125,43.21875 -0.0655,0.0771 -0.13181,0.1476 -0.21875,0.21875 l 16.5625,0.9375 10.5,-8.5 11,-17 -13.5,-18 -38.125,-0.875 z"
       id="path3702"
       transform="matrix(0.6362812,0,0,0.6362812,7.6936433,-10.065285)"
       inkscape:connector-curvature="0" />
    
    <path name="Plat de joue"
       style="fill:#921319;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:square;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
       d="m 479.875,78.9375 c 0.34134,7.189091 0.93966,14.327462 3.25,20.84375 7.5898,21.40693 18.02765,40.11508 34.125,55.21875 -5.6799,1.95428 -9.21826,3.15884 -11.875,4.09375 0.41224,0.77565 1.5132,2.80766 1.625,3.03125 0.81585,1.6317 7.32508,-4.6998 16.75,-8.25 6.91248,-2.6038 24.01189,-1.36903 26.40625,-4.1875 C 545.49285,135.28652 536.7499,102.74928 529.625,91.40625 520.34937,82.635259 489.68249,79.683068 479.875,78.9375 z"
       transform="matrix(0.6362812,0,0,0.6362812,7.6936433,-10.065285)"
       id="path3698"
       inkscape:connector-curvature="0" />
    

    对应上以后,后台数据json的数值就可以对应上相应的SVG部位了,


    相关文章

      网友评论

        本文标题:echart 自定义 SVG 图各部分的对应关系

        本文链接:https://www.haomeiwen.com/subject/bfnpvrtx.html