Skip to content
Advertisement

colorize D3 chord paths based on data

I migrated this https://bl.ocks.org/nbremer/d2720fdaab1123df73f4806360a09c9e D3 Chord layout to D3v7. My current problem is, I want to utilize the color attribute from var objects to fill the individual paths. Those are currently grey.

I managed to get it done for the outer circle with

JavaScript

I thought I could use the same line of code to display the respected color for the paths but I receive the error that objects[d.index].color is undefined, which confuse me. I assume I did not understood the path procedure fully, which makes me believe I do everything correct.. pathetic.

I appreciate any help.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

TL;DR:

Lines 92 through 94:

JavaScript

… become:

JavaScript

And lines 107 through 109:

JavaScript

… become:

JavaScript

Explanation

You have two problems:

  1. Your items d in the second method actually contain both “source” and “target”, for instance:
JavaScript

… and you therefore need to dig into either “source” or “target” before having access to an “index” attribute.

  1. your objects Array contains references to items that are apparently ordered such that their “id” attribute corresponds to their index in the objects Array, but I presume it to be either an unreliable coincidence or an inadvertent oversight.

In any case, you seem to be disregarding the index property entirely, while I believe you meant to use it to identify each item. I suggest you use Array#find here!

Updated Code Snippet

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement