You are here

function _d3_graphapi_format_graph_data in d3.js 7

Converts $graph data array two arrays for links and nodes.

Parameters

array $graph: Associative array of nodes with link information and data.

Return value

array Links and nodes.

1 call to _d3_graphapi_format_graph_data()
theme_d3_graphapi in ./d3.module
Displays the visualization for graphapi's selected library.

File

./d3.module, line 528
D3 module file for creating visualizations with d3.js.

Code

function _d3_graphapi_format_graph_data($graph) {
  $data = array();
  $indices = array();
  $index = 0;
  foreach ($graph as $id => $node) {
    $indices[$id] = $index;
    $index++;
  }

  // Add in edges.
  foreach ($graph as $id => $node) {
    $index = $indices[$id];
    $data['nodes'][$index] = array(
      'name' => $node['data']['title'],
      'group' => isset($node['data']['group']) ? $node['data']['group'] : 1,
      'data' => $node['data'],
    );
    if (count($node['edges']) > 0) {
      foreach ($node['edges'] as $edge => $edge_data) {
        $value = isset($edge_data['data']['value']) ? (int) $edge_data['data']['value'] : NULL;
        $data['links'][] = array(
          'data' => isset($edge_data['data']) ? $edge_data['data'] : array(),
          'source' => $index,
          'target' => $indices[$edge],
          // TODO: This is hard coded, it could be dynamic.
          'value' => $value,
        );
      }
    }
  }
  return $data;
}