0

I am looking at using reactjs as a framework to manage/control the various chart widgets.

-- when I started looking to place these components into separate js files -- undefined errors started appearing.

-- then how to structure how the charts should be rendered on a panel, how interactions could be implemented -- master/slave relationships -- update cta

http://jsfiddle.net/cfrapLma/28/

var config = [{
              "width": 200,
              "height": 200,
              "type": "piechart",
              "serviceApi": "api.php?mode=GetCars"
          }, {
              "width": 100,
              "height": 100,
              "type": "barchart",
              "serviceApi": "api.php?mode=GetBoats"
          },
          {
              "width": 200,
              "height": 200,
              "type": "piechart",
              "serviceApi": "api.php?mode=GetCars"
          },
          {
              "width": 200,
              "height": 200,
              "type": "linechart",
              "serviceApi": "api.php?mode=GetCars"
          }];



          var MultipleCharts = React.createClass({
              getChart: function(config) {
                  switch (config.type) {

                      case 'piechart':
                          return <PieChart width={config.width} height={config.height} service={config.service} />
                      case 'barchart':
                          return <BarChart width={config.width} height={config.height} service={config.service} />
                      case 'linechart':
                          return <LineChart width={config.width} height={config.height} service={config.service} />
                  }
              },

              render: function () {
                  var config = this.props.config;

                  return (
                      <div>
                          {config.map((chartConfig, index) => {
                              return (
                                  <div key={index} className={'holder' + index}>
                                      {this.getChart(chartConfig)}
                                  </div>
                              )
                          })}
                      </div>
                  );
              }
          });

          var PieChart = React.createClass({
              componentDidMount: function () {
                  var $this = $(ReactDOM.findDOMNode(this));
                  console.log("rendered div now engage d3");
                  // set el height and width etc.
              },
              render: function () {
                  return (
                      <div className="piechart" data-role="piechart" data-width={this.props.width} data-height={this.props.height}
                          data-service={this.props.service}>pie.
                      </div>
                  );
              }
          });

          var LineChart = React.createClass({
              componentDidMount: function () {
                  var $this = $(ReactDOM.findDOMNode(this));
                  console.log("rendered div now engage d3");
                  // set el height and width etc.
              },
              render: function () {
                  return (
                      <div className="linechart" data-role="linechart" data-width={this.props.width} data-height={this.props.height}
                          data-service={this.props.service}>line.
                      </div>
                  );
              }
          });



          var BarChart = React.createClass({
              componentDidMount: function () {
                  var $this = $(ReactDOM.findDOMNode(this));
                  console.log("rendered div now engage d3");
                  // set el height and width etc.
              },
              render: function () {
                  return (
                      <div className="barchart" data-role="barchart" data-width={this.props.width} data-height={this.props.height}
                          data-service={this.props.service}>bar.
                      </div>
                  );
              }
          });


          ReactDOM.render(
              <MultipleCharts config={config} />,
              document.getElementById('example')
          );
The Old County
  • 89
  • 13
  • 59
  • 129

1 Answers1

0

You should use some build tools like webpack or gulp. I am provides an example that how you should do it.

https://github.com/OnlyRefat/Example

It will help to you write moduler code. You can write code in separate files easily.

Rafi Ud Daula Refat
  • 2,187
  • 19
  • 28
  • What if you aren't running node - and using cdn components – The Old County Aug 20 '16 at 10:53
  • If you check all the best practices available on the web you will find that everyone is using npm with that. – Rafi Ud Daula Refat Aug 20 '16 at 10:54
  • I've never really had to setup a node project - also the api work I've made is using codeignitor – The Old County Aug 20 '16 at 11:12
  • is there a way to use require? or webpack in this form? – The Old County Aug 20 '16 at 11:21
  • This is not a node project. It is a frontend work that used npm. You can make call to api using ajax from this app. It is not dependent that the api is using php or node or anything. – Rafi Ud Daula Refat Aug 20 '16 at 11:32
  • sure - but wouldn't you need to get node running to install and run gulp? – The Old County Aug 20 '16 at 11:54
  • thats right. Everyone tries to use that cause it automates the frontend development. http://stackoverflow.com/questions/28684041/what-is-the-purposes-of-javascript-build-tools – Rafi Ud Daula Refat Aug 20 '16 at 11:58
  • So you are saying - just get node in place, get in place to do the build -- but still keep the codeignitor site handling the api in place. eg.. no need to do a complete sql to nosql mongodb conversion and have node also take over the api work? – The Old County Aug 20 '16 at 12:46
  • i would prefer to keep the API and the frontend React work in different place or you can say decouple the frontend and backend. React is good for his data manipulation nature. So it would be great. – Rafi Ud Daula Refat Aug 20 '16 at 13:03
  • Well there is a bounty on offer here - to show me the ways -- including how the data should be handled with react to other systems like d3. I was going to use reactjs to handle placeholder elements for the charts, in which then after rendering the d3 gets invoked and does the api call on its end - feed the chart the data from the codeignitor api service http://stackoverflow.com/questions/39006469/reactjs-dashboard-multiple-instances-with-an-array-of-parameters – The Old County Aug 20 '16 at 13:06