1

Puzzling problem:

My d3 chart, which has a day selector (right arrow) works in a stand-alone file. By "works" I mean when you click on the right arrow it advances through the data array and the chart bars update.

However, inside a jquery mobile multipage document, it won't work in Firefox. In jqm multipage it does work in Chrome (linux and windows) and IE.

Briefly, you click on the "chart page," then click on the right arrow to view the data for each day.

Don't worry about the arrow code, I have to fix it. What worries me is that this doesn't work in Firefox.

Here is the fiddle. Try it in those browsers.

html:

<!-- Start of first page: #one -->
<div data-role='page' id='one'>

  <div data-role='header'>
    <h1>Multi-page</h1>
  </div>
  <!-- /header -->

  <div data-role='content'>
    <h2>One</h2>

    <p>I have an <code>id</code> of 'one' on my page container. I'm first in the source order so I'm shown when the page loads.</p>

    <h3>Show internal pages:</h3>
    <p><a href='#snapshot-chart-page' data-role='button'>Chart page</a></p>
  </div>
  <!-- /content -->

  <div data-role='footer' data-theme='d'>
    <h4>Page Footer</h4>
  </div>
  <!-- /footer -->
</div>
<!-- /page one -->
<div data-role='page' id='snapshot-chart-page' data-ajax='false'>
  <div data-role='header' style='background:#82ca46;'>
    <a href='#nav-panel' data-icon='bars' data-iconpos='notext' class='ui-nodisc-icon' style='background-color: transparent;'>Menu</a>
    <div align='center' style='vertical-align:top;'>
      <h1>Page title
                </h1></div>
    <a href='#add-form' data-icon='calendar' data-iconpos='notext' class='ui-nodisc-icon' style='background-color: transparent;'>Add</a>
  </div>
  <!-- /header -->
  <div role='main' class='ui-content'>
    <div style='width:100%; margin: 0;padding: 0; overflow: auto;'>
      <form style='display: inline-block;
    position: relative;
    vertical-align: middle;
    margin-right: 6px;'>

        <input type='button' data-role='button' data-icon='arrow-l' data-iconpos='notext' class='ui-nodisc-icon previous-next-period left-previous select-custom-14' style='background-color: transparent;' field='quantity'>
        <input type='text' name='quantity' value='0' class='qty' />
        <input type='button' data-role='button' data-icon='arrow-r' data-iconpos='notext' class='ui-nodisc-icon previous-next-period right-next select-custom-14' style='background-color: transparent;' field='quantity'>

      </form>

      <table data-role='table' data-transition='fade' class='ghg-tables'>
        <caption class='barchart_title tabletitle'></caption>
        <thead>
        </thead>
      </table>
      <div class='chart-here'></div>
    </div>
  </div>
  <!-- /main -->
</div>
<!-- /snapshot-chart-page -->

js:

(function() {
  var margin = {
      top: 20,
      right: 20,
      bottom: 40,
      left: 80
    },
    width = 340 - margin.left - margin.right,
    height = 250 - margin.top - margin.bottom;

  var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1, 1);

  var y = d3.scale.linear()
    .range([height, 0]);

  var xAxis = d3.svg.axis()
    .scale(x)
    .orient('bottom');

  var svg = d3.select('.chart-here').append('svg')
    .attr('viewBox', '0 0 340 250')
    .attr('preserveAspectRatio', 'xMinYMin meet')
    .attr('width', width + margin.left + margin.right)
    .attr('height', height + margin.top + margin.bottom)
    .append('g')
    .attr('transform', 'translate(' + margin.left / 1.5 + ',' + margin.top / 1.5 + ')');

  var yAxis = d3.svg.axis()
    .scale(y)
    .orient('left')
    .ticks(5)
    .outerTickSize(0)
    .tickFormat(d3.format(',.3s'));

  var barchart_i = 0;
  var arr1 = [{
    "period": "day",
    "allcars_title": "Mileage by car, on Tuesday, March 01 2016. Total: 454.95M mi.",
    "car": [{
      "Chevvy": 33733000
    }, {
      "Ford": 32633000
    }, {
      "Honda": 119182000
    }, {
      "Tesla": 614000
    }, {
      "Audi": 268292000
    }, {
      "Hummer": 493000
    }]
  }, {
    "period": "day",
    "allcars_title": "Mileage by car, on Wednesday, March 02 2016. Total: 457.26M mi.",
    "car": [{
      "Chevvy": 23052000
    }, {
      "Ford": 44630000
    }, {
      "Honda": 121635000
    }, {
      "Tesla": 2599000
    }, {
      "Audi": 264247000
    }, {
      "Hummer": 1100000
    }]
  }, {
    "period": "day",
    "allcars_title": "Mileage by car, on Thursday, March 03 2016. Total: 452.96M mi.",
    "car": [{
      "Chevvy": 8577000
    }, {
      "Ford": 54172000
    }, {
      "Honda": 121661000
    }, {
      "Tesla": 1975000
    }, {
      "Audi": 265483000
    }, {
      "Hummer": 1089000
    }]
  }];
  var period_grain = arr1[0].period;
  var allcars_hour = arr1[barchart_i];
  var car = allcars_hour.car;
  var allcars_dash_title = allcars_hour.allcars_title;
  jQuery('.barchart_title').text(allcars_dash_title);
  var newobj = [];
  for (var allcars_hourx1 = 0; allcars_hourx1 < car.length; allcars_hourx1++) {
    var xx = car[allcars_hourx1];
    for (var value in xx) {
      var chartvar = newobj.push({
        car: value,
        miles: xx[value]
      });
      var data = newobj;
      data = data.sort(function(a, b) {
        return b.miles - a.miles;
      });
    }
  }
  x.domain(data.map(function(d) {
    return d.car;
  }));
  if (period_grain == 'hour') {
    var staticMax = 13000000;
  }
  if (period_grain == 'day') {
    var staticMax = 300000000;
  }
  if (period_grain == 'month') {
    var staticMax = 2000000;
  }
  if (period_grain == 'year') {
    var staticMax = 35000000;
  }

  y.domain([0, d3.max(data, function(d) {
    return d.miles > staticMax ? d.miles : staticMax;
  })]);

  svg.append('g')
    .attr('class', 'y axis')
    .call(yAxis)
    .append('text')
    .attr('y', 6)
    .attr('dy', '.71em')
    .style('text-anchor', 'start');

  var changeHour = function() {
    var dataJoin = svg.selectAll('.bar')
      .data(data, function(d) {
        return d.car;
      });

    svg.selectAll('.y.axis')
      .call(yAxis);
    xtext = svg.append('g')
      .attr('class', 'x axis')
      .attr('transform', 'translate(-20,' + height + ')') /*move tick text so it aligns with rects*/
      .call(xAxis)
      /* possible new elements, fired first time, set non-data dependent attributes*/
    dataJoin
      .enter()
      .append('rect')
      .attr('class', 'bar')
      .attr('transform', 'translate(-20)') /*move rects closer to Y axis*/


    /* changes to existing elements (now including the newly appended elements from above) which depend on data values (d)*/
    dataJoin
      .attr('x', function(d) {
        return x(d.car);
      })
      .attr('width', x.rangeBand() * 1)
      .attr('y', function(d) {
        return y(d.miles);
      })
      .attr('height', function(d) {
        return height - y(d.miles);
      })
      .style('fill', function(d) {
        if (d.car == 'Audi' || d.car == 'Chevvy' || d.car == 'Honda' || d.car == 'Hummer') {
          return 'green';
        } else {
          return '#404040';
        }
      })
    xtext.selectAll('text')
      .attr('transform', function(d) {
        return 'translate(' + this.getBBox().height * 50 + ',' + this.getBBox().height + ')rotate(0)';
      });
    dataJoin.exit().remove();

  }
  changeHour();

  //function to allow user to click arrows to view next/previous period_grain
  // This button will increment the value
  $('.right-next').click(function(e) {
    // Stop acting like a button
    e.preventDefault();
    // Get the field name
    fieldName = $(this).attr('field');
    // Get its current value
    barchart_i = parseInt($('input[name=' + fieldName + ']').val());
    // If is not undefined
    if (!isNaN(barchart_i)) {
      // Increment
      $('input[name=' + fieldName + ']').val(barchart_i + 1);
    } else {
      // Otherwise set to 0
      $('input[name=' + fieldName + ']').val(0);
    }
    incrementHour();
  });
  // This button will decrement the value till 0
  $('.left-previous').click(function(e) {
    // Stop acting like a button
    e.preventDefault();
    // Get the field name
    fieldName = $(this).attr('field');
    // Get its current value
    barchart_i = parseInt($('input[name=' + fieldName + ']').val());
    // If it isn't undefined or if it is greater than 0
    if (!isNaN(barchart_i) && barchart_i > 0) {
      // Decrement one
      $('input[name=' + fieldName + ']').val(barchart_i - 1);
    } else {
      // Otherwise set to 0
      $('input[name=' + fieldName + ']').val(0);
    }
    incrementHour();
  });


  incrementHour = function() {
    allcars_hour = arr1[barchart_i];

    var car = allcars_hour.car;
    var allcars_dash_title = allcars_hour.allcars_title;

    var newobj = [];
    for (var allcars_hourx1 = 0; allcars_hourx1 < car.length; allcars_hourx1++) {
      var xx = car[allcars_hourx1];
      for (var value in xx) {
        var chartvar = newobj.push({
          car: value,
          miles: xx[value]
        });
      }
    }
    data = newobj;
    console.log('data is ' + data);
    data = data.sort(function(a, b) {
      return b.miles - a.miles;
    });

    x.domain(data.map(function(d) {
      return d.car;
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.miles > staticMax ? d.miles : staticMax;
    })]);
    jQuery('.barchart_title').text(allcars_dash_title);
    changeHour();
  };
})();

This is bizarre. I have researched this extensively, and cannot find anything that relates to it.

Has anybody else encountered this? If so, are there any ideas how to fix it?

prokaryote
  • 437
  • 6
  • 16

0 Answers0