premium

Using D3 in Ext JS

Introduction

Presenting data in a clear and compelling way is an important job for any application. D3 is an extremely popular choice for data visualization. The new d3 package makes it simpler than ever to integrate D3 into your Ext JS application.

In this guide, we will explore the various D3 components that are available in Ext JS.

Using the D3 Package

Your D3 package should be located within the packages folder that came with your Ext JS Premium purchase. This packages folder should be copied to the root packages directory of the ext folder you use to generate applications.

For instance, if you have generated your application using Sencha Cmd, you would move the d3 package from:

  1. {premiumLocation}/ext/packages/d3

to:

  1. {yourApplication}/ext/packages/d3

or:

  1. {yourWorkspace}/ext/packages/d3

Once your package is in appropriately located, you’ll need to modify your app.json file. Open {appDir}/app.json and add “d3” to the “requires” block. It should now look like similar to the following code snippet:

  1. "requires": [
  2. "d3"
  3. ],

Do keep in mind that your requires block may contain other items depending on your default theme.

Your application should now be ready to use D3!

Note: The D3 package is only available in Ext Premium. For more information about Ext Premium and our other products, please check out the products page.

D3 Without a Cmd Application

You can also utilize the d3 package without a Cmd built application. However, you may need to use Cmd to build the initial CSS/JS builds if they do not appear in your package’s build folder.

We generally try to include pre-built packages, but sometimes they slip through the cracks and are not available until the next release.

Building a package is quick and simple. The process produces a singular JS/CSS file for inclusion in your non-Cmd application. To build these files, issue the following command from your CLI:

  1. //target a specific theme
  2. sencha package build {themeName}

Classic themes will just be theme names (triton, neptune, etc).

Modern themes will be prepended with “modern-“ (modern-neptune).

or

  1. //build for all themes
  2. sencha package build

You should now have a build folder that contains the following built files:

  1. d3/{toolkit}/d3.js
  2. d3/{toolkit}/{theme}/resources/d3-all.css

You can now include these outputs by whatever method you are using.

Components

Integrating D3 begins with the choice of rendering technology: SVG or Canvas. This is because D3 is not a drawing abstraction library (like Ext.draw.*) and so the nature of the drawing surface is an important starting point. These base classes provide a structure to assist with rendering to the correct container element and updating when the container resizes.

These base classes streamline the process of copying simple, whole page examples (such as those in the D3 Gallery) and housing them in a component. That component can then be easily managed like any other in an Ext JS application.

The D3 Adapter then builds on these primitive base classes and provides some of the most commonly used D3 visualizations as ready to use components. These higher-level components understand your Ext JS data stores and connect them to D3 drawings and then ensure these stay up to date when your data changes.

Hierarchical Components

The need to display hierarchical information is quite common and D3 provides a wide range of visualizations for this type of data. These components connect an Ext.data.TreeStore to several of the popular D3 layouts to display hierarchical data.

Tree

The ‘d3-tree’ component is a perfect way to visualize hierarchical data as an actual tree in cases where the relative size of nodes is of little interest, and the focus is on the relative position of each node in the hierarchy. A horizontal tree makes for a more consistent look and more efficient use of space when text labels are shown next to each node.

Expand Code

JS Run

  1. Ext.create('Ext.panel.Panel', {
  2. renderTo: Ext.getBody(),
  3. title: 'Tree Chart',
  4. items: [
  5. {
  6. xtype: 'd3-tree',
  7. store: {
  8. type: 'tree',
  9. data: [
  10. {
  11. text: "IT",
  12. expanded: false,
  13. children: [
  14. {leaf: true, text: 'Norrin Radd'},
  15. {leaf: true, text: 'Adam Warlock'}
  16. ]
  17. },
  18. {
  19. text: "Engineering",
  20. expanded: false,
  21. children: [
  22. {leaf: true, text: 'Mathew Murdoch'},
  23. {leaf: true, text: 'Lucas Cage'}
  24. ]
  25. },
  26. {
  27. text: "Support",
  28. expanded: false,
  29. children: [
  30. {leaf: true, text: 'Peter Quill'}
  31. ]
  32. }
  33. ]
  34. },
  35. interactions: {
  36. type: 'panzoom',
  37. zoom: {
  38. extent: [0.3, 3],
  39. doubleTap: false
  40. }
  41. },
  42. nodeSize: [90, 50]
  43. }
  44. ]
  45. });

Sunburst

The ‘d3-sunburst’ component visualizes tree nodes as donut sectors, with the root circle in the center. The angle and area of each sector corresponds to its node value. By default the same value is returned for each node, meaning that siblings will span equal angles and occupy equal area.

Expand Code

JS Run

  1. Ext.create('Ext.panel.Panel', {
  2. renderTo: Ext.getBody(),
  3. title: 'Sunburst Chart',
  4. height: 750,
  5. width: 750,
  6. layout: 'fit',
  7. items: [
  8. {
  9. xtype: 'd3-sunburst',
  10. padding: 20,
  11. tooltip: {
  12. renderer: function (component, tooltip, record) {
  13. tooltip.setHtml(record.get('text'));
  14. }
  15. },
  16. store: {
  17. type: 'tree',
  18. data: [
  19. {
  20. text: "Oscorp",
  21. children: [
  22. {text: 'Norman Osborn'},
  23. {text: 'Harry Osborn'},
  24. {text: 'Arthur Stacy'}
  25. ]
  26. },
  27. {
  28. text: "SHIELD",
  29. children: [
  30. {text: 'Nick Fury'},
  31. {text: 'Maria Hill'},
  32. {text: 'Tony Stark'}
  33. ]
  34. },
  35. {
  36. text: "Illuminati",
  37. children: [
  38. {text: 'Namor'},
  39. {text: 'Tony Stark'},
  40. {text: 'Reed Richards'},
  41. {text: 'Black Bolt'},
  42. {text: 'Dr. Stephen Strange'},
  43. {text: 'Charles Xavier'}
  44. ]
  45. }
  46. ]
  47. }
  48. }
  49. ]
  50. });

Pack

The ‘d3-pack’ component uses D3’s Pack Layout to visualize hierarchical data as an enclosure diagram. The size of each leaf node’s circle reveals a quantitative dimension of each data point. The enclosing circles show the approximate cumulative size of each subtree.

Expand Code

JS Run

  1. Ext.create('Ext.panel.Panel', {
  2. renderTo: Ext.getBody(),
  3. title: 'Pack Chart',
  4. height: 750,
  5. width: 750,
  6. layout: 'fit',
  7. items: [
  8. {
  9. xtype: 'd3-pack',
  10. tooltip: {
  11. renderer: function (component, tooltip, record) {
  12. tooltip.setHtml(record.get('text'));
  13. }
  14. },
  15. store: {
  16. type: 'tree',
  17. data: [
  18. {
  19. "text": "DC",
  20. "children": [
  21. {
  22. "text": "Flash",
  23. "children": [
  24. { "text": "Flashpoint" }
  25. ]
  26. },
  27. {
  28. "text": "Green Lantern",
  29. "children": [
  30. { "text": "Rebirth" },
  31. { "text": "Sinestro Corps War" }
  32. ]
  33. },
  34. {
  35. "text": "Batman",
  36. "children": [
  37. { "text": "Hush" },
  38. { "text": "The Long Halloween" },
  39. { "text": "Batman and Robin" },
  40. { "text": "The Killing Joke" }
  41. ]
  42. }
  43. ]
  44. },
  45. {
  46. "text": "Marvel",
  47. "children": [
  48. {
  49. "text": "All",
  50. "children": [
  51. { "text": "Infinity War" },
  52. { "text": "Infinity Gauntlet" },
  53. { "text": "Avengers Disassembled" }
  54. ]
  55. },
  56. {
  57. "text": "Spiderman",
  58. "children": [
  59. { "text": "Ultimate Spiderman" }
  60. ]
  61. },
  62. {
  63. "text": "Vision",
  64. "children": [
  65. { "text": "The Vision" }
  66. ]
  67. },
  68. {
  69. "text": "X-Men",
  70. "children": [
  71. { "text": "Gifted" },
  72. { "text": "Dark Phoenix Saga" },
  73. { "text": "Unstoppable" }
  74. ]
  75. }
  76. ]
  77. }
  78. ]
  79. }
  80. }
  81. ]
  82. });

TreeMap

The ‘d3-treemap’ component uses D3’s TreeMap Layout to recursively subdivide area into rectangles, where the area of any node in the tree corresponds to its value.

Expand Code

JS Run

  1. Ext.create('Ext.panel.Panel', {
  2. renderTo: Ext.getBody(),
  3. title: 'TreeMap Chart',
  4. height: 750,
  5. width: 750,
  6. layout: 'fit',
  7. items: [
  8. {
  9. xtype: 'd3-treemap',
  10. tooltip: {
  11. renderer: function (component, tooltip, record) {
  12. tooltip.setHtml(record.get('text'));
  13. }
  14. },
  15. nodeValue: function (node) {
  16. // Associates the rendered size of the box to a value in your data
  17. return node.data.value;
  18. },
  19. store: {
  20. type: 'tree',
  21. data: [
  22. { text: 'Hulk',
  23. value : 5,
  24. children: [
  25. { text: 'The Leader', value: 3 },
  26. { text: 'Abomination', value: 2 },
  27. { text: 'Sandman', value: 1 }
  28. ]
  29. },
  30. { text: 'Vision',
  31. value : 4,
  32. children: [
  33. { text: 'Kang', value: 4 },
  34. { text: 'Magneto', value: 3 },
  35. { text: 'Norman Osborn', value: 2 },
  36. { text: 'Anti-Vision', value: 1 }
  37. ]
  38. },
  39. { text: 'Ghost Rider',
  40. value : 3,
  41. children: [
  42. { text: 'Mephisto', value: 1 }
  43. ]
  44. },
  45. { text: 'Loki',
  46. value : 2,
  47. children: [
  48. { text: 'Captain America', value: 3 },
  49. { text: 'Deadpool', value: 4 },
  50. { text: 'Odin', value: 5 },
  51. { text: 'Scarlet Witch', value: 2 },
  52. { text: 'Silver Surfer', value: 1 }
  53. ]
  54. },
  55. { text: 'Daredevil',
  56. value : 1,
  57. children: [
  58. { text: 'Purple Man', value: 4 },
  59. { text: 'Kingpin', value: 3 },
  60. { text: 'Namor', value: 2 },
  61. { text: 'Sabretooth', value: 1 }
  62. ]
  63. }
  64. ]
  65. }
  66. }
  67. ]
  68. });

HeatMap Component

Heatmaps are a great way to present three-dimensional data in a two-dimensional drawing. The HeatMap component maps X and Y values to axes as you might expect. It then maps Z values to a “color axis”.

Expand Code

JS Run

  1. Ext.create('Ext.panel.Panel', {
  2. renderTo: Ext.getBody(),
  3. title: 'Heatmap Chart',
  4. height: 750,
  5. width: 750,
  6. layout: 'fit',
  7. items: [
  8. {
  9. xtype: 'd3-heatmap',
  10. padding: {
  11. top: 20,
  12. right: 30,
  13. bottom: 20,
  14. left: 80
  15. },
  16. xAxis: {
  17. axis: {
  18. ticks: 'd3.timeDay',
  19. tickFormat: "d3.timeFormat('%b %d')",
  20. orient: 'bottom'
  21. },
  22. scale: {
  23. type: 'time'
  24. },
  25. title: {
  26. text: 'Date'
  27. },
  28. field: 'date',
  29. step: 24 * 60 * 60 * 1000
  30. },
  31. yAxis: {
  32. axis: {
  33. orient: 'left'
  34. },
  35. scale: {
  36. type: 'linear'
  37. },
  38. title: {
  39. text: 'Total'
  40. },
  41. field: 'bucket',
  42. step: 100
  43. },
  44. colorAxis: {
  45. scale: {
  46. type: 'linear',
  47. range: ['white', 'orange']
  48. },
  49. field: 'count',
  50. minimum: 0
  51. },
  52. tiles: {
  53. attr: {
  54. 'stroke': 'black',
  55. 'stroke-width': 1
  56. }
  57. },
  58. store: {
  59. fields: [
  60. {name: 'date', type: 'date', dateFormat: 'Y-m-d'},
  61. 'bucket',
  62. 'count'
  63. ],
  64. data: [
  65. { "date": "2012-07-20", "bucket": 800, "count": 119 },
  66. { "date": "2012-07-20", "bucket": 900, "count": 123 },
  67. { "date": "2012-07-20", "bucket": 1000, "count": 173 },
  68. { "date": "2012-07-20", "bucket": 1100, "count": 226 },
  69. { "date": "2012-07-20", "bucket": 1200, "count": 284 },
  70. { "date": "2012-07-21", "bucket": 800, "count": 123 },
  71. { "date": "2012-07-21", "bucket": 900, "count": 165 },
  72. { "date": "2012-07-21", "bucket": 1000, "count": 237 },
  73. { "date": "2012-07-21", "bucket": 1100, "count": 278 },
  74. { "date": "2012-07-21", "bucket": 1200, "count": 338 },
  75. { "date": "2012-07-22", "bucket": 900, "count": 154 },
  76. { "date": "2012-07-22", "bucket": 1000, "count": 241 },
  77. { "date": "2012-07-22", "bucket": 1100, "count": 246 },
  78. { "date": "2012-07-22", "bucket": 1200, "count": 300 },
  79. { "date": "2012-07-22", "bucket": 1300, "count": 305 },
  80. { "date": "2012-07-23", "bucket": 800, "count": 120 },
  81. { "date": "2012-07-23", "bucket": 900, "count": 156 },
  82. { "date": "2012-07-23", "bucket": 1000, "count": 209 },
  83. { "date": "2012-07-23", "bucket": 1100, "count": 267 },
  84. { "date": "2012-07-23", "bucket": 1200, "count": 299 },
  85. { "date": "2012-07-23", "bucket": 1300, "count": 316 },
  86. { "date": "2012-07-24", "bucket": 800, "count": 105 },
  87. { "date": "2012-07-24", "bucket": 900, "count": 156 },
  88. { "date": "2012-07-24", "bucket": 1000, "count": 220 },
  89. { "date": "2012-07-24", "bucket": 1100, "count": 255 },
  90. { "date": "2012-07-24", "bucket": 1200, "count": 308 },
  91. { "date": "2012-07-25", "bucket": 800, "count": 104 },
  92. { "date": "2012-07-25", "bucket": 900, "count": 191 },
  93. { "date": "2012-07-25", "bucket": 1000, "count": 201 },
  94. { "date": "2012-07-25", "bucket": 1100, "count": 238 },
  95. { "date": "2012-07-25", "bucket": 1200, "count": 223 },
  96. { "date": "2012-07-26", "bucket": 1300, "count": 132 },
  97. { "date": "2012-07-26", "bucket": 1400, "count": 117 },
  98. { "date": "2012-07-26", "bucket": 1500, "count": 124 },
  99. { "date": "2012-07-26", "bucket": 1600, "count": 154 },
  100. { "date": "2012-07-26", "bucket": 1700, "count": 167 }
  101. ]
  102. }
  103. }
  104. ]
  105. });

Note: “Heatmap.js” can throw up red flags for some ad blockers. If “Heatmap.js” won’t load while you’re developing, disable your ad blocker or create an exception for “Heatmap.js”.

Custom Components

Scenes

Ext.d3.svg.Svg has a concept of a scene, which is represented by an SVG group (‘g’ element) that is designed to hold all rendered data nodes and other elements of the visualization.

For examples of a pure SVG scene example, see our Kitchen Sink.

Canvas

Ext.d3.canvas.Canvas deals with the Canvas element and its (‘2d’) context. Since D3 does not provide resolution independence for crisp looking drawings on HiDPI screens, it’s handled by the ‘d3-canvas’ component. HiDPI support is completely transparent. One just calls native Canvas APIs as they normally would. The HDPI overrides are automatically applied to the canvas context where necessary (see Ext.d3.canvas.HiDPI).

For examples of a pure Canvas example, see our Kitchen Sink.

Theming

Themes can be applied in the same fashion as other Ext JS components. However, there is an exception when theming tree nodes and heatmap cell colors. These will need an Ext.d3.axis.Color.

In our above HeatMap example, You saw that the nodes/cells with the lowest ‘performance’ are white, while cells with the highest ‘performance’ are ‘orange’.

image alt text

This representative gradient can be achieved with the following code:

  1. colorAxis: {
  2. field: 'performance',
  3. scale: {
  4. type: 'linear',
  5. range: ['white', 'orange']
  6. }
  7. }

Events

D3 has it’s own way of dealing with node events. However, it is incompatible with the Ext JS event system. Additionally, the D3 event system does not automatically map touch events to desktop events and vice versa. Direct use of that event system is discouraged.

Instead, use one of the following approaches:

Preferred Method

  1. var sceneElement = Ext.get(me.getScene().node()); // **preferred**
  2. sceneElement.on(‘click’, handlerFn, scope, {
  3. delegate: g.x-class-name
  4. });

Fly Method

  1. Ext.fly(selection.node()).on(‘click’, handlerFn, scope);

Each Method

  1. selection.each(function (node) {
  2. Ext.fly(this).on(‘click’, handlerFn, scope);
  3. });

Note: D3 4.0 now features automatic event mapping. However, it may be incompatible with the Ext JS event system.
If it does become compatible, using two event systems in a single application is still discouraged.

Interactions

Instead of making use of D3’s “behaviors”, D3 components have a concept of interaction.

Currently only ‘panzoom’ interaction (Ext.d3.interaction.PanZoom) is supported. It is meant to replace and enhance the ‘zoom’ behavior provided by D3. For example, the ‘zoom’ behavior in D3 has no support for constrained panning, kinetic scrolling, scroll indicators and is generally incompatible with ExtJS event system. ‘Panzoom’ interaction fixes all that.

Note: Some of these features made it into D3 4.0, which was released summer 2016.

Tooltips

All high level components support the ‘tooltip’ config via the Ext.d3.mixin.ToolTip mixin.

The tooltip config works just like in other ExtJS components except for one extra property — ‘renderer’. Renderer is generally the only thing that needs to be specified when configuring a tooltip. Here are two examples of how you might set up your tooltips.

View Controller Method

  1. tooltip: { // inside a D3 component config in a view
  2. renderer: 'onTooltip'
  3. }
  4. // view controller’s handler
  5. onTooltip: function (component, tooltip, node, element, event) {
  6. var n = node.childNodes.length;
  7. tooltip.setHtml(n + ' items' + (n === 1 ? '' : 's') + ' inside.');
  8. }

View Method

  1. tooltip :
  2. renderer: function(component, tooltip, record) {
  3. tooltip.setHtml(record.get('text'));
  4. }
  5. }

Pivoting with D3

We can also aggregate local data using a Pivot Matrix. The new “pivot-d3” package contains several useful components that connect these pieces for you.

Ext.pivot.d3.HeatMap

This component produces a D3 HeatMap after the data is aggregated by the pivot matrix.

  1. {
  2. xtype: 'pivotheatmap',
  3. // pivot matrix configurations
  4. matrix: {
  5. store: {
  6. type: 'salesperemployee'
  7. },
  8. leftAxis: {
  9. dataIndex: 'employee',
  10. header: 'Employee',
  11. sortable: false
  12. },
  13. topAxis: {
  14. dataIndex: 'day',
  15. sortIndex: 'dayNumber',
  16. header: 'Day'
  17. },
  18. aggregate: {
  19. dataIndex: 'sales',
  20. aggregator: 'sum'
  21. }
  22. }
  23. }

When the pivot matrix is configured there should be only one dimension per axis defined.

Ext.pivot.d3.TreeMap

This component produces a D3 TreeMap after the data is aggregated by the pivot matrix.

  1. {
  2. xtype: 'pivottreemap',
  3. // pivot matrix configurations
  4. matrix: {
  5. store: {
  6. type: 'salesperemployee'
  7. },
  8. leftAxis: {
  9. dataIndex: 'employee',
  10. header: 'Employee',
  11. sortable: false
  12. },
  13. aggregate: {
  14. dataIndex: 'sales',
  15. aggregator: 'sum'
  16. }
  17. }
  18. }

The topAxis configuration of the pivot matrix is ignored, leftAxis supports multiple dimensions and aggregate should have only one dimension.

Ext.pivot.d3.Container

This new component connects the pivot Configurator plugin with any of the the above components to allow the end-user to configure the pivot matrix:

image alt text

The Configurator can be configured using the “configurator” config:

  1. {
  2. xtype: 'pivotd3container',
  3. drawing: {
  4. xtype: 'something' // one of the above pivot D3 components
  5. // more configs specific to that component
  6. },
  7. configurator: {
  8. // pivotconfigurator plugin configs
  9. fields: [{
  10. dataIndex: 'person',
  11. header: 'Person',
  12. settings: {
  13. // field settings
  14. }
  15. },{
  16. // more fields
  17. }]
  18. },
  19. // following configs are similar to the pivot grid
  20. matrix: {
  21. leftAxis: [],
  22. topAxis: [],
  23. aggregate: []
  24. }
  25. }