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

时间:2017-06-06 16:28:54

标签: d3.js linegraph

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>

1 个答案:

答案 0 :(得分:1)

你可能想要一个有序的比例:

  

序数标度具有离散域,例如一组名称或   类别。 (d3v3 API documentation

这将创建一个比例,允许您的数据“在x轴上等距离间隔”。

范围可以使用.rangeRoundBands设置,而不是.range

.rangeRoundBands([intervalMin,intervalMax]):将域值映射到连续间隔,确保值为整数以避免消除锯齿效果。

.range([values]):将域值映射到一系列离散值

虽然API documentation拥有所有这些信息(并且更详细)。

以下是使用序数x标度(没有轴标签等)绘制数据的简单示例:

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>

注意:

使用条形图,每个条形图从序数刻度返回的点开始,这对条形图很好,因为每个条形图将是一定的宽度,轴将考虑到这一点:轴标签将在中间正确对齐酒吧的开头和结尾。但是,使用折线图时,线条的顶点将是每个条形图开始的位置 - 标签将位于顶点之间的中间位置。因此,您可能必须使用偏移量来沿x轴对齐刻度线:

(相关代码:.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>