甜甜圈图表内的图像 - AmCharts

时间:2015-10-08 16:45:43

标签: javascript amcharts donut-chart

我试图将图像放在饼图的内半径内。所以它看起来像这样:

enter image description here

我发现最接近的是title and subtitle inside。但是它会在de svg中生成一个文本标签,所以我找不到显示图像的方法。

AmCharts.makeChart("chartdiv", {
"type": "pie",
"theme": "none",
"allLabels": [{
    "text": "This is chart title",
    "align": "center",
    "bold": true,
    "y": 220
},{
    "text": "Ans here's the subtitle as well",
    "align": "center",
    "bold": false,
    "y": 250
}],
"dataProvider": [{
    "title": "New",
    "value": 4852
}, {
    "title": "Returning",
    "value": 9899
}],
"titleField": "title",
"valueField": "value",
"labelRadius": -130,
"radius": "42%",
"innerRadius": "60%",
"labelText": ""
});

谢谢!

1 个答案:

答案 0 :(得分:3)

从技术上讲,无法在amCharts饼图中添加图像作为标签。

但是,您可以通过将该图像添加为图表容器的背景来轻松解决此问题。即:

#chartdiv {
  width: 210px;
  height: 210px;
  background: transparent url(logo.png) no-repeat center 70px;
}

以上适用于您的图表:



var chart = AmCharts.makeChart("chartdiv", {
  "type": "pie",
  "dataProvider": [{
    "slice": "First",
    "value": 34
  }, {
    "slice": "Second",
    "value": 66
  }],
  "valueField": "value",
  "titleField": "slice",
  "labelsEnabled": false,
  "pullOutRadius": 0,
  "innerRadius": 60,
  "allLabels": [{
    "text": "34%",
    "color": "#f40000",
    "size": 20,
    "align": "center",
    "y": "55%"
  }]
});

#chartdiv {
  width: 210px;
  height: 210px;
  margin: 0 auto;
  background: transparent url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/so1.png) no-repeat center 70px;
}

<script src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="http://www.amcharts.com/lib/3/pie.js"></script>
<div id="chartdiv"></div>
&#13;
&#13;
&#13;