Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Chartjs v2 #227

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
06beb9c
(feat) Chart.js 2.0-dev compatibility
danmolitor Mar 20, 2016
16b1022
Merge pull request #91 from danmolitor/chartjs2dev
austinpray Apr 6, 2016
7f6c340
v2: assign all nextProps dataset properties to chart
ianks Apr 19, 2016
15059bc
v2: Simplify chart type logic
ianks Apr 19, 2016
1da6721
Require chart.js at beginning of module
ianks May 4, 2016
ad46cca
Fixes chart creation on initializeChart
eluciano11 May 6, 2016
0cedffb
Merge pull request #1 from eluciano11/label-fix-v2
ianks May 6, 2016
c711d75
Merge pull request #109 from ianks/label-fix-v2
austinpray May 18, 2016
771a788
fix bug
May 22, 2016
3cfa294
Add a horizontal-bar type, and refactor passed to chart.js
OddEssay Jun 21, 2016
70feda4
Update README to include HorizontalBar as listed type
OddEssay Jun 21, 2016
92aa566
Switch to use not and improve style of syntax
OddEssay Jun 21, 2016
da9f718
Merge pull request #117 from fscz/chartjs-v2
austinpray Jul 14, 2016
381b53b
Fix: unknown prop 'redraw' warning in React v15.2
strawbrary Jul 11, 2016
362e0e5
Merge pull request #122 from OddEssay/feature/horizontal-bar
austinpray Apr 6, 2017
3f5c102
export horizontal bar chart
evanheisler Apr 11, 2017
e24c7ae
Merge pull request #179 from evanheisler/chartjs-v2
austinpray Apr 12, 2017
6836b54
Tag 2.0.0-beta2
austinpray Apr 12, 2017
152605a
Add create-react-class
May 14, 2017
1706499
Use create-react-class to avoid warning
May 14, 2017
e317447
Merge pull request #184 from merlinpatt/chartjs-v2
austinpray Jun 29, 2017
35f665b
Merge pull request #142 from miracle2k/chartjs-v2
benmccann Jan 3, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ rich interactive react charting components using [chart.js](http://www.chartjs.o

* Line chart
* Bar chart
* HorizontalBar chart
* Radar chart
* Polar area chart
* Pie chart
Expand Down
145 changes: 78 additions & 67 deletions dist/react-chartjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ return /******/ (function(modules) { // webpackBootstrap

module.exports = {
Bar: __webpack_require__(1),
Doughnut: __webpack_require__(6),
Line: __webpack_require__(7),
Pie: __webpack_require__(8),
PolarArea: __webpack_require__(9),
Radar: __webpack_require__(10),
Bubble: __webpack_require__(6),
Doughnut: __webpack_require__(7),
Line: __webpack_require__(8),
Pie: __webpack_require__(9),
PolarArea: __webpack_require__(10),
Radar: __webpack_require__(11),
Scatter: __webpack_require__(12),
createClass: __webpack_require__(2).createClass
};

Expand All @@ -78,6 +80,13 @@ return /******/ (function(modules) { // webpackBootstrap
/* 2 */
/***/ function(module, exports, __webpack_require__) {

// Designed to be used with the current v2.0-dev version of Chart.js
// It's not on NPM, but if you'd like to use it you can, install it
// by setting the chart.js version in your package.json to:
// "chart.js": "git://github.com/danmolitor/Chart.js.git#v2.0-dev"

// I'll try to rework this for their 2.0.0 beta as well.

var React = __webpack_require__(3);
var ReactDOM = __webpack_require__(4);

Expand Down Expand Up @@ -112,35 +121,63 @@ return /******/ (function(modules) { // webpackBootstrap
this.initializeChart(this.props);
};


classData.componentWillUnmount = function() {
var chart = this.state.chart;
chart.destroy();
};

classData.componentWillReceiveProps = function(nextProps) {
var chart = this.state.chart;
if (nextProps.redraw) {
chart.destroy();
this.initializeChart(nextProps);
} else {
dataKey = dataKey || dataKeys[chart.name];
updatePoints(nextProps, chart, dataKey);
if (chart.scale) {
chart.scale.xLabels = nextProps.data.labels;
chart.scale.calculateXLabelRotation();
}
chart.update();
}
};

// // Reset the array of datasets
chart.data.datasets.forEach(function(set, setIndex) {
set.data.forEach(function(val, pointIndex) {
set.data = [];
});
});

// // Reset the array of labels
chart.data.labels = [];

// Adds the datapoints from nextProps
nextProps.data.datasets.forEach(function(set, setIndex) {
set.data.forEach(function(val, pointIndex) {
chart.data.datasets[setIndex].data[pointIndex] = nextProps.data.datasets[setIndex].data[pointIndex];
});
});

// Sets the labels from nextProps
nextProps.data.labels.forEach(function(val, labelIndex) {
chart.data.labels[labelIndex] = nextProps.data.labels[labelIndex];
});

// Updates Chart with new data
chart.update();
};

classData.initializeChart = function(nextProps) {
var Chart = __webpack_require__(5);
var el = ReactDOM.findDOMNode(this);
var ctx = el.getContext("2d");
var chart = new Chart(ctx)[chartType](nextProps.data, nextProps.options || {});

if (chartType === 'PolarArea'){
var chart = new Chart(ctx, {
type: 'polarArea',
data: nextProps.data,
options: nextProps.options
});
} else {
var chart = new Chart(ctx, {
type: chartType.toLowerCase(),
data: nextProps.data,
options: nextProps.options
});
}
this.state.chart = chart;
};


// return the chartjs instance
classData.getChart = function() {
return this.state.chart;
Expand All @@ -165,50 +202,6 @@ return /******/ (function(modules) { // webpackBootstrap
}
};

var dataKeys = {
'Line': 'points',
'Radar': 'points',
'Bar': 'bars'
};

var updatePoints = function(nextProps, chart, dataKey) {
var name = chart.name;

if (name === 'PolarArea' || name === 'Pie' || name === 'Doughnut') {
nextProps.data.forEach(function(segment, segmentIndex) {
if (!chart.segments[segmentIndex]) {
chart.addData(segment);
} else {
Object.keys(segment).forEach(function (key) {
chart.segments[segmentIndex][key] = segment[key];
});
}
});
} else {
while (chart.scale.xLabels.length > nextProps.data.labels.length) {
chart.removeData();
}
nextProps.data.datasets.forEach(function(set, setIndex) {
set.data.forEach(function(val, pointIndex) {
if (typeof(chart.datasets[setIndex][dataKey][pointIndex]) == "undefined") {
addData(nextProps, chart, setIndex, pointIndex);
} else {
chart.datasets[setIndex][dataKey][pointIndex].value = val;
}
});
});
}
};

var addData = function(nextProps, chart, setIndex, pointIndex) {
var values = [];
nextProps.data.datasets.forEach(function(set) {
values.push(set.data[pointIndex]);
});
chart.addData(values, nextProps.data.labels[setIndex]);
};


/***/ },
/* 3 */
/***/ function(module, exports) {
Expand All @@ -233,7 +226,7 @@ return /******/ (function(modules) { // webpackBootstrap

var vars = __webpack_require__(2);

module.exports = vars.createClass('Doughnut', ['getSegmentsAtEvent']);
module.exports = vars.createClass('Bubble', ['getPointsAtEvent']);


/***/ },
Expand All @@ -242,7 +235,7 @@ return /******/ (function(modules) { // webpackBootstrap

var vars = __webpack_require__(2);

module.exports = vars.createClass('Line', ['getPointsAtEvent']);
module.exports = vars.createClass('Doughnut', ['getSegmentsAtEvent']);


/***/ },
Expand All @@ -251,7 +244,7 @@ return /******/ (function(modules) { // webpackBootstrap

var vars = __webpack_require__(2);

module.exports = vars.createClass('Pie', ['getSegmentsAtEvent']);
module.exports = vars.createClass('Line', ['getPointsAtEvent']);


/***/ },
Expand All @@ -260,7 +253,7 @@ return /******/ (function(modules) { // webpackBootstrap

var vars = __webpack_require__(2);

module.exports = vars.createClass('PolarArea', ['getSegmentsAtEvent']);
module.exports = vars.createClass('Pie', ['getSegmentsAtEvent']);


/***/ },
Expand All @@ -269,9 +262,27 @@ return /******/ (function(modules) { // webpackBootstrap

var vars = __webpack_require__(2);

module.exports = vars.createClass('PolarArea', ['getSegmentsAtEvent']);


/***/ },
/* 11 */
/***/ function(module, exports, __webpack_require__) {

var vars = __webpack_require__(2);

module.exports = vars.createClass('Radar', ['getPointsAtEvent']);


/***/ },
/* 12 */
/***/ function(module, exports, __webpack_require__) {

var vars = __webpack_require__(2);

module.exports = vars.createClass('Scatter', ['getPointsAtEvent']);


/***/ }
/******/ ])
});
Expand Down
2 changes: 1 addition & 1 deletion dist/react-chartjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module.exports = {
Bar: require('./lib/bar'),
Bubble: require('./lib/bubble'),
Doughnut: require('./lib/doughnut'),
HorizontalBar: require('./lib/horizontal-bar'),
Line: require('./lib/line'),
Pie: require('./lib/pie'),
PolarArea: require('./lib/polar-area'),
Radar: require('./lib/radar'),
Scatter: require('./lib/scatter'),
createClass: require('./lib/core').createClass
};
3 changes: 3 additions & 0 deletions lib/bubble.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var vars = require('./core');

module.exports = vars.createClass('Bubble', ['getPointsAtEvent']);
Loading