Search code examples
angularjsnvd3.jsangular-nvd3

NVD3 - Time Format


I am trying to display a bar chart in angularjs using nvd3 such that x axis displays a date and y axis displays the time. I have tried the following:

 $scope.options1 = {
    chart: {
      type: 'discreteBarChart',
      height: 450,
      margin: {
        top: 20,
        right: 20,
        bottom: 50,
        left: 55
      },
      x: function(d) {
        return d.date;
      },
      y: function(d) {
        return d.hour;
      },
      showValues: true,
      duration: 500,
      xAxis: {
        axisLabel: 'Date',
        tickFormat: function(d) {
          return d3.time.format('%d-%m-%y')(new Date(d));
        }
      },
      yAxis: {
        axisLabel: 'Time of Day',
        axisLabelDistance: -10,
        tickFormat: function(d) {
          return d3.time.format("%H%p")(new Date(d));
        }
      }
    }
  };

  $scope.data1 = [{
    key: "Cumulative Return",
    values: [{
      "date": "01-Jun-16",
      "hour": "9AM"
    }, {
      "date": "04-Jun-16",
      "hour": "10AM"
    }, {
      "date": "02-Jun-16",
      "hour": "2PM"
    }, {
      "date": "03-Jun-16",
      "hour": "4PM"
    }]
  }]

My problem is that the code displays NaN instead of any values (so I suppose I'm parsing the values the wrong way but couldn't find a better solution) Any idea what might be wrong here?


Solution

  • This wasn't exactly what I was looking for but I managed to solve it by converting the hours into 0-24 integers

      x: function(d) {
            return d3.time.format('%d-%m-%y')(new Date(d.date));
          },
          y: function(d) {
            var timeParser = d3.time.format("%I%p");
            var time = new Date(timeParser.parse(d.hour));
            var hour = time.getHours();
            return hour;
          },