简体   繁体   English

如何更改 amCharts TreeMap 中更深层次项目的颜色?

[英]How do I change the color of deeper level items in an amCharts TreeMap?

The amCharts docs say "Sub-items (children) automatically inherit color from their parent." amCharts 文档说“子项(子项)自动从其父项继承颜色。” But I want to change the color of sub-items depending on a color attribute in my data.但我想根据数据中的颜色属性更改子项的颜色。 For the top level it works by using chart.dataFields.color = 'color' , but I have no clue how it works for deeper levels.对于顶层,它通过使用chart.dataFields.color = 'color' ,但我不知道它是如何用于更深层次的。 I found no documentation for how to do this.我没有找到有关如何执行此操作的文档。 My data looks like this:我的数据如下所示:

let data = [
  {
    name: "Some title",
    value: 3,
    color: "#32a852",
    children: [
      {
        name: "Some child title",
        value: 3,
        color: "#dc3545"
      },
      ...
    ]
  },
  ...
]

And my chart code:还有我的图表代码:

let chart = am4core.create('treemap', am4charts.TreeMap)
chart.data = data
chart.hiddenState.properties.opacity = 0 // this makes initial fade in effect
chart.layoutAlgorithm = chart.binaryTree

// only one level visible initially
chart.maxLevels = 1
// define data fields
chart.dataFields.name = 'name'
chart.dataFields.value = 'value'
chart.dataFields.children = 'children'
chart.dataFields.color = 'color'
chart.colors.step = 2

chart.homeText = 'A treemap'

// enable navigation
chart.navigationBar = new am4charts.NavigationBar()

// level 0 series template
let level0SeriesTemplate = chart.seriesTemplates.create('0')
let level0SeriesColumn = level0SeriesTemplate.columns.template

level0SeriesColumn.tooltipText = '{name}: {value}'
level0SeriesColumn.column.cornerRadius(10, 10, 10, 10)
level0SeriesColumn.strokeWidth = 5
level0SeriesColumn.autoWrap = true

let bullet0 = level0SeriesTemplate.bullets.push(new am4charts.LabelBullet())
bullet0.locationX = 0.5
bullet0.locationY = 0.5
bullet0.label.text = '{name}'
bullet0.label.fill = am4core.color('#ffffff')

// level 1 series template
let level1SeriesTemplate = chart.seriesTemplates.create('1')
let level1SeriesColumn = level1SeriesTemplate.columns.template

level1SeriesColumn.fillOpacity = 0
level1SeriesColumn.tooltipText = '{name}: {value}'
level1SeriesColumn.column.cornerRadius(10, 10, 10, 10)
level1SeriesColumn.strokeWidth = 5

let bullet1 = level1SeriesTemplate.bullets.push(new am4charts.LabelBullet())
bullet1.locationX = 0.5
bullet1.locationY = 0.5
bullet1.label.text = '{name}'
bullet1.label.fill = am4core.color('#ffffff')

The color data field does not work because you have fill for columns set on second level series to zero:颜色数据字段不起作用,因为您将第二级系列上的列填充为零:

level1SeriesColumn.fillOpacity = 0; // remove this line

The line above results in completely transparent second level columns, hence the top level color showing through.上面的行导致完全透明的二级列,因此顶级颜色显示出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM