2

我使用嵌套饼图显示分层数据,我希望能够按框显示特定颜色

例如这里:假设我只想将此框的颜色从黄色更改为灰色。 在此处输入图像描述

你可以在这里测试它: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 代码中没有什么特别的......

你知道这是否可能吗?

4

1 回答 1

3

我找到了这个例子:https ://codepen.io/team/amcharts/pen/PQrvJr在那里你可以看到“荷兰”在两个级别之间有不同的颜色,但在 javascript 代码中没有什么特别的......

造成这种情况的原因是:

pieSeries.colors.step = 2;

a的step属性ColorSet导致它跳过其列表中的颜色(在这种情况下,自动生成)。如果您将外部系列的每 2 种颜色与内部系列进行比较,您会发现它们是一致的。

至于为特定切片着色,有不同的方法可以解决。

现在,这就是派系列如何获得颜色:

// Earlier in code
var colorSet = new am4core.ColorSet();
colorSet.list = ["#18dc08", "#e2ed0c", "#3be791", "#0eade6", "#8f55e9", "#dd0a4b"].map(function(color) {
    return new am4core.color(color);
});

// createPie() ...
pieSeries.colors = colorSet;

如果您确切知道要更改哪个系列中的哪个切片,则可以提供一个新ColorSet的列表,其列表相似但针对该切片进行了调整,例如:

if (chart.series.length === 3) {
    var distinctColors = new am4core.ColorSet();
  distinctColors.list = colorSet.list.map(function(color, index) {
    if (index === 1) {
      return am4core.color("gray");
    }
    return color;
  });
  pieSeries.colors = distinctColors;
} else {
  pieSeries.colors = colorSet;
}

不过,理想情况下,您会使用适配器,因此每当fill要应用 a 时,您都可以提供逻辑来确定要提供什么颜色,例如将 Sector1: categor3 着色为黑色(不过,由于其他东西在编码):

pieSeries.slices.template.adapter.add("fill", function(fill, target) {
  if (target.dataItem.dataContext && target.dataItem.dataContext.name === "Sector1" &&  targetAttribute === "category3") {
    return am4core.color("black");
  }
  return fill;
});

这是一个演示(我还用"over"适配器替换了您的事件代码tooltipHTML):

https://jsfiddle.net/notacouch/v26zdency/

于 2019-04-26T22:14:04.123 回答