From eb2c1f01f2b5783e0da242a315a643168ce5c787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20C=C3=A1rdenas?= Date: Fri, 20 Mar 2015 15:15:06 +0100 Subject: [PATCH] Allow custom chart types. This changeset exposes the `createClass` method in `lib/core`, and adds a third argument to it so that you can pass the `dataKey` value that was previously fixed in the `dataKeys` variable. E. g. if you want to use https://github.com/Regaddi/Chart.StackedBar.js, you would do something like `var StackedBarChart = require('react-chartjs').createClass('StackedBar', ['getBarsAtEvent'], 'bars');` (provided you previously registered `StackedBar` with Chart.js, of course). --- index.js | 3 ++- lib/core.js | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6b8887c..beaedd0 100644 --- a/index.js +++ b/index.js @@ -4,5 +4,6 @@ module.exports = { Line: require('./lib/line'), Pie: require('./lib/pie'), PolarArea: require('./lib/polar-area'), - Radar: require('./lib/radar') + Radar: require('./lib/radar'), + createClass: require('./lib/core').createClass }; diff --git a/lib/core.js b/lib/core.js index e116173..5cfa723 100644 --- a/lib/core.js +++ b/lib/core.js @@ -1,5 +1,5 @@ module.exports = { - createClass: function(chartType, methodNames) { + createClass: function(chartType, methodNames, dataKey) { var classData = { displayName: chartType + 'Chart', getInitialState: function() { return {}; }, @@ -40,7 +40,8 @@ module.exports = { chart.destroy(); this.initializeChart(nextProps); } else { - updatePoints(nextProps, chart); + dataKey = dataKey || dataKeys[chart.name]; + updatePoints(nextProps, chart, dataKey); chart.update(); } }; @@ -82,7 +83,7 @@ var dataKeys = { 'Bar': 'bars' }; -var updatePoints = function(nextProps, chart) { +var updatePoints = function(nextProps, chart, dataKey) { var name = chart.name; if (name === 'PolarArea' || name === 'Pie' || name === 'Doughnut') { @@ -90,7 +91,6 @@ var updatePoints = function(nextProps, chart) { chart.segments[segmentIndex].value = segment.value; }); } else { - var dataKey = dataKeys[name]; nextProps.data.datasets.forEach(function(set, setIndex) { set.data.forEach(function(val, pointIndex) { chart.datasets[setIndex][dataKey][pointIndex].value = val;