amCharts4-如何在强制定向树中设置节点之间的单个链接/边的样式?

时间:2019-05-13 12:39:19

标签: javascript amcharts

我正在使用am4Charts并具有带有几个节点的强制定向树。 我想更改图中单个链接的笔触宽度。

简单的例子:

<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<script src="//www.amcharts.com/lib/4/plugins/forceDirected.js"></script>
<div id="chartdiv"></div>

Javascript:

am4core.useTheme(am4themes_animated);

// Create chart
chart = am4core.create("chartdiv", am4plugins_forceDirected.ForceDirectedTree);

// Create series
var series = chart.series.push(new am4plugins_forceDirected.ForceDirectedSeries());

// Set data
series.data = [{
  "name": "First",
  "link": ["Second", "Third"],
  "children": [{
    "name": "A1", "value": 100, "link": ["B2"]
  }, {
    "name": "A2", "value": 60
  }, {
    "name": "A3", "value": 30
  }]
}, {
  "name": "Second",
  "children": [{
    "name": "B1", "value": 135
  }, {
    "name": "B2", "value": 98
  }, {
    "name": "B3", "value": 56
  }]
}, {
  "name": "Third",
  "children": [{
    "name": "C1", "value": 1
  }]
}];
series.dataFields.linkWith = "link";
series.dataFields.id = "name";
// Set up data fields
series.dataFields.value = "value";
series.dataFields.name = "name";
series.dataFields.children = "children";

// Add labels
series.nodes.template.label.text = "{name}";
series.fontSize = 10;
series.minRadius = 15;
series.maxRadius = 40;
// series.centerStrength = 50;
// series.manyBodyStrength = 50;
series.links.template.strokeWidth = 5;
series.links.template.strokeOpacity = 1;

series.centerStrength = 0.5;

Codepen example

在文档中,我找到了一种为所有链接设置strokeWidthstrokeOpacity的方法。

我已经尝试制作第二个series并将其“ Third”节点移至该节点,并仅为第二个系列设置strokeWidth,但是这些节点未连接。即使它奏效了,这似乎也是一个令人费解的方法。

如何设置两个节点之间单个链接的strokeWidth,例如,从节点“ First”到节点“ Third”,并保持所有其他链接不变,最好不要将节点分成多个{{1} }?

1 个答案:

答案 0 :(得分:0)

series.links.template.configField = 'configLink'; //maps configLink property of data nodes to the links

然后

{
    "name": "Second",
    "children": [{
    "name": "B1", "value": 135
    }, {
    "name": "B2", "value": 98
    }, {
    "name": "B3", "value": 56
    }],
    configLink: {
        fill: color,
        stroke: color,
        strokeWidth: 2
    }
}

您还可以添加适配器:

series.links.template.adapter.add('stroke', (stroke, target) => {
        if (target.dataItem.id === 'somenodesID') {
            return am4core.color();
        }

        return am4core.color('#ddd');
    });

为节点提供id属性。