JavaScript Making D3 chart Responsive in React app making chart.js responsive

I am currently trying to render plot in React using D3. To integrate both of them , i have used the following link:

http://oli.me.uk/2015/09/09/d3-within-react-the-right-way/.

I have also tried the solutions provided in the link for making the chart responsive:

Resize svg when window is resized in d3.js

Although it works great in standalone code upon integrating it with React it starts omitting ticks . Working jsfiddle for the D3 alone: https://jsfiddle.net/adityap16/11edxrnq/1/

Code of Standalone D3 :

var data = [
{"mytime": "2015-12-01T11:10:00.000Z", "value": 64},
{"mytime": "2015-12-01T11:15:00.000Z", "value": 67},
{"mytime": "2015-12-01T11:20:00.000Z", "value": 70},
{"mytime": "2015-12-01T11:25:00.000Z", "value": 64},
{"mytime": "2015-12-01T11:30:00.000Z", "value": 72},
{"mytime": "2015-12-01T11:35:00.000Z", "value": 75},
{"mytime": "2015-12-01T11:40:00.000Z", "value": 71},
{"mytime": "2015-12-01T11:45:00.000Z", "value": 80}
];
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ").parse;

data.forEach(function(d) {
          d.mytime = parseDate(d.mytime);
        });
//var margin = { top: 30, right: 30, bottom: 40, left:50 },
var margin = { top: 30, right: 30, bottom: 40, left:50 },
height = 200,
width = 800;
var color =  "green";
var xaxis_param = "mytime";
var yaxis_param = "value"
var params1 =  {margin:margin,height:height,width:width, color: color, xaxis_param:xaxis_param, yaxis_param :yaxis_param};
draw_graph(data,params1);




function  draw_graph(data,params){


    //Get the margin 
    var xaxis_param = params.xaxis_param;
    var yaxis_param = params.yaxis_param;
    var color_code = params.color;
    var margin = params.margin;
    var height = params.height - margin.top - margin.bottom,
        width = params.width - margin.left - margin.right;

    var x_extent = d3.extent(data, function(d){
        return d[xaxis_param]});
    var y_extent = d3.extent(data, function(d){
        return d[yaxis_param]});

    var x_scale = d3.time.scale()
        .domain(x_extent)
        .range([0,width]);


    var y_scale = d3.scale.linear()
        .domain([0,y_extent[1]])
        .range([height,0]);



    //Line
    var lineGen = d3.svg.line()
        .x(function (d) {
            return x_scale(d[xaxis_param]);
        })
        .y(function (d) {
            return y_scale(d[yaxis_param]);
        });
    var myChart = d3.select('body')
              .append("div")
             .classed("svg-container", true)
             .append('svg')
     .attr("preserveAspectRatio", "xMinYMin meet")
   .attr("viewBox", "0 0 800 600")
   //class to make it responsive
   .classed("svg-content-responsive", true)    
                    .attr('class','my-chart')
                    .style('background', '#E7E0CB')

                    .append('g')
                    .attr('transform', 'translate('+ margin.left +', '+ margin.top +')');
            myChart
                    .append('svg:path')
                    .datum(data)
                    .attr('class', 'line')
                    .attr("d",lineGen)
                    .attr('stroke', color_code)
                    .attr('stroke-width', 1)
                    .attr('fill', 'none');


    var legend = myChart.append("g")
          .attr("class", "legend")
          .attr("transform", "translate(" + 5 + "," + (height - 25) + ")")

        legend.append("rect")
          .style("fill", color_code)
          .attr("width", 20)
          .attr("height", 20);

        legend.append("text")
          .text(yaxis_param)
          .attr("x", 25)
          .attr("y", 12);

    var vGuideScale = d3.scale.linear()
        .domain([0,y_extent[1]])
        .range([height, 0])

    var vAxis = d3.svg.axis()
        .scale(vGuideScale)
        .orient('left')
        .ticks(5)


    var hAxis = d3.svg.axis()
        .scale(x_scale)
        .orient('bottom')
        .ticks(d3.time.minute, 5);

  myChart.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(hAxis);

  myChart.append("g")
      .attr("class", "y axis")
      .call(vAxis)


}

I might need to need to setState or something upon resize, that should trigger React to re-render with the new information but being new to React , i am not able to make it work.

Edit: I am still trying to edit but the problem seems to be the ticks on x axis and y axis when you shrink the window you are able to see the ticks but not in a full screen. Still trying to understand why?

Thanks!

Answer:1

Is it possbile to inject raw html code into a BrowserWindow? mainWindow = new BrowserWindow({width: 1200, height: 800}); mainWindow.loadURL("<p>Hello, World!</p>"); The above code will ...

Is it possbile to inject raw html code into a BrowserWindow? mainWindow = new BrowserWindow({width: 1200, height: 800}); mainWindow.loadURL("<p>Hello, World!</p>"); The above code will ...

  1. electron inject html
  2. electron inject into html

Im building a website in firebase. It's a simple look-up service which only has an input element that fires a request to a 3rd party api. www.3rdparty.com/api/[myapikey]/method The problem is ...

Im building a website in firebase. It's a simple look-up service which only has an input element that fires a request to a 3rd party api. www.3rdparty.com/api/[myapikey]/method The problem is ...

I am trying to sort a column with numerical numbers with comma (,). I am getting a wrong result using the num-fmt option: Here is my code : $('#test').DataTable({ columnDefs: [ { ...

I am trying to sort a column with numerical numbers with comma (,). I am getting a wrong result using the num-fmt option: Here is my code : $('#test').DataTable({ columnDefs: [ { ...

  1. jquery datatables sort date
  2. jquery datatables sort by hidden column
  3. jquery datatables sort column
  4. jquery datatables sort date column
  5. jquery datatables sort by date and time
  6. jquery datatables sort by data attribute
  7. jquery datatables sort after load
  8. jquery datatables sort numeric
  9. jquery datatables sort arrows not showing
  10. jquery datatables sort by date not working
  11. jquery datatables sort column ascending
  12. jquery datatables sort
  13. jquery datatables sort by two columns
  14. jquery datatables sort only some columns
  15. jquery datatables sort_both.png
  16. jquery datatables sort event
  17. jquery datatables sort column descending
  18. jquery datatables sort multiple columns
  19. jquery datatables sort not working
  20. jquery datatables sort checkbox column

I have a block on my twig writing a table from a variable received from controller {% block foot_informations %} {% if ads is not empty %} <div class="panel-foot-information row"> ...

I have a block on my twig writing a table from a variable received from controller {% block foot_informations %} {% if ads is not empty %} <div class="panel-foot-information row"> ...

  1. pass array twig to javascript
  2. pass array twig to js
  3. pass array to twig
  4. pass array to twig template
  5. pass array from twig to controller
  6. twig pass array to function
  7. symfony pass array to twig
  8. twig pass array to vue
  9. pass php array to twig
  10. twig pass array to filter