Search code examples
d3.jslinegraph

having trouble plotting a D3 line graph where X-Axis is not a date


I have data that looks like this:

LABEL,VALUE
2011/2012,6267.783164
2012/2013,6385.832768
2013/2014,6334.048158
2014/2015,6359.295548
2015/2016,6380.218338
2016/2017,6519.040832
2017/2018,6370.332847

the Label is to be my x-axis values and though they represent a fiscal year they are esentially just a label that i want to be spaced equi-distint on a xaxis

here is the code I currently have ...note I have 2 similar data sets one for headcount and the other for FTE

I think it has to do with this line I am just unsure what to change it too:

var x = d3.time.scale().range([0, width]);

<!DOCTYPE html>
<meta charset="utf-8">

<head>
<title>Self Identification Percentages</title>
<style>
.dataDisplay {
  font-size:1em;
}


body { font: 12px Arial;}

path { 
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}



</style>

 <script src="./jquery.min.js" type="text/javascript"></script>
 <script src="./jquery-ui.min.js" type="text/javascript"></script>
 <script src="./jquery.corner.js"></script>
</head>

<body>

<script src="./d3/d3.v3.min.js"></script>



<div id="graph"></div>

<script>






// Set the dimensions of the canvas / graph
var margin = {top: 0, right: 20, bottom: 20, left: 60},
    width = 400 - margin.left - margin.right,
    height = 350 - margin.top - margin.bottom;

// Parse the date / time
///var parseDate = d3.time.format("%Y-%m-%d").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

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

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.LABEL); })
    .y(function(d) { return y(d.VALUE); });

// Adds the svg canvas
var svg = d3.select("body")
    .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
    .append("g")
        .attr("transform", 
              "translate(" + margin.left + "," + margin.top + ")");

// Get the data


 var dataFile = ".\\temp\\hc.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.LABEL = d.LABEL;
        d.VALUE = d.VALUE;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.LABEL; }));
    y.domain([0, d3.max(data, function(d) { return d.VALUE; })]);


    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#7CA2C8");




    // Add the X Axis
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);




  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", -60)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
          .style("font-size", "12px")
          .style("font-weight", "900")
          .style("font", "sans-serif")
      .text("Count");


 });




 dataFile = ".\\temp\\fte.csv";
 d3.csv(dataFile, function(error, data) {
    data.forEach(function(d) {
        d.LABEL = d.LABEL;
        d.VALUE = d.VALUE;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.LABEL; }));
    y.domain([0, d3.max(data, function(d) { return d.VALUE; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data))
        .attr("stroke", "#ECAD6F");
 });



</script>
<div class=Legend>
<b>
 <font size=2 color=#7CA2C8>HeadCount<font><!--input size 2 type="checkbox" id="HeadCount" name="HeadCount" value="HC" onChange="this.form.submit()"-->
 <font size=2 color=#7CC8A2>FTE<font><!--input size 2  type="checkbox" id="FTE" name="FTE" value="FTE"  onChange="this.form.submit()"-->
</b>
</div>



</body>
</html>

Solution

  • You probably want an ordinal scale:

    Ordinal scales have a discrete domain, such as a set of names or categories. (d3v3 API documentation)

    This will create a scale that will allow your data to "be spaced equi-distint on a xaxis".

    The range can be set with .rangeRoundBands, as opposed to .range:

    .rangeRoundBands([intervalMin,intervalMax]): Will map domain values to a continuous interval, ensuring that values are integers to avoid anti-aliasing effects.

    .range([values]): Will map domain values to a range of discrete values

    The API documentation has all this information though (and in greater detail).

    Here's a simple example of your data drawn with an ordinal x scale (without axis labels, etc):

    var data = [
    {year:"2011/2012",value:6267.783164},
    {year:"2012/2013",value:6385.832768},
    {year:"2013/2014",value:6334.048158},
    {year:"2014/2015",value:6359.295548},
    {year:"2015/2016",value:6380.218338},
    {year:"2016/2017",value:6519.040832},
    {year:"2017/2018",value:6370.332847}
    ];
    
    
    var width = 500;
    var height = 400;
    
    
    var x = d3.scale.ordinal()
      .rangeRoundBands([0,width])
      .domain(data.map(function(d) { return d.year }));
      
    var y = d3.scale.linear()
      .range([height,0])
      .domain([d3.min(data,function(d) { return d.value; }), d3.max(data, function(d) { return d.value; })
      ]);
      
    var svg = d3.select("body")
      .append("svg")
      .attr("width",width)
      .attr("height",height);
      
    var line = d3.svg.line()
      .x(function(d) { return x(d.year); })
      .y(function(d) { return y(d.value); });
      
    svg.append("path")
      .datum(data)
      .attr("d",line)
      .attr("fill","none")
      .attr("stroke","steelblue");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

    Note:

    With a bar graph each bar starts at a point returned by the ordinal scale, this is fine for bar graphs as each bar will be a certain width and the axis will account for this: The axis label will be aligned properly halfway between the beginning and end of the bar. However, with a line graph, the vertices of the line will be where each bar would have begun - and the labels will be half way between the vertices. So you may have to use an offset to align the ticks as you want along the x axis:

    (relevant code: .x(function(d) { return x(d.year) + x.rangeBand()/2; }) )

    var data = [
    {year:"2011/2012",value:6267.783164},
    {year:"2012/2013",value:6385.832768},
    {year:"2013/2014",value:6334.048158},
    {year:"2014/2015",value:6359.295548},
    {year:"2015/2016",value:6380.218338},
    {year:"2016/2017",value:6519.040832},
    {year:"2017/2018",value:6370.332847}
    ];
    
    
    var width = 500;
    var height = 200;
    var margin = {top:10,left:10,bottom:30,right:10}
    
    var x = d3.scale.ordinal()
      .rangeRoundBands([margin.left,width-margin.right])
      .domain(data.map(function(d) { return d.year }));
      
    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");
      
    var y = d3.scale.linear()
      .range([height-margin.bottom,margin.top])
      .domain([d3.min(data,function(d) { return d.value; }), d3.max(data, function(d) { return d.value; })
      ]);
      
    var svg = d3.select("body")
      .append("svg")
      .attr("width",width)
      .attr("height",height);
      
    var line = d3.svg.line()
      .x(function(d) { return x(d.year) + x.rangeBand()/2; })
      .y(function(d) { return y(d.value); });
      
    svg.append("path")
      .datum(data)
      .attr("d",line)
      .attr("fill","none")
      .attr("stroke","steelblue");
      
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + (height - margin.bottom)  + ")")
        .call(xAxis);
    .axis text {
      font: 10px sans-serif;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>