score:1

Accepted answer

If you are using a time scale, it expects you to pass in dates. This line:

.x(function(d, i) { return x(i); })

is passing in array index values (ie 0, 1, 2 ...)

For this to work you are going to need to change the data you pass to valueline to be an array of objects, with each object having an x date and a y value.

Here's your entire code snippet refactor with proper date parsing:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@4.0.0" data-semver="3.5.17" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
  </head>

  <body>
    <script>
      var data =  {
    "Elektrotehnički fakultet Osijek": { "2008/09":1539, "2009/10":1678, "2010/11":1873, "2011/12":2231, "2012/13":2192, "2013/14":1841},
    "Fakultet elektrotehnike i računarstva Zagreb": { "2008/09":4795, "2009/10":4538, "2010/11":4320, "2011/12":4913, "2012/13":4634, "2013/14":3290},
    "Fakultet elektrotehnike strojarstva i brodogradnje Split": { "2008/09":2480, "2009/10":2685, "2010/11":2790, "2011/12":2769, "2012/13":2649, "2013/14":2633}
    }

var margin = {top: 50, bottom: 70, left:70, right: 30};
var width = 700 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;

var svg = d3.select('body')
  .append('svg')
  .attr('width', width)
  .attr('height', height);

var timeFormat2 = d3.time.format('%Y');

// this will end up being an array
// of objects with two properties, date and value
var properData = [];
for (key in data["Elektrotehnički fakultet Osijek"]){
  var value = data["Elektrotehnički fakultet Osijek"][key],
      date = timeFormat2.parse(key.split("/")[0]);
  properData.push({
    value: value,
    date: date
  })
}

var x = d3.time.scale()
    .domain(d3.extent(properData, function(d){ return d.date }))
    .range([0, width]);

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

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(function(d, i) { return Object.keys(data["Elektrotehnički fakultet Osijek"])[i].replace(/_/g, ' '); });

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(10);

var valueline = d3.svg.line()
    .x(function(d, i) { return x(d.date); })
    .y(function(d) { return y(d.value); });    
   
var linechart = svg.append("path")
    .attr("class", "line")
    .attr("d", valueline(properData))
    .style("stroke", "blue")
    .style("stroke-width", "2")
    .style("fill", "none")
    </script>
  </body>

</html>


Related Query

More Query from same tag