我使用嵌套饼图显示分层数据,我希望能够按框显示特定颜色
你可以在这里测试它:https ://jsfiddle.net/aliz/gwz7om9e/
javascript代码:
(function(){
/*
Define all colors used.
*/
var colorSet = new am4core.ColorSet();
colorSet.list = ["#18dc08", "#e2ed0c", "#3be791", "#0eade6", "#8f55e9", "#dd0a4b"].map(function(color) {
return new am4core.color(color);
});
var size = 16.6;
/*
Creates a pie slice
*/
function createPie(chart, targetAttribute, positionRadiusInPie){
var pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = "size"; // the percentage size so all slice are equals
pieSeries.dataFields.category = "name";
pieSeries.slices.template.strokeWidth = 1;
pieSeries.slices.template.strokeOpacity = 1;
pieSeries.alignLabels = false;
pieSeries.ticks.template.disabled = true;
// Enable label
pieSeries.labels.template.text = "{"+ targetAttribute +".length}";
pieSeries.labels.template.radius = am4core.percent(positionRadiusInPie); // magic number
// color
pieSeries.colors = colorSet;
pieSeries.labels.template.fill = am4core.color("white");
pieSeries.slices.template.stroke = am4core.color("#fff");
// Disable sliding out of slices
//pieSeries.slices.template.states.getKey("hover").properties.shiftRadius = 0;
//pieSeries.slices.template.states.getKey("hover").properties.scale = 1.1;
// Tooltip conf
// https://www.amcharts.com/docs/v4/reference/pieseries/#Events
pieSeries.slices.template.events.on("over", function(ev){
// https://www.amcharts.com/docs/v4/tutorials/tooltips-with-rich-html-content/
var modalContent = [
'<center><strong>'+ev.target.dataItem.dataContext.name+'</strong></center>',
'<center><i>'+targetAttribute+'</i></center>',
'<hr />',
].join("");
if(!ev.target.dataItem.dataContext[targetAttribute] ||
ev.target.dataItem.dataContext[targetAttribute].length == 0){
modalContent += [
'<span>No information</span>'
].join("");
}else{
modalContent += [
'<table>',
'<tbody>'
].join("");
for(var i = 0; i < ev.target.dataItem.dataContext[targetAttribute].length; i++){
var item = ev.target.dataItem.dataContext[targetAttribute][i];
modalContent += [
'<tr>',
'<th align="left">Name</th>',
'<td align="right">'+item.name+'</td>',
'</tr>',
].join("");
}
modalContent += [
'</tbody>',
'</table>',
'<hr />'
].join("");
}
pieSeries.slices.template.tooltipHTML = modalContent;
}, this);
// https://www.amcharts.com/docs/v4/reference/columnseries/#Events
pieSeries.slices.template.events.on("inited", function(ev) {
if(!ev.target.dataItem.dataContext[targetAttribute] || ev.target.dataItem.dataContext[targetAttribute].length == 0){
ev.target.fillOpacity = 0.5;
}
}, this);
}
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.PieChart);
// Let's cut a hole in our Pie chart the size of 20% the radius
chart.innerRadius = am4core.percent(20);
chart.pullOutRadius = 0;
// Add data: can be fetched from a webservice
chart.data = [
{
"name": "Sector1",
"color": colorSet.list[0].hex,
"size": size,
"category1": [
{
name: "test 1"
},
{
name: "titi"
}
],
"category2": [
//{
// name: "test 1"
// }
],
"category3" : [
//{
// name: "test 1"
//}
],
"category4" : [
{
name: "test 1"
}
]
},
{
"name": "Sector2",
"color": colorSet.list[1].hex,
"size": size,
"category1": [
{
name: "test 1"
}
],
"category2": [
{
name: "test 1"
}
],
"category3" : [
{
name: "test 1"
}
],
"category4" : [
{
name: "test 1"
}
]
},
{
"name": "Sector3",
"color": colorSet.list[2].hex,
"size": size,
"category1": [
{
name: "test 1"
}
],
"category2": [
//{
// name: "test 1"
//}
],
"category3" : [
{
name: "test 1"
}
],
"category4" : [
{
name: "test 1"
}
]
},
{
"name": "Sector4",
"color": colorSet.list[3].hex,
"size": size,
"category1": [
{
name: "test 1"
}
],
"category2": [
{
name: "test 1"
}
],
"category3" : [
{
name: "test 1"
}
],
"category4" : [
{
name: "test 1"
}
]
},
{
"name": "Sector5",
"color": colorSet.list[4].hex,
"size": size,
"category1": [
{
name: "test 1"
}
],
"category2": [
{
name: "test 1"
}
],
"category3" : [
{
name: "test 1"
}
],
"category4" : [
{
name: "test 1"
}
]
},
{
"name": "Sector6",
"color": colorSet.list[5].hex,
"size": size,
"category2": [
{
name: "test 1"
}
],
"category1": [
{
name: "test 1"
}
],
"category3" : [
{
name: "test 1"
}
],
"category4" : [
{
name: "test 1"
}
]
}
];
/*
category1
*/
createPie(chart, "category1", -28);
/*
category2
*/
createPie(chart, "category2", -20);
/*
category3
*/
createPie(chart, "category3", -10);
/*
Business ops
*/
createPie(chart, "category4", -8);
/*
Legend builder
// https://www.amcharts.com/docs/v4/reference/ipiechartevents/
*/
chart.events.on('inited', function(event) {
// populate our custom legend when chart renders
chart.customLegend = document.getElementById('legend');
// https://www.amcharts.com/docs/v4/tutorials/truncating-legend-labels/
//chart.customLegend.labels.template.truncate = true;
for (var i in chart.data) {
var row = chart.data[i];
var color = chart.data[i].color;
var name = row.name;
chart.customLegend.innerHTML +=
[
'<div class="legend-item" id="legend-item-' + i + '" style="color: ' + color + ';">',
'<div class="legend-marker" style="background: ' + color + '"></div>',
name,
'</div>'
].join("");
}
});
})()
我找到了这个例子:https ://codepen.io/team/amcharts/pen/PQrvJr 在那里你可以看到“荷兰”在两个级别之间有不同的颜色,但在 javascript 代码中没有什么特别的......
你知道这是否可能吗?