【问题标题】:Why does the value of the "parent" get the sum of the values of the "children" and not the value of itself in the TreeMap?为什么“父”的值得到“子”的值的总和,而不是TreeMap中自身的值?
【发布时间】:2020-09-18 13:06:40
【问题描述】:

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

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

代码:

图表:

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

InputJson:

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
  ]
}]

TreeMap 层级:

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 中)

【问题讨论】:

    标签: javascript reactjs amcharts treemap amcharts4


    【解决方案1】:

    我遇到了类似的问题,我在 GitHub 上发现你的问题是 issue,但我不喜欢我找到的关于这个问题的唯一答案是“这是不可能的 em>”。

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

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

    举个例子:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 2021-07-03
      • 1970-01-01
      • 2017-10-25
      • 2017-04-06
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多