Skip to content

Commit 2687ec2

Browse files
authored
Feature/docs (#6)
* utils docs with module definition * Documentation for graph helper * Add docs for graph config :muscle. Fix highlightStrokeColor default value ⭐ * Finish Graph docs. Update README * Remove unexistent prop from Link. Add Link docs * Add node docs * Remove docs from gitignore. Add .npmignore file * Update README. Add gif sample to sandbox
1 parent 51ee3da commit 2687ec2

38 files changed

+5222
-172
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ typings/
6565

6666
# PROJECT configurations
6767
dist/
68-
DOCUMENTATION.md
69-
docs
7068
coverage
7169
sandbox/rd3g.sandbox.bundle.js.map
70+
gen-docs

.npmignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.babelrc
2+
.eslintrc.js
3+
.git
4+
.gitignore
5+
.travis.yml
6+
coverage
7+
docs
8+
gen-docs
9+
package.json
10+
sandbox
11+
test
12+
webpack.config.dist.js
13+
webpack.config.js
14+
yarn.lock

README.md

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,95 @@
11
# react-d3-graph · [![Build Status](https://travis-ci.com/danielcaldas/react-d3-graph.svg?token=fb6uSENok5Y3gSSi5yjE&branch=master)](https://travis-ci.com/danielcaldas/react-d3-graph)
22
Interactive and configurable graphs with react and d3 effortlessly.
33

4+
![react-d3-graph gif sample][https://github.com/danielcaldas/react-d3-graph/tree/master/sandbox/rd3g.gif]
5+
46
## Playground
57
Here a live playground (https://danielcaldas.github.io/react-d3-graph/sandbox/index.html) page where you can interactively config your own graph,
6-
and generate the configuration data structure ready to use! :sunglasses:
8+
and generate a ready to use configuration! :sunglasses:
9+
10+
## Documentation
11+
Full documentation [here](https://github.com/danielcaldas/docs/index.html).
712

813
## Compatibility
9-
- Node version >= 4.7.0
10-
- npm3
14+
- Node version **>=6.9.5**
15+
- React version **15.5.0**
16+
- d3 version **4.7.4**
17+
18+
## Usage sample
19+
Graph component is the main component for react-d3-graph components, its interface allows its user
20+
to build the graph once the user provides the data, configuration (optional) and callback interactions (also optional).
21+
The code for the live example (<https://danielcaldas.github.io/react-d3-graph/sandbox/index.html>)
22+
can be consulted here <https://github.com/danielcaldas/react-d3-graph/blob/master/sandbox/Sandbox.js>
1123

12-
## Notes
13-
- Obtain a cool node when set the fill property as white a other color for the border. It will seem like the node is more like a ring.
14-
- Default configuration:
1524
```javascript
16-
export default {
17-
automaticRearrangeAfterDropNode: false, // Very expensive for dense graph
18-
height: 450,
19-
highlightBehavior: false,
20-
highlightOpacity: 1, // For all elements: nodes, text and links
21-
maxZoom: 8,
22-
minZoom: 0.5,
23-
staticGraph: false,
24-
width: 800,
25+
// Graph payload (with minimalist structure)
26+
const data = {
27+
nodes: [
28+
{id: 'Harry'},
29+
{id: 'Sally'},
30+
{id: 'Alice'}
31+
],
32+
links: [
33+
{source: 'Harry', target: 'Sally'},
34+
{source: 'Harry', target: 'Alice'},
35+
]
36+
};
37+
38+
// The graph configuration
39+
const myConfig = {
40+
highlightBehavior: true,
2541
node: {
26-
color: '#d3d3d3',
27-
fontSize: 10, // in px
28-
fontWeight: 'normal',
29-
highlightColor: 'SAME',
30-
highlightFontSize: 10,
31-
highlightFontWeight: 'normal',
32-
highlightStrokeColor: 'none',
33-
highlightStrokeWidth: 1.5,
34-
labelProperty: 'id',
35-
mouseCursor: 'pointer',
36-
opacity: 1,
37-
renderLabel: true,
38-
size: 200,
39-
strokeColor: 'none',
40-
strokeWidth: 1.5,
41-
symbolType: 'circle'
42+
color: 'lightgreen',
43+
size: 120,
44+
highlightStrokeColor: 'blue'
4245
},
4346
link: {
44-
color: '#d3d3d3',
45-
highlightColor: '#d3d3d3',
46-
opacity: 1,
47-
strokeWidth: 1.5
47+
highlightColor: 'lightblue'
4848
}
4949
};
50+
51+
// Graph event callbacks
52+
const onClickNode = function(nodeId) {
53+
window.alert('Clicked node', nodeId);
54+
};
55+
56+
const onMouseOverNode = function(nodeId) {
57+
window.alert('Mouse over node', nodeId);
58+
};
59+
60+
const onMouseOutNode = function(nodeId) {
61+
window.alert('Mouse out node', nodeId);
62+
};
63+
64+
const onClickLink = function(source, target) {
65+
window.alert(`Clicked link between ${source} and ${target}`);
66+
};
67+
68+
<Graph
69+
id='graph-id' // id is mandatory, if no id is defined rd3g will throw an error
70+
data={data}
71+
config={myConfig}
72+
onClickNode={onClickNode}
73+
onClickLink={onClickLink}
74+
onMouseOverNode={onMouseOverNode}
75+
onMouseOutNode={onMouseOutNode} />
5076
```
5177

5278
## TODOs
53-
This consists in a list of ideas for further development:
79+
This consists in a list of ideas for further developments:
5480
- Expose a graph property **background-color** that is applied to the svg graph container;
5581
- Expose d3-force values as configurable such as **alphaTarget** simulation value;
5682
- Improve opacity/highlightBehavior strategy maybe use a global *background: rgba(...)* value and then set a higher
5783
value on selected nodes;
5884
- At the moment highlightBehavior is highlighting the mouse hovered node, its 1st degree connections and their 1st
59-
degree connections. Make **highlightBehaviorDegree** which consists in a *step value* on the depth that we wish to highlight.
60-
61-
#### Sanbox/Playground
62-
- Improve page layout (optimize space).
85+
degree connections. Make **highlightBehaviorDegree** which consists in a *step value* on the depth that we wish to highlight;
86+
- Semantic node size. Have a property value in each node that then is used along side config.nodeSize property
87+
to calculate effective node size in run time;
88+
- Improve semanticStrokeWidth calculation;
89+
- On Graph instantiation do a check on all config properties. If there is a "bad property" (name or value) throw
90+
a custom error (property error checking);
91+
- Path highlight - highlight a certain set of links and nodes (use case: highlight shortest path between two given nodes);
92+
- Link mouseover with highlight behavior highlights the intervenient nodes.
6393

6494
## Contributions
6595
Contributions are welcome fell free to submit new features or simply grab something from

docs/assets/anchor.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*!
2+
* AnchorJS - v1.2.1 - 2015-07-02
3+
* https://github.com/bryanbraun/anchorjs
4+
* Copyright (c) 2015 Bryan Braun; Licensed MIT
5+
*/
6+
7+
function AnchorJS(options) {
8+
'use strict';
9+
10+
this.options = options || {};
11+
12+
this._applyRemainingDefaultOptions = function(opts) {
13+
this.options.icon = this.options.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
14+
this.options.visible = this.options.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always'
15+
this.options.placement = this.options.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left'
16+
this.options.class = this.options.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
17+
};
18+
19+
this._applyRemainingDefaultOptions(options);
20+
21+
this.add = function(selector) {
22+
var elements,
23+
elsWithIds,
24+
idList,
25+
elementID,
26+
i,
27+
roughText,
28+
tidyText,
29+
index,
30+
count,
31+
newTidyText,
32+
readableID,
33+
anchor;
34+
35+
this._applyRemainingDefaultOptions(this.options);
36+
37+
// Provide a sensible default selector, if none is given.
38+
if (!selector) {
39+
selector = 'h1, h2, h3, h4, h5, h6';
40+
} else if (typeof selector !== 'string') {
41+
throw new Error('The selector provided to AnchorJS was invalid.');
42+
}
43+
44+
elements = document.querySelectorAll(selector);
45+
if (elements.length === 0) {
46+
return false;
47+
}
48+
49+
this._addBaselineStyles();
50+
51+
// We produce a list of existing IDs so we don't generate a duplicate.
52+
elsWithIds = document.querySelectorAll('[id]');
53+
idList = [].map.call(elsWithIds, function assign(el) {
54+
return el.id;
55+
});
56+
57+
for (i = 0; i < elements.length; i++) {
58+
59+
if (elements[i].hasAttribute('id')) {
60+
elementID = elements[i].getAttribute('id');
61+
} else {
62+
roughText = elements[i].textContent;
63+
64+
// Refine it so it makes a good ID. Strip out non-safe characters, replace
65+
// spaces with hyphens, truncate to 32 characters, and make toLowerCase.
66+
//
67+
// Example string: // '⚡⚡⚡ Unicode icons are cool--but they definitely don't belong in a URL fragment.'
68+
tidyText = roughText.replace(/[^\w\s-]/gi, '') // ' Unicode icons are cool--but they definitely dont belong in a URL fragment'
69+
.replace(/\s+/g, '-') // '-Unicode-icons-are-cool--but-they-definitely-dont-belong-in-a-URL-fragment'
70+
.replace(/-{2,}/g, '-') // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL-fragment'
71+
.substring(0, 64) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
72+
.replace(/^-+|-+$/gm, '') // 'Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
73+
.toLowerCase(); // 'unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'
74+
75+
// Compare our generated ID to existing IDs (and increment it if needed)
76+
// before we add it to the page.
77+
newTidyText = tidyText;
78+
count = 0;
79+
do {
80+
if (index !== undefined) {
81+
newTidyText = tidyText + '-' + count;
82+
}
83+
// .indexOf is supported in IE9+.
84+
index = idList.indexOf(newTidyText);
85+
count += 1;
86+
} while (index !== -1);
87+
index = undefined;
88+
idList.push(newTidyText);
89+
90+
// Assign it to our element.
91+
// Currently the setAttribute element is only supported in IE9 and above.
92+
elements[i].setAttribute('id', newTidyText);
93+
94+
elementID = newTidyText;
95+
}
96+
97+
readableID = elementID.replace(/-/g, ' ');
98+
99+
// The following code builds the following DOM structure in a more effiecient (albeit opaque) way.
100+
// '<a class="anchorjs-link ' + this.options.class + '" href="#' + elementID + '" aria-label="Anchor link for: ' + readableID + '" data-anchorjs-icon="' + this.options.icon + '"></a>';
101+
anchor = document.createElement('a');
102+
anchor.className = 'anchorjs-link ' + this.options.class;
103+
anchor.href = '#' + elementID;
104+
anchor.setAttribute('aria-label', 'Anchor link for: ' + readableID);
105+
anchor.setAttribute('data-anchorjs-icon', this.options.icon);
106+
107+
if (this.options.visible === 'always') {
108+
anchor.style.opacity = '1';
109+
}
110+
111+
if (this.options.icon === '\ue9cb') {
112+
anchor.style.fontFamily = 'anchorjs-icons';
113+
anchor.style.fontStyle = 'normal';
114+
anchor.style.fontVariant = 'normal';
115+
anchor.style.fontWeight = 'normal';
116+
anchor.style.lineHeight = 1;
117+
}
118+
119+
if (this.options.placement === 'left') {
120+
anchor.style.position = 'absolute';
121+
anchor.style.marginLeft = '-1em';
122+
anchor.style.paddingRight = '0.5em';
123+
elements[i].insertBefore(anchor, elements[i].firstChild);
124+
} else { // if the option provided is `right` (or anything else).
125+
anchor.style.paddingLeft = '0.375em';
126+
elements[i].appendChild(anchor);
127+
}
128+
}
129+
130+
return this;
131+
};
132+
133+
this.remove = function(selector) {
134+
var domAnchor,
135+
elements = document.querySelectorAll(selector);
136+
for (var i = 0; i < elements.length; i++) {
137+
domAnchor = elements[i].querySelector('.anchorjs-link');
138+
if (domAnchor) {
139+
elements[i].removeChild(domAnchor);
140+
}
141+
}
142+
return this;
143+
};
144+
145+
this._addBaselineStyles = function() {
146+
// We don't want to add global baseline styles if they've been added before.
147+
if (document.head.querySelector('style.anchorjs') !== null) {
148+
return;
149+
}
150+
151+
var style = document.createElement('style'),
152+
linkRule =
153+
' .anchorjs-link {' +
154+
' opacity: 0;' +
155+
' text-decoration: none;' +
156+
' -webkit-font-smoothing: antialiased;' +
157+
' -moz-osx-font-smoothing: grayscale;' +
158+
' }',
159+
hoverRule =
160+
' *:hover > .anchorjs-link,' +
161+
' .anchorjs-link:focus {' +
162+
' opacity: 1;' +
163+
' }',
164+
anchorjsLinkFontFace =
165+
' @font-face {' +
166+
' font-family: "anchorjs-icons";' +
167+
' font-style: normal;' +
168+
' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
169+
' src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAAMIAAAAFGxvY2EAKACuAAADHAAAAAxtYXhwAAgAVwAAAygAAAAgbmFtZQ5yJ3cAAANIAAAB2nBvc3QAAwAAAAAFJAAAACAA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JAAoANAD4YW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype");' +
170+
' }',
171+
pseudoElContent =
172+
' [data-anchorjs-icon]::after {' +
173+
' content: attr(data-anchorjs-icon);' +
174+
' }',
175+
firstStyleEl;
176+
177+
style.className = 'anchorjs';
178+
style.appendChild(document.createTextNode('')); // Necessary for Webkit.
179+
180+
// We place it in the head with the other style tags, if possible, so as to
181+
// not look out of place. We insert before the others so these styles can be
182+
// overridden if necessary.
183+
firstStyleEl = document.head.querySelector('[rel="stylesheet"], style');
184+
if (firstStyleEl === undefined) {
185+
document.head.appendChild(style);
186+
} else {
187+
document.head.insertBefore(style, firstStyleEl);
188+
}
189+
190+
style.sheet.insertRule(linkRule, style.sheet.cssRules.length);
191+
style.sheet.insertRule(hoverRule, style.sheet.cssRules.length);
192+
style.sheet.insertRule(pseudoElContent, style.sheet.cssRules.length);
193+
style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length);
194+
};
195+
}
196+
197+
var anchors = new AnchorJS();

docs/assets/bass-addons.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.input {
2+
font-family: inherit;
3+
display: block;
4+
width: 100%;
5+
height: 2rem;
6+
padding: .5rem;
7+
margin-bottom: 1rem;
8+
border: 1px solid #ccc;
9+
font-size: .875rem;
10+
border-radius: 3px;
11+
box-sizing: border-box;
12+
}

0 commit comments

Comments
 (0)