├── .babelrc
├── .editorconfig
├── .eslintrc
├── .gitignore
├── .jshintrc
├── .lintstagedrc
├── .npmignore
├── LICENSE
├── LICENSE.d3
├── README.md
├── config
├── webpack.config.base.js
├── webpack.config.dev.js
└── webpack.config.prod.js
├── dist
├── react-d3-components.js
├── react-d3-components.js.map
└── react-d3-components.min.js
├── example
├── index.html
├── react-d3-components.js
└── style.css
├── package-lock.json
├── package.json
└── src
├── AccessorMixin.jsx
├── AreaChart.jsx
├── ArrayifyMixin.jsx
├── Axis.jsx
├── Bar.jsx
├── BarChart.jsx
├── Brush.jsx
├── Chart.jsx
├── DefaultPropsMixin.jsx
├── DefaultScalesMixin.jsx
├── HeightWidthMixin.jsx
├── LineChart.jsx
├── Path.jsx
├── PieChart.jsx
├── ScatterPlot.jsx
├── StackAccessorMixin.jsx
├── StackDataMixin.jsx
├── Tooltip.jsx
├── TooltipMixin.jsx
├── Waveform.jsx
└── index.jsx
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "@babel/preset-env",
4 | [
5 | "@babel/preset-stage-0",
6 | {
7 | "decoratorsLegacy": true
8 | }
9 | ],
10 | "@babel/preset-react"
11 | ]
12 | }
13 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org
2 |
3 | root = true
4 |
5 | [*]
6 | indent_style = space
7 | indent_size = 4
8 | end_of_line = lf
9 | charset = utf-8
10 | trim_trailing_whitespace = true
11 | insert_final_newline = true
12 |
13 | [*.md]
14 | trim_trailing_whitespace = false
15 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "rules": {
3 | "quotes": [2, "single"],
4 | "linebreak-style": [2, "unix"],
5 | "semi": [2, "always"],
6 | "no-extra-parens": [2, "all"],
7 | "curly": [2, "multi-line"],
8 | "no-multi-spaces": 2,
9 | "array-bracket-spacing": [2, "never"],
10 | "block-spacing": [2, "always"],
11 | "camelcase": 2,
12 | "comma-spacing": [2, {
13 | "before": false,
14 | "after": true
15 | }],
16 | "eol-last": 2,
17 | "func-call-spacing": [2, "never"],
18 | "jsx-quotes": [2, "prefer-double"],
19 | "keyword-spacing": 2,
20 | "no-trailing-spaces": 2,
21 | "semi-spacing": [2, {
22 | "before": false,
23 | "after": true
24 | }],
25 | "space-before-blocks": 2,
26 | "space-before-function-paren": [2, {
27 | "anonymous": "always",
28 | "named": "never"
29 | }],
30 | "space-infix-ops": 2,
31 | "arrow-body-style": [2, "as-needed"],
32 | "arrow-parens": [2, "as-needed"],
33 | "arrow-spacing": 2,
34 | "no-useless-rename": 2,
35 | "no-var": 2,
36 | "object-shorthand": [2, "always", {
37 | "avoidQuotes": true
38 | }],
39 | "prefer-const": 2,
40 | "template-curly-spacing": 2,
41 | "dot-location": [2, "property"],
42 | "no-multiple-empty-lines": [2, {
43 | "max": 1,
44 | "maxEOF": 1
45 | }],
46 | "react/display-name": 2,
47 | "react/jsx-no-duplicate-props": 2,
48 | "react/jsx-no-undef": 2,
49 | "react/jsx-uses-react": 2,
50 | "react/jsx-uses-vars": 2,
51 | "react/no-deprecated": 1,
52 | "react/no-direct-mutation-state": 2,
53 | "react/no-is-mounted": 2,
54 | "react/no-unknown-property": 2,
55 | "react/no-render-return-value": 2,
56 | "react/prop-types": 0,
57 | "react/react-in-jsx-scope": 2,
58 | "react/require-render-return": 2,
59 | "react/self-closing-comp": [2, {
60 | "component": true,
61 | "html": true
62 | }],
63 | "react/jsx-no-bind": [2, {
64 | "ignoreRefs": true,
65 | "allowArrowFunctions": true,
66 | "allowBind": false
67 | }],
68 | "react/jsx-equals-spacing": [2, "never"],
69 | "react/jsx-curly-spacing": [2, "never"]
70 | },
71 | "parser": "babel-eslint",
72 | "parserOptions": {
73 | "ecmaVersion": 6,
74 | "sourceType": "module",
75 | "ecmaFeatures": {
76 | "jsx": true,
77 | "modules": true
78 | }
79 | },
80 | "env": {
81 | "browser": true,
82 | "es6": true,
83 | "node": true
84 | },
85 | "plugins": [
86 | "react"
87 | ],
88 | "extends": ["eslint:recommended", "prettier"]
89 | }
90 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | lib/
2 | node_modules/
3 | .DS_Store/
4 |
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "browser": true,
3 | "browserify": true,
4 | "esnext": true,
5 | "bitwise": true,
6 | "curly": true,
7 | "eqeqeq": true,
8 | "forin": true,
9 | "funcscope": true,
10 | "immed": true,
11 | "indent": 4,
12 | "latedef": true,
13 | "singleGroups": true,
14 | "undef": true,
15 | "unused": true
16 | }
--------------------------------------------------------------------------------
/.lintstagedrc:
--------------------------------------------------------------------------------
1 | {
2 | "src/*.jsx": [
3 | "eslint --fix",
4 | "git add"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | example
2 | src
3 | .jshintrc
4 | .eslintrc
5 | .editorconfig
6 | .npmignore
7 | webpack.config.min.js
8 | webpack.config.js
9 |
10 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Neri Marschik
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/LICENSE.d3:
--------------------------------------------------------------------------------
1 | Copyright (c) 2010-2015, Michael Bostock
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 |
7 | * Redistributions of source code must retain the above copyright notice, this
8 | list of conditions and the following disclaimer.
9 |
10 | * Redistributions in binary form must reproduce the above copyright notice,
11 | this list of conditions and the following disclaimer in the documentation
12 | and/or other materials provided with the distribution.
13 |
14 | * The name Michael Bostock may not be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
21 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Looking for maintainers. If you are interested in maintaining this library please open an issue.
2 |
3 | # react-d3-components
4 |
5 | > D3 Components for React
6 |
7 | Let React have complete control over the DOM even when using D3. This way we can benefit from Reacts Virtual DOM.
8 |
9 | 
10 |
11 | ## Table of Contents
12 | * [Installation](#installation)
13 | * [Development](#development)
14 | * [Description](#description)
15 | * [Documentation](#documentation)
16 | * [Features](#features)
17 | * [Todo](#todo)
18 | * [Changelog](#changelog)
19 | * [Examples](#examples)
20 | * [Bar Chart](#barchart)
21 | * [Brush](#brush)
22 | * [Tooltips](#tooltips)
23 | * [Axis parameters](#axis-parameters)
24 | * [Custom accessors](#custom-accessors)
25 | * [Customization](#overriding-default-parameters)
26 | * [Stacked Bar Chart](#stackedbarchart)
27 | * [Grouped Bar Chart](#groupedbarchart)
28 | * [Scatter, Line and Area Charts](#other-charts)
29 | * [Pie Chart](#piechart)
30 | * [Waveform](#waveform)
31 |
32 | ## Installation
33 | ```
34 | npm install react-d3-components
35 | ```
36 |
37 | ## Development
38 | In order to compile the code, from the repository folder, type in your terminal
39 | ```
40 | npm install & npm run build:js
41 | ```
42 | This will install the dependencies required and run the build:js. At the end of the process the compiled .js and min.js will be available in the dist folder.
43 | Examples are available in the folder example.
44 | From the root folder type
45 | ```
46 | python -m SimpleHTTPServer 8000
47 | ```
48 | to start a webserver, and navigate to http://localhost:8000/example in order to visualize the new example page.
49 | Note that the example page directly points to the dist folder.
50 |
51 |
52 | ## Description
53 | Ideally the library should be usable with minimum configuration. Just put the data in and see the charts.
54 | I try to provide sensible defaults, but since for most use-cases we need to customize D3's parameters they will be made accessible to the user. Most Charts will turn into their stacked variant when given an array as input.
55 |
56 | If you like the project please consider starring and a pull request. I am open for any additions.
57 |
58 | ## Documentation
59 | Documentation is in the [Wiki](https://github.com/codesuki/react-d3-components/wiki).
60 | For quick testing the [examples](#examples) might be enough.
61 |
62 | ## Features
63 | * Brushes
64 | * Tooltips
65 | * Custom accessors to support any data format
66 | * Negative axes
67 | * CSS classes to allow styling
68 | * Bar Chart
69 | * Stacked Bar Chart
70 | * Scatter Plot
71 | * Line Chart
72 | * Area Chart
73 | * Stacked Area Chart
74 | * Pie Plot
75 |
76 | ## Todo
77 | * More Charts
78 | * Animations
79 | * Legend
80 | * Documentation
81 | * Tests
82 |
83 | ## Changelog
84 | * 0.6.6: Fix ticks rendering over bar chart
85 | * 0.6.5:
86 | * Add tickDirection property to Axis
87 | * Add hideLabels property to PieChart
88 | * Add yOrientation property to AreaChart
89 | * Fix Line Chart rendering
90 | * 0.6.4: Fixed react dependency version (was 0.15.0 instead of 15.0.0)
91 | * 0.6.3: Changed react dependency version to >=0.14.0 to allow react 0.15.
92 | * 0.6.2: Fixed build system. Added colorByLabel property to BarChart.
93 | * 0.6.1: Fixed 'BarChart.getDOMNode(...) is deprecated.'
94 | * 0.6.0: Added [Waveform Chart](#waveform). Moved to React 0.14.
95 | * 0.5.2: Fixed default scale for dates
96 | * 0.5.1: Fixed new props not being used by DefaultScalesMixin
97 | * 0.5.0:
98 | * Improved tooltip. (see example below)
99 | * Tooltip now has different modes.
100 | * AreaChart tooltip now contains x-value argument.
101 | * Support for grouped bar charts. (see example below)
102 | * Support to include child elements inside charts.
103 | * Several bug fixes including recent pull requests.
104 | * React is now a peer dependency
105 | * 0.4.8: Fixed bug were graphs don't resize correctly.
106 | * 0.4.7: Moved to React 0.13.1
107 | * 0.4.6:
108 | * Added sort property to PieChart, same usage as d3.Pie.sort().
109 | * Added support for strokeWidth, strokeDasharray, strokeLinecap to LineChart, can be string or function.
110 | * Small bug fixes.
111 | * 0.4.5: Fixed tooltip not showing when mouse is over tooltip symbol. Tooltip will soon be revamped to allow custom components.
112 | * 0.4.4: Fixed tooltip position inside relative layout containers. Moved to webpack.
113 | * 0.4.3: Fixed tooltip not showing in Safari.
114 | * 0.4.2: Improved LineChart tooltip to show d3.svg.symbol() on nearest data point. Can be customized with shape and shapeColor props. LineChart toolip callback is callback(label, value) now where the format of value depends on your data format, default is {x: x, y: y}.
115 | * 0.4.1: Fixed compatibility issues with Safari and possible other browsers not supporting Number.isFinite().
116 | * 0.4.0: Added X-Axis Brush. Functioning but might change to improve ease of usage etc. Fixed Axis tickFormat not being set correctly.
117 | * 0.3.6: Fixed not correctly requiring D3.
118 | * 0.3.5: Fixed npm packaging.
119 | * 0.3.4: Charts now render correctly when included in another component. Line chart default axes now meet at 0.
120 | * 0.3.0: Added tooltips and made axis properties accessible.
121 | * 0.2.2: Fixed accessors not being used everywhere
122 | * 0.2.1: Excluded external libraries from build and make it usable as a browser include
123 | * 0.2.0: Custom accessors, stacked charts, default scales
124 |
125 | ## Examples
126 | Check out example/index.html found [here](http://codesuki.github.io/react-d3-components/example.html).
127 |
128 | ### BarChart
129 | ```javascript
130 | var BarChart = ReactD3.BarChart;
131 |
132 | var data = [{
133 | label: 'somethingA',
134 | values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
135 | }];
136 |
137 | React.render(
138 | ,
143 | document.getElementById('location')
144 | );
145 | ```
146 |
147 | 
148 |
149 | ### Brush
150 | With Brushes we can build interactive Graphs. My personal use-case is to select date ranges as shown below and in the example.
151 | The Brush feature is still incomplete, for now only offering a x-Axis Brush but y-Axis will follow soon as well as refactoring.
152 | For now the Brush is rendered in its own SVG, this allows flexible use but might change in the future, or become optional.
153 | Also there is no Brush support for the built-in default Scales.
154 | ```css
155 | .brush .extent {
156 | stroke: #000;
157 | fill-opacity: .125;
158 | shape-rendering: crispEdges;
159 | }
160 |
161 | .brush .background {
162 | fill: #ddd;
163 | }
164 | ```
165 | ```javascript
166 | var LineChart = ReactD3.LineChart;
167 | var Brush = ReactD3.Brush;
168 |
169 | var SomeComponent = React.createClass({
170 | getInitialState: function() {
171 | return {
172 | data: {label: '', values: [
173 | {x: new Date(2015, 2, 5), y: 1},
174 | {x: new Date(2015, 2, 6), y: 2},
175 | {x: new Date(2015, 2, 7), y: 0},
176 | {x: new Date(2015, 2, 8), y: 3},
177 | {x: new Date(2015, 2, 9), y: 2},
178 | {x: new Date(2015, 2, 10), y: 3},
179 | {x: new Date(2015, 2, 11), y: 4},
180 | {x: new Date(2015, 2, 12), y: 4},
181 | {x: new Date(2015, 2, 13), y: 1},
182 | {x: new Date(2015, 2, 14), y: 5},
183 | {x: new Date(2015, 2, 15), y: 0},
184 | {x: new Date(2015, 2, 16), y: 1},
185 | {x: new Date(2015, 2, 16), y: 1},
186 | {x: new Date(2015, 2, 18), y: 4},
187 | {x: new Date(2015, 2, 19), y: 4},
188 | {x: new Date(2015, 2, 20), y: 5},
189 | {x: new Date(2015, 2, 21), y: 5},
190 | {x: new Date(2015, 2, 22), y: 5},
191 | {x: new Date(2015, 2, 23), y: 1},
192 | {x: new Date(2015, 2, 24), y: 0},
193 | {x: new Date(2015, 2, 25), y: 1},
194 | {x: new Date(2015, 2, 26), y: 1}
195 | ]},
196 | xScale: d3.time.scale().domain([new Date(2015, 2, 5), new Date(2015, 2, 26)]).range([0, 400 - 70]),
197 | xScaleBrush: d3.time.scale().domain([new Date(2015, 2, 5), new Date(2015, 2, 26)]).range([0, 400 - 70])
198 | };
199 | },
200 |
201 | render: function() {
202 | return (
203 |
204 |
212 |
213 |
222 |
223 |
224 | );
225 | },
226 |
227 | _onChange: function(extent) {
228 | this.setState({xScale: d3.time.scale().domain([extent[0], extent[1]]).range([0, 400 - 70])});
229 | }
230 | });
231 | ```
232 | 
233 |
234 | ### Tooltips
235 | You can provide a callback to every chart that will return html for the tooltip.
236 | Depending on the type of chart the callback will receive different parameters that are useful.
237 |
238 | * Bar Chart: label is the first parameter. y0, y of the hovered bar and the total bar height in case of a stacked bar chart.
239 | * Scatter Plot: x, y of the hovered point.
240 | * Pie Chart: label is the first parameter. y of the hovered wedge.
241 | * Area Chart: closest y value to the cursor of the area under the mouse and the cumulative y value in case of a stacked area chart. x value is the third parameter. label is the fourth parameter.
242 |
243 | Example Scatter Plot:
244 | ```javascript
245 | var tooltipScatter = function(x, y) {
246 | return "x: " + x + " y: " + y;
247 | };
248 |
249 | React.render(,
257 | document.getElementById('scatterplot')
258 | );
259 | ```
260 |
261 | Tooltip positioning is influenced by `tooltipOffset` `tooltipContained` and `tooltipMode`, which has 3 options `mouse`, `fixed`, `element`.
262 |
263 | * `mouse` is the default behavior and just follows the mouse
264 | * `fixed` uses `tooltipOffset` as an offset from the top left corner of the svg
265 | * `element` puts the tooltip on top of data points for line/area/scatter charts and on top of bars for the barchart
266 |
267 | `tooltipOffset` is an object with `top` and `left` keys i.e. `{top: 10, left: 10}`
268 |
269 | If `tooltipContained` is true the tooltip will try to stay inside the svg by using `css-transform`.
270 |
271 | 
272 |
273 | ### Axis parameters
274 | All D3 axis parameters can optionally be provided to the chart. For detailed explanation please check the documentation.
275 | ```javascript
276 |
277 | React.render(,
286 | document.getElementById('linechart'));
287 | ```
288 |
289 | The following are the default values.
290 | ```javascript
291 | {
292 | tickArguments: [10],
293 | tickValues: null,
294 | tickFormat: x => { return x; },
295 | innerTickSize: 6,
296 | tickPadding: 3,
297 | outerTickSize: 6,
298 | className: "axis",
299 | zero: 0,
300 | label: ""
301 | }
302 | ```
303 |
304 | ### Custom Accessors
305 | ```javascript
306 | data = [{
307 | customLabel: 'somethingA',
308 | customValues: [[0, 3], [1.3, -4], [3, 7], [-3.5, 8], [4, 7], [4.5, 7], [5, -7.8]]
309 | }];
310 |
311 | var labelAccessor = function(stack) { return stack.customLabel; };
312 | var valuesAccessor = function(stack) { return stack.customValues; };
313 | var xAccessor = function(element) { return element[0]; };
314 | var yAccessor = function(element) { return element[1]; };
315 |
316 | React.render(,
326 | document.getElementById('location'));
327 | ```
328 |
329 | ### Overriding default parameters
330 | All Charts provide defaults for scales, colors, etc...
331 | If you want to use your own scale just pass it to the charts constructor.
332 |
333 | The scales are normal D3 objects, their documentation can be found [here](https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md) and [here](https://github.com/d3/d3-3.x-api-reference/blob/master/Quantitative-Scales.md).
334 |
335 | There are more parameters like barPadding, strokeWidth, fill, opacity, etc. please check the documentation for details.
336 |
337 | ```javascript
338 | var xScale = d3.scale.ordinal(); //... + set it up appropriately
339 | var yScale = d3.scale.linear();
340 | var colorScale = d3.scale.category20();
341 |
342 |
350 | ```
351 |
352 | #### LineChart stroke style
353 | You can customize the line style of LineCharts with CSS or if you want to have more control over how each line in your dataset gets rendered you can use the stroke property of LineChart as follows. Note that you do not have to set all the properties in the object.
354 |
355 | ```javascript
356 | var dashFunc = function(label) {
357 | if (label == "somethingA") {
358 | return "4 4 4";
359 | }
360 | if (label == "somethingB") {
361 | return "3 4 3";
362 | }
363 | }
364 |
365 | var widthFunc = function(label) {
366 | if (label == "somethingA") {
367 | return "4";
368 | }
369 | if (label == "somethingB") {
370 | return "2";
371 | }
372 | }
373 |
374 | var linecapFunc = function(label) {
375 | if (label == "somethingA") {
376 | return "round";
377 | }
378 | }
379 |
380 | React.render(,
391 | document.getElementById('linechart')
392 | );
393 | ```
394 | 
395 |
396 | ### StackedBarChart
397 | ```javascript
398 | var BarChart = ReactD3.BarChart;
399 |
400 | data = [
401 | {
402 | label: 'somethingA',
403 | values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
404 | },
405 | {
406 | label: 'somethingB',
407 | values: [{x: 'SomethingA', y: 6}, {x: 'SomethingB', y: 8}, {x: 'SomethingC', y: 5}]
408 | },
409 | {
410 | label: 'somethingC',
411 | values: [{x: 'SomethingA', y: 6}, {x: 'SomethingB', y: 8}, {x: 'SomethingC', y: 5}]
412 | }
413 | ];
414 |
415 | React.render(,
420 | document.getElementById('location'));
421 | ```
422 |
423 | 
424 |
425 | ### Grouped Bar Chart
426 | ```javascript
427 | var BarChart = ReactD3.BarChart;
428 |
429 | data = [
430 | {
431 | label: 'somethingA',
432 | values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
433 | },
434 | {
435 | label: 'somethingB',
436 | values: [{x: 'SomethingA', y: 6}, {x: 'SomethingB', y: 8}, {x: 'SomethingC', y: 5}]
437 | },
438 | {
439 | label: 'somethingC',
440 | values: [{x: 'SomethingA', y: 6}, {x: 'SomethingB', y: 8}, {x: 'SomethingC', y: 5}]
441 | }
442 | ];
443 |
444 | React.render(,
450 | document.getElementById('location'));
451 | ```
452 |
453 | 
454 |
455 | ### Other Charts
456 | ```javascript
457 | var ScatterPlot = ReactD3.ScatterPlot;
458 | var LineChart = ReactD3.LineChart;
459 | var AreaChart = ReactD3.AreaChart;
460 |
461 | data = [
462 | {
463 | label: 'somethingA',
464 | values: [{x: 0, y: 2}, {x: 1.3, y: 5}, {x: 3, y: 6}, {x: 3.5, y: 6.5}, {x: 4, y: 6}, {x: 4.5, y: 6}, {x: 5, y: 7}, {x: 5.5, y: 8}]
465 | },
466 | {
467 | label: 'somethingB',
468 | values: [{x: 0, y: 3}, {x: 1.3, y: 4}, {x: 3, y: 7}, {x: 3.5, y: 8}, {x: 4, y: 7}, {x: 4.5, y: 7}, {x: 5, y: 7.8}, {x: 5.5, y: 9}]
469 | }
470 | ];
471 |
472 | React.render(,
477 | document.getElementById('location'));
478 |
479 | React.render(,
484 | document.getElementById('location')
485 | );
486 |
487 | React.render(,
493 | document.getElementById('location')
494 | );
495 | ```
496 |
497 | 
498 |
499 | 
500 |
501 | 
502 |
503 | ### PieChart
504 | By default d3 sorts the PieChart but you can use the sort property to pass a custom comparator or null to disable sorting.
505 |
506 | ```javascript
507 | var PieChart = ReactD3.PieChart;
508 |
509 | var data = {
510 | label: 'somethingA',
511 | values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
512 | };
513 |
514 | var sort = null; // d3.ascending, d3.descending, func(a,b) { return a - b; }, etc...
515 |
516 | React.render(,
523 | document.getElementById('location')
524 | );
525 | ```
526 |
527 | 
528 |
529 | ### Waveform
530 | The waveform chart displays a sequence of values as if they were part of an audio waveform.
531 | The values are centered on the horizontal axis and reflected along the horizontal origin.
532 | For now only values in the range [0,1] are supported.
533 |
534 | The graph can accept a colorScale parameter, that is an array of values in the range [0,width], where width is the width of the graph.
535 | Following an example of gradient from white to black for a waveform of width 200.
536 |
537 | ```javascript
538 | colorScale={ d3.scale.linear()
539 | .domain([0,200])
540 | .range(['#fff','#000'])}
541 | ```
542 | The graph is responsive and adopts a viewBox strategy to resize the graph maintaining its proportions.
543 | We also adopt subSampling in order to maintain the graph rapresentation of the waveform.
544 | As it is now each bar needs to have a minimum width of 1px, as well as 1px space between to adjacent bars.
545 | In order to allow this, we subsample the input data in order to have exactly a maximum of width/2 elements.
546 |
547 | It is therefore a good strategy to select the width of the graph to be twice the length of the dataset. The viewBox responsiveness will then resize the graph to the width of the container.
548 | If the samples are less than half of the space available we just display them with a width >1px. Space between bars is increased in width as well.
549 |
550 | 
551 |
552 |
553 |
554 | ## Credits
555 | This library uses parts of [D3.js](https://github.com/mbostock/d3).
556 |
--------------------------------------------------------------------------------
/config/webpack.config.base.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 |
3 | module.exports = {
4 | entry: path.resolve(__dirname, '../src/index.jsx'),
5 | output: {
6 | library: 'ReactD3',
7 | libraryTarget: 'umd',
8 | path: path.resolve(__dirname, '../dist')
9 | },
10 | module: {
11 | rules: [
12 | {
13 | test: /.jsx$/,
14 | loader: 'babel-loader',
15 | include: path.resolve(__dirname, '../src')
16 | }
17 | ]
18 | },
19 | resolve: {
20 | extensions: ['.js', '.jsx']
21 | },
22 | externals: {
23 | d3: true,
24 | react: 'React',
25 | 'react-dom': 'ReactDOM'
26 | }
27 | };
28 |
--------------------------------------------------------------------------------
/config/webpack.config.dev.js:
--------------------------------------------------------------------------------
1 | const merge = require('webpack-merge');
2 |
3 | module.exports = merge(require('./webpack.config.base'), {
4 | mode: 'development',
5 | devtool: 'eval',
6 | output: {
7 | filename: 'react-d3-components.js'
8 | }
9 | });
10 |
--------------------------------------------------------------------------------
/config/webpack.config.prod.js:
--------------------------------------------------------------------------------
1 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
2 | const merge = require('webpack-merge');
3 | const {
4 | optimize: { OccurrenceOrderPlugin, AggressiveMergingPlugin }
5 | } = require('webpack');
6 |
7 | module.exports = merge(require('./webpack.config.base'), {
8 | mode: 'production',
9 | devtool: 'source-map',
10 | output: {
11 | filename: 'react-d3-components.min.js',
12 | sourceMapFilename: 'react-d3-components.js.map'
13 | },
14 | plugins: [
15 | new OccurrenceOrderPlugin(),
16 | new AggressiveMergingPlugin(),
17 | new UglifyJsPlugin({
18 | sourceMap: true,
19 | uglifyOptions: {
20 | compress: {
21 | warnings: false
22 | }
23 | }
24 | })
25 | ]
26 | });
27 |
--------------------------------------------------------------------------------
/dist/react-d3-components.min.js:
--------------------------------------------------------------------------------
1 | !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("React"),require("d3"),require("ReactDOM")):"function"==typeof define&&define.amd?define(["React","d3","ReactDOM"],t):"object"==typeof exports?exports.ReactD3=t(require("React"),require("d3"),require("ReactDOM")):e.ReactD3=t(e.React,e.d3,e.ReactDOM)}(window,function(e,t,n){return function(e){var t={};function n(a){if(t[a])return t[a].exports;var r=t[a]={i:a,l:!1,exports:{}};return e[a].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,a){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:a})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(n.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(a,r,function(t){return e[t]}.bind(null,r));return a},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=19)}([function(e,t,n){e.exports=n(21)()},function(t,n){t.exports=e},function(e,t,n){"use strict";var a=n(1),r=n(24);if(void 0===a)throw Error("create-react-class could not find the React object. If you are using script tags, make sure that React is being loaded before create-react-class.");var i=(new a.Component).updater;e.exports=r(a.Component,a.isValidElement,i)},function(e,n){e.exports=t},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=o(n(1)),r=o(n(0)),i=o(n(2));function o(e){return e&&e.__esModule?e:{default:e}}var l=r.default.number,s=r.default.shape,u=(0,i.default)({displayName:"Chart",propTypes:{height:l.isRequired,width:l.isRequired,margin:s({top:l,bottom:l,left:l,right:l}).isRequired},render:function(){var e=this.props,t=e.width,n=e.height,r=e.margin,i=e.viewBox,o=e.preserveAspectRatio,l=e.children;return a.default.createElement("svg",{ref:"svg",width:t,height:n,viewBox:i,preserveAspectRatio:o},a.default.createElement("g",{transform:"translate(".concat(r.left,", ").concat(r.top,")")},l))}});t.default=u},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default={componentWillMount:function(){this._calculateInner(this.props)},componentWillReceiveProps:function(e){this._calculateInner(e)},_calculateInner:function(e){var t=e.height,n=e.width,a=e.margin;this._innerHeight=t-a.top-a.bottom,this._innerWidth=n-a.left-a.right}}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=i(n(0)),r=i(n(3));function i(e){return e&&e.__esModule?e:{default:e}}var o=a.default.oneOfType,l=a.default.object,s=a.default.array,u=a.default.shape,c=a.default.func,d=a.default.number,f={propTypes:{data:o([l,s]).isRequired,height:d.isRequired,width:d.isRequired,margin:u({top:d,bottom:d,left:d,right:d}),xScale:c,yScale:c,colorScale:c},getDefaultProps:function(){return{data:{label:"No data available",values:[{x:"No data available",y:1}]},margin:{top:0,bottom:0,left:0,right:0},xScale:null,yScale:null,colorScale:r.default.scale.category20()}}};t.default=f},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=i(n(0)),r=i(n(17));function i(e){return e&&e.__esModule?e:{default:e}}var o=a.default.func,l=a.default.oneOf,s=a.default.bool,u=a.default.objectOf,c=a.default.number,d={propTypes:{tooltipHtml:o,tooltipMode:l(["mouse","element","fixed"]),tooltipContained:s,tooltipOffset:u(c)},getInitialState:function(){return{tooltip:{hidden:!0}}},getDefaultProps:function(){return{tooltipMode:"mouse",tooltipOffset:{top:-35,left:0},tooltipHtml:null,tooltipContained:!1}},componentDidMount:function(){this._svgNode=r.default.findDOMNode(this).getElementsByTagName("svg")[0]},onMouseEnter:function(e,t){if(this.props.tooltipHtml){e.preventDefault();var n,a=this.props,r=a.margin,i=a.tooltipMode,o=a.tooltipOffset,l=a.tooltipContained,s=this._svgNode;if(s.createSVGPoint){var u=s.createSVGPoint();u.x=e.clientX,u.y=e.clientY,n=[(u=u.matrixTransform(s.getScreenCTM().inverse())).x-r.left,u.y-r.top]}else{var c=s.getBoundingClientRect();n=[e.clientX-c.left-s.clientLeft-r.left,e.clientY-c.top-s.clientTop-r.top]}var d=function(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],a=!0,r=!1,i=void 0;try{for(var o,l=e[Symbol.iterator]();!(a=(o=l.next()).done)&&(n.push(o.value),!t||n.length!==t);a=!0);}catch(e){r=!0,i=e}finally{try{a||null==l.return||l.return()}finally{if(r)throw i}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}(this._tooltipHtml(t,n),3),f=d[0],p=d[1],h=d[2],m=s.getBoundingClientRect().top+r.top,v=s.getBoundingClientRect().left+r.left,y=0,g=0;"fixed"===i?(y=m+o.top,g=v+o.left):"element"===i?(y=m+h+o.top,g=v+p+o.left):(y=e.clientY+o.top,g=e.clientX+o.left);var x=50;l&&(x=function(e,t,n){return 0*(1-e)+100*e}(n[0]/s.getBoundingClientRect().width)),this.setState({tooltip:{top:y,left:g,hidden:!1,html:f,translate:x}})}},onMouseLeave:function(e){this.props.tooltipHtml&&(e.preventDefault(),this.setState({tooltip:{hidden:!0}}))}};t.default=d},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=o(n(1)),r=o(n(0)),i=o(n(2));function o(e){return e&&e.__esModule?e:{default:e}}var l=r.default.array,s=r.default.func,u=r.default.oneOf,c=r.default.number,d=r.default.string,f=(0,i.default)({displayName:"Axis",propTypes:{tickArguments:l,tickValues:l,tickFormat:s,tickDirection:u(["horizontal","vertical","diagonal"]),innerTickSize:c,tickPadding:c,outerTickSize:c,scale:s.isRequired,className:d,zero:c,orientation:u(["top","bottom","left","right"]).isRequired,label:d},getDefaultProps:function(){return{tickArguments:[10],tickValues:null,tickFormat:null,tickDirection:"horizontal",innerTickSize:6,tickPadding:3,outerTickSize:6,className:"axis",zero:0,label:""}},_getTranslateString:function(){var e=this.props,t=e.orientation,n=e.height,a=e.width,r=e.zero;return"top"===t?"translate(0, ".concat(r,")"):"bottom"===t?"translate(0, ".concat(0==r?n:r,")"):"left"===t?"translate(".concat(r,", 0)"):"right"===t?"translate(".concat(0==r?a:r,", 0)"):""},render:function(){var e=this.props,t=e.height,n=e.tickArguments,r=e.tickValues,i=e.tickDirection,o=e.innerTickSize,l=e.tickPadding,s=e.outerTickSize,u=e.scale,c=e.orientation,d=e.zero,f=this.props,p=f.width,h=f.className,m=f.label,v=this.props.tickFormat,y=null==r?u.ticks?u.ticks.apply(u,n):u.domain():r;v||(v=u.tickFormat?u.tickFormat.apply(u,n):function(e){return e}),d!=t&&d!=p&&0!=d&&(y=y.filter(function(e){return 0!=e}));var g,x,_,E,M,b,S,R,D,w=Math.max(o,0)+l,P="top"===c||"left"===c?-1:1,N=this._d3ScaleRange(u),k=u.rangeBand?function(e){return u(e)+u.rangeBand()/2}:u,O=0;"bottom"===c||"top"===c?(g="translate({}, 0)",x=0,_=P*w,E=0,M=P*o,b=P<0?"0em":".71em",S="middle",R="M".concat(N[0],", ").concat(P*s,"V0H").concat(N[1],"V").concat(P*s),"vertical"===i?(O=-90,x=-w,_=-o,S="end"):"diagonal"===i&&(O=-60,x=-w,_=0,S="end"),D=a.default.createElement("text",{className:"".concat(h," label"),textAnchor:"end",x:p,y:-6},m)):(g="translate(0, {})",x=P*w,_=0,E=P*o,M=0,b=".32em",S=P<0?"end":"start",R="M".concat(P*s,", ").concat(N[0],"H0V").concat(N[1],"H").concat(P*s),"vertical"===i?(O=-90,x-=P*w,_=-(w+o),S="middle"):"diagonal"===i&&(O=-60,x-=P*w,_=-(w+o),S="middle"),D=a.default.createElement("text",{className:"".concat(h," label"),textAnchor:"end",y:6,dy:"left"===c?".75em":"-1.25em",transform:"rotate(-90)"},m));var A=y.map(function(e,t){var n=k(e),r=g.replace("{}",n);return a.default.createElement("g",{key:"".concat(e,".").concat(t),className:"tick",transform:r},a.default.createElement("line",{x2:E,y2:M,stroke:"#aaa"}),a.default.createElement("text",{x:x,y:_,dy:b,textAnchor:S,transform:"rotate(".concat(O,")")},v(e)))}),j=a.default.createElement("path",{className:"domain",d:R,fill:"none",stroke:"#aaa"}),T=a.default.createElement("rect",{className:"axis-background",fill:"none"});return a.default.createElement("g",{ref:"axis",className:h,transform:this._getTranslateString(),style:{shapeRendering:"crispEdges"}},T,A,j,D)},_d3ScaleExtent:function(e){var t=e[0],n=e[e.length-1];return th/2?(p[0].values=function(e,t){for(var n=[],a=e.length,r=a/t,i=0;i=10*Math.PI/180;return a.default.createElement("g",{key:"".concat(o(t.data),".").concat(l(t.data),".").concat(n),className:"arc"},a.default.createElement(_,{data:t.data,fill:i(o(t.data)),d:r(t),onMouseEnter:s,onMouseLeave:u}),!c&&!!t.value&&d&&e.renderLabel(t))});return a.default.createElement("g",null,d)},midAngle:function(e){return e.startAngle+(e.endAngle-e.startAngle)/2}}),M=(0,i.default)({displayName:"PieChart",mixins:[u.default,c.default,d.default,f.default],propTypes:{innerRadius:v,outerRadius:v,labelRadius:v,padRadius:h,cornerRadius:v,sort:x,hideLabels:y},getDefaultProps:function(){return{innerRadius:null,outerRadius:null,labelRadius:null,padRadius:"auto",cornerRadius:0,sort:void 0,hideLabels:!1}},_tooltipHtml:function(e){return[this.props.tooltipHtml(this.props.x(e),this.props.y(e)),0,0]},render:function(){var e=this.props,t=e.data,n=e.width,r=e.height,i=e.margin,u=e.viewBox,c=e.preserveAspectRatio,d=e.colorScale,f=e.padRadius,p=e.cornerRadius,h=e.sort,m=e.x,v=e.y,y=e.values,g=e.hideLabels,x=this.props,_=x.innerRadius,M=x.outerRadius,b=x.labelRadius,S=this._innerWidth,R=this._innerHeight,D=o.default.layout.pie().value(function(e){return v(e)});void 0!==h&&(D=D.sort(h));var w=Math.min(S,R)/2;_||(_=.8*w),M||(M=.4*w),b||(b=.9*w);var P=o.default.svg.arc().innerRadius(_).outerRadius(M).padRadius(f).cornerRadius(p),N=o.default.svg.arc().innerRadius(b).outerRadius(b),k=D(y(t)),O="translate(".concat(S/2,", ").concat(R/2,")");return a.default.createElement("div",null,a.default.createElement(l.default,{height:r,width:n,margin:i,viewBox:u,preserveAspectRatio:c},a.default.createElement("g",{transform:O},a.default.createElement(E,{width:S,height:R,colorScale:d,pie:k,arc:P,outerArc:N,radius:w,x:m,y:v,onMouseEnter:this.onMouseEnter,onMouseLeave:this.onMouseLeave,hideLabels:g})),this.props.children),a.default.createElement(s.default,this.state.tooltip))}});t.default=M},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=v(n(1)),r=v(n(0)),i=v(n(2)),o=v(n(3)),l=v(n(4)),s=v(n(8)),u=v(n(9)),c=v(n(6)),d=v(n(5)),f=v(n(10)),p=v(n(14)),h=v(n(11)),m=v(n(7));function v(e){return e&&e.__esModule?e:{default:e}}function y(){return(y=Object.assign||function(e){for(var t=1;tthis.state.xExtent[1]?(this.setState({xExtent:[this.state.xExtent[1],a],xExtentDomain:null}),this._resizeDir="e"):this.setState({xExtent:[a,this.state.xExtent[1]],xExtentDomain:null}):"e"==this._resizeDir&&(a
2 |
3 |
4 | React D3 Components Examples
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
489 |
490 |