You are here

function d3_draw in d3.js 7

D3's main API callback.

Parameters

array $vis: The chart data

Return value

string Rendered chart html

6 calls to d3_draw()
d3_examples_bar in modules/d3_examples/d3_examples.module
Generates a random example bar graph.
d3_examples_column in modules/d3_examples/d3_examples.module
Demonstrates a column chart using the D3 API directly.
d3_examples_linegraph in modules/d3_examples/d3_examples.module
Demonstrates how to create a line graph using the D3 API directly.
d3_examples_pie in modules/d3_examples/d3_examples.module
Generates a random pie chart example.
d3_views_plugin_style_d3::draw in modules/d3_views/views/plugins/d3_views_plugin_style_d3.inc
Send the vis to d3's theme handlers.

... See full list

File

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

Code

function d3_draw($vis) {

  // Type is required for correct processing.
  $type = !empty($vis['type']) ? strtolower($vis['type']) : FALSE;
  if (!$type) {
    drupal_set_message(t('No chart type specified'), 'error');
    return;
  }

  // Ensure we have d3 installed, don't just show a blank screen.
  $d3 = libraries_load('d3');
  if (empty($d3['installed'])) {
    drupal_set_message(t('The d3 library is not correctly installed'), 'error');
    return;
  }
  $output = array(
    '#theme' => 'd3',
  );

  // If user selected cdn, we need to load this manually.
  // @see http://drupal.org/node/864376
  if (!empty($d3['cdn'])) {
    $output['#attached']['js'][] = array(
      'data' => $d3['library path'] . '/' . $d3['files']['js']['data'],
      'type' => 'external',
      'group' => JS_LIBRARY,
    );
  }

  // Form library name - convention is going to be d3.[library].
  $library_name = 'd3.' . $type;

  // Check for library instance / definition.
  if ($lib = libraries_load($library_name)) {

    // Enforce lowercase change.
    $output['#library'] = $lib + array(
      'library name' => str_replace('.', '_', $library_name),
    );
    $output['#type'] = $type;

    // Store as vis_id because key ['id'] will get overridden.
    $output['#vis_id'] = $vis['id'];

    // Now add to the behaviors to execute on page load.
    $js = array(
      'd3' => array(
        'inventory' => array(
          $vis['id'] => $vis,
        ),
      ),
    );
    $output['#attached']['js'][] = array(
      'data' => $js,
      'type' => 'setting',
    );

    // Renders the html - .tpl.php if specified or default d3.tpl.php.
    return render($output);
  }
  else {
    drupal_set_message(t('Invalid chart type %type and/or library %library_name is not installed', array(
      '%type' => $type,
      '%library_name' => $library_name,
    )));
    return '';
  }
}