前言
以官網(wǎng)給出的例子牛的圖片為例子,這里主要解釋一下后臺返回到前端的代碼如何映射到SVG圖的各個部分
學(xué)習(xí)鏈接: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,
## 后臺返回的數(shù)據(jù)
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);
這就帶來一個問題夯秃,后臺返回的數(shù)據(jù)是如何映射到SVG圖的各個部分上的呢顽冶?
通過查看 SVG 的html代碼:
其實(shí)后臺傳來的數(shù)據(jù)的 name沼沈,需要與SVG圖片html代碼相應(yīng)部位的name 的內(nèi)容相對應(yīng)
data: [
{ name: 'Queue', value: 15 },
{ name: 'Langue', value: 35 },
{ name: 'Plat de joue', value: 15 }
]
## 對應(yīng) 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" />
對應(yīng)上以后,后臺數(shù)據(jù)json的數(shù)值就可以對應(yīng)上相應(yīng)的SVG部位了,