首页 > 解决方案 > 为什么“父”的值得到“子”的值的总和,而不是TreeMap中自身的值?

问题描述

我不明白为什么当“父母”具有价值时,他仍然没有收到自己的价值,仍然收到“他的孩子”价值的总和作为价值。

尽管文档中记录了当“父母”具有价值时,他不会继承其孩子的价值: 在此处输入图像描述 Amcharts Doc:https ://www.amcharts.com/docs/v4/chart-types/force-directed /

代码:

图表:

let chart = am4core.create("chartdiv",am4charts.TreeMap)

输入JSON:

const jsonData =[{
  "name": "First",
  "value": 2390, //Parent value
  "children": [
    { "name": "A1", "value": 0.4 }, //children value
    { "name": "A2", "value": 0.4 }, //children value
    { "name": "A3", "value": 0.1917355900000002 } //children value
  ]
}, {
  "name": "Second",
  "value": 5933, //Parent value
  "children": [
    { "name": "B1", "value": 0.2332 }, //children value
    { "name": "B2", "value": 0.323 }, //children value
    { "name": "B3", "value": 0.122332 } //children value
  ]
}]

树图级别:

const level1 = chart.seriesTemplates.create("0");
let level1_column = level1.columns.template;
const level1_bullet = level1.bullets.push(new am4charts.LabelBullet());

level1_bullet.label.text =  "{value}";  //The value is the sum of the values of all his children.


const level2 = chart.seriesTemplates.create("1");
let level2_column = level2.columns.template;
const level2_bullet = level2.bullets.push(new am4charts.LabelBullet());

level2_bullet.label.text =  "{value}";  //The value is of the child.

Amcharts Doc TreeMap:https ://www.amcharts.com/docs/v4/chart-types/treemap/

安慰:

我需要父值列是红色值对象而不是黄色对象。(如 InputJson) 在此处输入图像描述

标签: javascriptreactjsamchartstreemapamcharts4

解决方案


我有类似的问题,我在 GitHub 上发现你的问题是一个问题,但我不喜欢我发现的关于这个问题的唯一答案是“这是不可能的”。

您不能将父值设置为自定义数字,因为它始终是根据子值计算得出的。这是真的。

但是,您可以计算自定义项目值以显示数据,并使用自定义工具提示来显示实际值。

只是给你一个例子:

在我的例子中,我在点击事件时动态添加了孩子,所以最初父母有自己的价值,但是在插入与其父母价值无关的孩子之后,整个图表就搞砸了。

我没有使用真实数据作为项目价值,而是根据父母的项目价值和孩子的真实价值来计算它:

  • 对于根元素,我使用了一个固定数字(例如 10000)。
  • 对于每个有父项的项目,我通过将父项的项值除以子项的实际值的总和,然后将其乘以项的实际值来计算它的项值。它给出的值与父级和其他子级成正比。

但是这种方法也很有用,当您有 0 或非常小的值的项目,但您想以固定值显示它们时。我还在github 上找到了一个关于此的问题,答案是相同的“不可能的”。

我希望这个答案对某人有所帮助。


推荐阅读