【问题标题】:having trouble plotting a D3 line graph where X-Axis is not a date无法绘制 X 轴不是日期的 D3 折线图
【发布时间】:2017-06-06 16:28:53
【问题描述】:

我的数据如下所示:

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

标签是我的 x 轴值,虽然它们代表一个财政年度,但它们本质上只是一个标签,我希望在 x 轴上等距分布

这是我目前拥有的代码...注意我有 2 个类似的数据集,一个用于员工人数,另一个用于 FTE

我认为这与这条线有关,我也不确定要更改什么:

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>

【问题讨论】:

    标签: d3.js linegraph


    【解决方案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");
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"&gt;&lt;/script&gt;

    注意:

    对于条形图,每个条形图都从序数刻度返回的点开始,这对于条形图来说很好,因为每个条形图都有一定的宽度,轴将说明这一点:轴标签将在中间正确对齐栏的开始和结束。但是,对于折线图,折线的顶点将是每个条形的开始位置 - 并且标签将位于顶点之间的中间。因此,您可能必须使用偏移量来沿 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;
    }
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 感谢安德鲁!我怎样才能让它成为一个折线图?然后我还有另一个数据集在同一张图上绘制为折线图
    • 对不起,我的错,将sn-p更改为问题中指定的折线图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多