D3格式化问题

时间:2014-11-10 21:43:51

标签: javascript css d3.js

我遇到了d3图形表示的格式问题。 X轴是以年为单位输入的,我认为它更容易被视为任何其他数字(而不是dateFormat ...%Y) 输出是自动的(?)添加逗号为千分之一 - 我的假设。 有没有办法保持这个4挖掘它是什么?

(这是我第一次涉足HTML,CSS,JS和d3--感谢您的同情/评论) 感谢。

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

    body { font: 12px Arial;}

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

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

    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>

    <script>

    var margin = {top: 30, right: 40, bottom: 30, left: 50},
        width = 600 - margin.left - margin.right,
        height = 270 - margin.top - margin.bottom;


    var  x = d3.scale.linear().range([0, width]);
    var y0 = d3.scale.linear().range([height, 0]);
    var y1 = d3.scale.linear().range([height, 0]);

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

    var yAxisLeft = d3.svg.axis().scale(y0)
        .orient("left").ticks(5);

    var yAxisRight = d3.svg.axis().scale(y1)
        .orient("right").ticks(5); 

    var valueline = d3.svg.line()
        .x(function(d) { return x(d.year); })
        .y(function(d) { return y0(d.freq); });

    var valueline2 = d3.svg.line()
        .x(function(d) { return x(d.year); })
        .y(function(d) { return y1(d.fn); });

    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
    d3.csv("duallines2.csv", function(error, data) {
        data.forEach(function(d) {
            d.year = +(d.year);
            d.freq = +d.freq;
            d.fn = +d.fn;
        });

        // Scale the range of the data
        x.domain(d3.extent(data, function(d) { return d.year; }));
        y0.domain([0, d3.max(data, function(d) {
            return Math.max(d.freq); })]); 
        y1.domain([0, d3.max(data, function(d) { 
            return Math.max(d.fn); })]);

        svg.append("path")        // Add the valueline path.
            .attr("d", valueline(data));

        svg.append("path")        // Add the valueline2 path.
            .style("stroke", "red")
            .attr("d", valueline2(data));

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

        svg.append("g")
            .attr("class", "y axis")
            .style("fill", "steelblue")
            .call(yAxisLeft);   

        svg.append("g")             
            .attr("class", "y axis")    
            .attr("transform", "translate(" + width + " ,0)")   
            .style("fill", "red")       
            .call(yAxisRight);

    });

    </script>
    </body>

数据文件如下所示:

“duallines2.csv”

年,频率,FN 1950,58.13,3.41 1951,53.98,4.55 1952,67.00,6.78 1953,89.70,7.85 1954,99.00,8.92 1955,130.28,9.92 1956,166.70,10.13 1957,234.98,12.23 1958,345.44,13.45 1959,443.34,16.04 1960,543.70,18.03 1961,580.13,21.02 1962,605.23,22.34 1963,622.77,20.15 1964,626.20,21.26 1965,628.44,31.04 1966,636.23,35.04 1967,633.68,41.02 1968,624.31,43.05 1969,629.32,46.03 1970,618.63,51.03 1971,599.55,53.42 1973,609.86,57.82 1974,617.62,59.01 1975,614.48,56.03 1976,606.98,58.01

1 个答案:

答案 0 :(得分:0)

您可以定义自己的format - 在这种情况下,将每个xAxis刻度标签设为整数 - 然后告诉xAxis使用format:< / p>

var format = d3.format("d");
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(5)
    .tickFormat(format); // <----- tell the xAxis to use your custom formatter

这是您将获得的更新输出:

graph with proper x-axis labels

请参阅格式化数字here的文档。