0

所以我试图为我的折线图(下面的代码)创建一条路径。数据存储在 JSON 对象中,数据点只是一个点数组(日期和值)。数据已全部解析,因此格式正确,但是当我将路径附加到 svg(最后几行代码)并尝试使用 .datum 输入数据时,javascript 控制台显示“错误:解析问题: 并表明 d="MNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,..."。我不知道这个 NaNLNaN 是从哪里来的。

<script>
    var margin = {top: 20, right: 20, bottom: 30, left: 50},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    var parseDate = d3.time.format("%x").parse;

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

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

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

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

    var line = d3.svg.line()
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(d.close); });

    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 + ")");

    var datastring = "{ \"title\":\"Stock Price of Apple (AAPL)\", \"x\":{ \"label\":\"Date\", \"unit\":\"Day\" }, \"y\":{ \"label\":\"Price\", \"unit\":\"$\" }, \"points\":[ [ \"3/13/13\", \"428.45\" ], [ \"3/12/13\", \"435.6\" ], [ \"3/11/13\", \"429.75\" ], [ \"3/8/13\", \"429.8\" ], [ \"3/7/13\", \"424.5\" ], [ \"3/6/13\", \"434.51\" ], [ \"3/5/13\", \"421.48\" ], [ \"3/4/13\", \"427.8\" ], [ \"3/1/13\", \"438\" ], [ \"2/28/13\", \"444.05\" ], [ \"2/27/13\", \"448.43\" ], [ \"2/26/13\", \"443.82\" ], [ \"2/25/13\", \"453.85\" ], [ \"2/22/13\", \"449.25\" ], [ \"2/21/13\", \"446\" ], [ \"2/20/13\", \"457.69\" ], [ \"2/19/13\", \"461.1\" ], [ \"2/15/13\", \"468.85\" ], [ \"2/14/13\", \"464.52\" ], [ \"2/13/13\", \"467.21\" ], [ \"2/12/13\", \"479.51\" ], [ \"2/11/13\", \"476.5\" ], [ \"2/8/13\", \"474\" ], [ \"2/7/13\", \"463.25\" ], [ \"2/6/13\", \"456.47\" ], [ \"2/5/13\", \"444.05\" ], [ \"2/4/13\", \"453.91\" ], [ \"2/1/13\", \"459.11\" ], [ \"1/31/13\", \"456.98\" ], [ \"1/30/13\", \"457\" ], [ \"1/29/13\", \"458.5\" ], [ \"1/28/13\", \"437.83\" ], [ \"1/25/13\", \"451.69\" ], [ \"1/24/13\", \"460\" ], [ \"1/23/13\", \"508.81\" ], [ \"1/22/13\", \"504.56\" ], [ \"1/18/13\", \"498.52\" ], [ \"1/17/13\", \"510.31\" ], [ \"1/16/13\", \"494.64\" ], [ \"1/15/13\", \"498.3\" ], [ \"1/14/13\", \"502.68\" ], [ \"1/11/13\", \"521\" ], [ \"1/10/13\", \"528.55\" ], [ \"1/9/13\", \"522.5\" ], [ \"1/8/13\", \"529.21\" ], [ \"1/7/13\", \"522\" ], [ \"1/4/13\", \"536.97\" ], [ \"1/3/13\", \"547.88\" ], [ \"1/2/13\", \"553.82\" ], [ \"12/31/12\", \"510.53\" ], [ \"12/28/12\", \"510.29\" ], [ \"12/27/12\", \"513.54\" ], [ \"12/26/12\", \"519\" ], [ \"12/24/12\", \"520.35\" ], [ \"12/21/12\", \"512.47\" ], [ \"12/20/12\", \"530\" ], [ \"12/19/12\", \"531.47\" ], [ \"12/18/12\", \"525\" ], [ \"12/17/12\", \"508.93\" ], [ \"12/14/12\", \"514.75\" ], [ \"12/13/12\", \"531.15\" ], [ \"12/12/12\", \"547.77\" ], [ \"12/11/12\", \"539.77\" ], [ \"12/10/12\", \"525\" ], [ \"12/7/12\", \"553.4\" ], [ \"12/6/12\", \"528.94\" ], [ \"12/5/12\", \"568.91\" ], [ \"12/4/12\", \"581.8\" ], [ \"12/3/12\", \"593.65\" ], [ \"11/30/12\", \"586.79\" ], [ \"11/29/12\", \"590.22\" ], [ \"11/28/12\", \"577.27\" ], [ \"11/27/12\", \"589.55\" ], [ \"11/26/12\", \"575.9\" ], [ \"11/23/12\", \"567.17\" ], [ \"11/21/12\", \"564.25\" ], [ \"11/20/12\", \"571.91\" ], [ \"11/19/12\", \"540.71\" ], [ \"11/16/12\", \"525.2\" ], [ \"11/15/12\", \"537.53\" ], [ \"11/14/12\", \"545.5\" ], [ \"11/13/12\", \"538.91\" ], [ \"11/12/12\", \"554.15\" ], [ \"11/9/12\", \"540.42\" ], [ \"11/8/12\", \"560.63\" ], [ \"11/7/12\", \"573.84\" ], [ \"11/6/12\", \"590.23\" ], [ \"11/5/12\", \"583.52\" ], [ \"11/2/12\", \"595.89\" ], [ \"11/1/12\", \"598.22\" ], [ \"10/31/12\", \"594.88\" ], [ \"10/26/12\", \"609.43\" ], [ \"10/25/12\", \"620\" ], [ \"10/24/12\", \"621.44\" ], [ \"10/23/12\", \"631\" ], [ \"10/22/12\", \"612.42\" ], [ \"10/19/12\", \"631.05\" ], [ \"10/18/12\", \"639.59\" ], [ \"10/17/12\", \"648.87\" ], [ \"10/16/12\", \"635.37\" ], [ \"10/15/12\", \"632.35\" ], [ \"10/12/12\", \"629.56\" ], [ \"10/11/12\", \"646.5\" ], [ \"10/10/12\", \"639.74\" ], [ \"10/9/12\", \"638.65\" ], [ \"10/8/12\", \"646.88\" ], [ \"10/5/12\", \"665.2\" ], [ \"10/4/12\", \"671.25\" ], [ \"10/3/12\", \"664.86\" ], [ \"10/2/12\", \"661.81\" ], [ \"10/1/12\", \"671.16\" ], [ \"9/28/12\", \"678.75\" ], [ \"9/27/12\", \"664.29\" ], [ \"9/26/12\", \"668.74\" ], [ \"9/25/12\", \"688.26\" ], [ \"9/24/12\", \"686.86\" ], [ \"9/21/12\", \"702.41\" ], [ \"9/20/12\", \"699.16\" ], [ \"9/19/12\", \"700.26\" ], [ \"9/18/12\", \"699.88\" ], [ \"9/17/12\", \"699.35\" ], [ \"9/14/12\", \"689.96\" ], [ \"9/13/12\", \"677.37\" ], [ \"9/12/12\", \"666.85\" ], [ \"9/11/12\", \"665.11\" ], [ \"9/10/12\", \"680.45\" ], [ \"9/7/12\", \"678.05\" ], [ \"9/6/12\", \"673.17\" ], [ \"9/5/12\", \"675.57\" ], [ \"9/4/12\", \"665.76\" ], [ \"8/31/12\", \"667.25\" ], [ \"8/30/12\", \"670.64\" ], [ \"8/29/12\", \"675.25\" ], [ \"8/28/12\", \"674.98\" ], [ \"8/27/12\", \"679.99\" ], [ \"8/24/12\", \"659.51\" ], [ \"8/23/12\", \"666.11\" ], [ \"8/22/12\", \"654.42\" ], [ \"8/21/12\", \"670.82\" ], [ \"8/20/12\", \"650.01\" ], [ \"8/17/12\", \"640\" ], [ \"8/16/12\", \"631.21\" ], [ \"8/15/12\", \"631.3\" ], [ \"8/14/12\", \"631.87\" ], [ \"8/13/12\", \"623.39\" ], [ \"8/10/12\", \"618.71\" ], [ \"8/9/12\", \"617.85\" ], [ \"8/8/12\", \"619.39\" ], [ \"8/7/12\", \"622.77\" ], [ \"8/6/12\", \"617.29\" ], [ \"8/3/12\", \"613.63\" ], [ \"8/2/12\", \"602.84\" ], [ \"8/1/12\", \"615.91\" ], [ \"7/31/12\", \"603.23\" ], [ \"7/30/12\", \"590.92\" ], [ \"7/27/12\", \"575.01\" ], [ \"7/26/12\", \"579.76\" ], [ \"7/25/12\", \"574.46\" ], [ \"7/24/12\", \"607.38\" ], [ \"7/23/12\", \"594.4\" ], [ \"7/20/12\", \"613.03\" ], [ \"7/19/12\", \"611.28\" ], [ \"7/18/12\", \"606.59\" ], [ \"7/17/12\", \"610.79\" ], [ \"7/16/12\", \"605.12\" ], [ \"7/13/12\", \"602.95\" ], [ \"7/12/12\", \"600.24\" ], [ \"7/11/12\", \"606.12\" ], [ \"7/10/12\", \"617.97\" ], [ \"7/9/12\", \"605.3\" ], [ \"7/6/12\", \"607.09\" ], [ \"7/5/12\", \"600.56\" ], [ \"7/3/12\", \"594.88\" ], [ \"7/2/12\", \"584.73\" ], [ \"6/29/12\", \"578\" ], [ \"6/28/12\", \"571.67\" ], [ \"6/27/12\", \"575\" ], [ \"6/26/12\", \"571.33\" ], [ \"6/25/12\", \"577.3\" ], [ \"6/22/12\", \"579.04\" ], [ \"6/21/12\", \"585.44\" ], [ \"6/20/12\", \"588.21\" ], [ \"6/19/12\", \"583.4\" ], [ \"6/18/12\", \"570.96\" ], [ \"6/15/12\", \"571\" ], [ \"6/14/12\", \"571.24\" ], [ \"6/13/12\", \"574.52\" ], [ \"6/12/12\", \"574.46\" ], [ \"6/11/12\", \"587.72\" ], [ \"6/8/12\", \"571.6\" ], [ \"6/7/12\", \"577.29\" ], [ \"6/6/12\", \"567.77\" ], [ \"6/5/12\", \"561.27\" ], [ \"6/4/12\", \"561.5\" ], [ \"6/1/12\", \"569.16\" ], [ \"5/31/12\", \"580.74\" ], [ \"5/30/12\", \"569.2\" ], [ \"5/29/12\", \"570.9\" ], [ \"5/25/12\", \"564.59\" ], [ \"5/24/12\", \"575.87\" ], [ \"5/23/12\", \"557.5\" ], [ \"5/22/12\", \"569.55\" ], [ \"5/21/12\", \"534.5\" ], [ \"5/18/12\", \"533.96\" ], [ \"5/17/12\", \"545.31\" ], [ \"5/16/12\", \"554.05\" ], [ \"5/15/12\", \"561.45\" ], [ \"5/14/12\", \"562.57\" ], [ \"5/11/12\", \"565\" ], [ \"5/10/12\", \"574.58\" ], [ \"5/9/12\", \"563.7\" ], [ \"5/8/12\", \"569.58\" ], [ \"5/7/12\", \"561.5\" ], [ \"5/4/12\", \"577.08\" ], [ \"5/3/12\", \"590.5\" ], [ \"5/2/12\", \"580.24\" ], [ \"5/1/12\", \"584.9\" ], [ \"4/30/12\", \"597.8\" ], [ \"4/27/12\", \"605.07\" ], [ \"4/26/12\", \"614.27\" ], [ \"4/25/12\", \"615.64\" ], [ \"4/24/12\", \"562.61\" ], [ \"4/23/12\", \"570.61\" ], [ \"4/20/12\", \"591.38\" ], [ \"4/19/12\", \"600.22\" ], [ \"4/18/12\", \"613.72\" ], [ \"4/17/12\", \"578.94\" ], [ \"4/16/12\", \"610.06\" ], [ \"4/13/12\", \"624.11\" ], [ \"4/12/12\", \"625\" ], [ \"4/11/12\", \"636.2\" ], [ \"4/10/12\", \"639.93\" ], [ \"4/9/12\", \"626.13\" ], [ \"4/5/12\", \"626.98\" ], [ \"4/4/12\", \"624.35\" ], [ \"4/3/12\", \"627.3\" ], [ \"4/2/12\", \"601.83\" ], [ \"3/30/12\", \"608.77\" ], [ \"3/29/12\", \"612.78\" ], [ \"3/28/12\", \"618.38\" ], [ \"3/27/12\", \"606.18\" ], [ \"3/26/12\", \"599.79\" ], [ \"3/23/12\", \"600.49\" ], [ \"3/22/12\", \"597.78\" ], [ \"3/21/12\", \"602.74\" ], [ \"3/20/12\", \"599.51\" ], [ \"3/19/12\", \"598.37\" ], [ \"3/16/12\", \"584.72\" ], [ \"3/15/12\", \"599.61\" ], [ \"3/14/12\", \"578.05\" ], [ \"3/13/12\", \"557.54\" ], [ \"3/12/12\", \"548.98\" ], [ \"3/9/12\", \"544.21\" ], [ \"3/8/12\", \"534.69\" ], [ \"3/7/12\", \"536.8\" ], [ \"3/6/12\", \"523.66\" ], [ \"3/5/12\", \"545.42\" ], [ \"3/2/12\", \"544.24\" ], [ \"3/1/12\", \"548.17\" ], [ \"2/29/12\", \"541.56\" ], [ \"2/28/12\", \"527.96\" ], [ \"2/27/12\", \"521.31\" ], [ \"2/24/12\", \"519.67\" ], [ \"2/23/12\", \"515.08\" ], [ \"2/22/12\", \"513.08\" ], [ \"2/21/12\", \"506.88\" ], [ \"2/17/12\", \"503.11\" ], [ \"2/16/12\", \"491.5\" ], [ \"2/15/12\", \"514.26\" ], [ \"2/14/12\", \"504.66\" ], [ \"2/13/12\", \"499.53\" ], [ \"2/10/12\", \"490.96\" ], [ \"2/9/12\", \"480.76\" ], [ \"2/8/12\", \"470.5\" ], [ \"2/7/12\", \"465.25\" ], [ \"2/6/12\", \"458.38\" ], [ \"2/3/12\", \"457.3\" ], [ \"2/2/12\", \"455.9\" ], [ \"2/1/12\", \"458.41\" ], [ \"1/31/12\", \"455.59\" ], [ \"1/30/12\", \"445.71\" ], [ \"1/27/12\", \"444.34\" ], [ \"1/26/12\", \"448.36\" ], [ \"1/25/12\", \"454.44\" ], [ \"1/24/12\", \"425.1\" ], [ \"1/23/12\", \"422.67\" ], [ \"1/20/12\", \"427.49\" ], [ \"1/19/12\", \"430.15\" ], [ \"1/18/12\", \"426.96\" ], [ \"1/17/12\", \"424.2\" ], [ \"1/13/12\", \"419.7\" ], [ \"1/12/12\", \"422.28\" ], [ \"1/11/12\", \"422.68\" ], [ \"1/10/12\", \"425.91\" ], [ \"1/9/12\", \"425.5\" ], [ \"1/6/12\", \"419.77\" ], [ \"1/5/12\", \"414.95\" ], [ \"1/4/12\", \"410\" ], [ \"1/3/12\", \"409.4\" ], [ \"12/30/11\", \"403.51\" ], [ \"12/29/11\", \"403.4\" ], [ \"12/28/11\", \"406.89\" ], [ \"12/27/11\", \"403.1\" ], [ \"12/23/11\", \"399.69\" ], [ \"12/22/11\", \"397\" ], [ \"12/21/11\", \"396.69\" ], [ \"12/20/11\", \"387.76\" ], [ \"12/19/11\", \"382.47\" ], [ \"12/16/11\", \"380.36\" ], [ \"12/15/11\", \"383.33\" ], [ \"12/14/11\", \"386.7\" ], [ \"12/13/11\", \"393\" ], [ \"12/12/11\", \"391.68\" ], [ \"12/9/11\", \"392.85\" ], [ \"12/8/11\", \"391.45\" ], [ \"12/7/11\", \"389.93\" ], [ \"12/6/11\", \"392.51\" ], [ \"12/5/11\", \"393.49\" ], [ \"12/2/11\", \"389.83\" ], [ \"12/1/11\", \"382.54\" ], [ \"11/30/11\", \"381.29\" ], [ \"11/29/11\", \"375.84\" ], [ \"11/28/11\", \"372.35\" ], [ \"11/25/11\", \"368.42\" ], [ \"11/23/11\", \"374.51\" ], [ \"11/22/11\", \"371.02\" ], [ \"11/21/11\", \"370.4\" ], [ \"11/18/11\", \"378.92\" ], [ \"11/17/11\", \"383.98\" ], [ \"11/16/11\", \"389.25\" ], [ \"11/15/11\", \"380.8\" ], [ \"11/14/11\", \"383.52\" ], [ \"11/11/11\", \"386.61\" ], [ \"11/10/11\", \"397.03\" ], [ \"11/9/11\", \"396.97\" ], [ \"11/8/11\", \"402.21\" ], [ \"11/7/11\", \"399.91\" ], [ \"11/4/11\", \"402.03\" ], [ \"11/3/11\", \"399.07\" ], [ \"11/2/11\", \"400.09\" ], [ \"11/1/11\", \"397.41\" ], [ \"10/31/11\", \"402.42\" ], [ \"10/28/11\", \"403\" ], [ \"10/27/11\", \"407.56\" ], [ \"10/26/11\", \"401.76\" ], [ \"10/25/11\", \"405.03\" ], [ \"10/24/11\", \"396.18\" ], [ \"10/21/11\", \"398.1\" ], [ \"10/20/11\", \"400\" ], [ \"10/19/11\", \"401.35\" ], [ \"10/18/11\", \"421.76\" ], [ \"10/17/11\", \"421.74\" ], [ \"10/14/11\", \"416.83\" ], [ \"10/13/11\", \"404.98\" ], [ \"10/12/11\", \"407.34\" ], [ \"10/11/11\", \"392.57\" ], [ \"10/10/11\", \"379.09\" ], [ \"10/7/11\", \"375.78\" ], [ \"10/6/11\", \"373.33\" ], [ \"10/5/11\", \"367.86\" ], [ \"10/4/11\", \"374.57\" ], [ \"10/3/11\", \"380.37\" ], [ \"9/30/11\", \"387.12\" ], [ \"9/29/11\", \"401.92\" ], [ \"9/28/11\", \"400.19\" ], [ \"9/27/11\", \"408.73\" ], [ \"9/26/11\", \"399.86\" ], [ \"9/23/11\", \"400.28\" ], [ \"9/22/11\", \"401.03\" ], [ \"9/21/11\", \"419.64\" ], [ \"9/20/11\", \"415.25\" ], [ \"9/19/11\", \"397\" ], [ \"9/16/11\", \"395.54\" ], [ \"9/15/11\", \"391.43\" ], [ \"9/14/11\", \"387.02\" ], [ \"9/13/11\", \"382.14\" ], [ \"9/12/11\", \"373\" ], [ \"9/9/11\", \"383.93\" ], [ \"9/8/11\", \"382.4\" ], [ \"9/7/11\", \"385.56\" ], [ \"9/6/11\", \"367.37\" ], [ \"9/2/11\", \"374.74\" ], [ \"9/1/11\", \"385.82\" ], [ \"8/31/11\", \"390.57\" ], [ \"8/30/11\", \"388.25\" ], [ \"8/29/11\", \"388.18\" ], [ \"8/26/11\", \"371.17\" ], [ \"8/25/11\", \"365.08\" ], [ \"8/24/11\", \"373.47\" ], [ \"8/23/11\", \"360.3\" ], [ \"8/22/11\", \"364.51\" ], [ \"8/19/11\", \"362.17\" ], [ \"8/18/11\", \"370.84\" ], [ \"8/17/11\", \"382.31\" ], [ \"8/16/11\", \"381.52\" ], [ \"8/15/11\", \"379.63\" ], [ \"8/12/11\", \"378.07\" ], [ \"8/11/11\", \"370.52\" ], [ \"8/10/11\", \"371.15\" ], [ \"8/9/11\", \"361.3\" ], [ \"8/8/11\", \"361.69\" ], [ \"8/5/11\", \"380.44\" ], [ \"8/4/11\", \"389.41\" ], [ \"8/3/11\", \"390.98\" ], [ \"8/2/11\", \"397.65\" ], [ \"8/1/11\", \"397.78\" ], [ \"7/29/11\", \"387.64\" ], [ \"7/28/11\", \"391.62\" ], [ \"7/27/11\", \"400.59\" ], [ \"7/26/11\", \"400\" ], [ \"7/25/11\", \"390.35\" ], [ \"7/22/11\", \"388.32\" ], [ \"7/21/11\", \"386.95\" ], [ \"7/20/11\", \"396.12\" ], [ \"7/19/11\", \"378\" ], [ \"7/18/11\", \"365.43\" ], [ \"7/15/11\", \"361.17\" ], [ \"7/14/11\", \"361.01\" ], [ \"7/13/11\", \"358.33\" ], [ \"7/12/11\", \"353.53\" ], [ \"7/11/11\", \"356.34\" ], [ \"7/8/11\", \"353.34\" ], [ \"7/7/11\", \"354.67\" ], [ \"7/6/11\", \"348.95\" ], [ \"7/5/11\", \"343\" ], [ \"7/1/11\", \"335.95\" ], [ \"6/30/11\", \"334.7\" ], [ \"6/29/11\", \"336.04\" ], [ \"6/28/11\", \"333.65\" ], [ \"6/27/11\", \"327.59\" ], [ \"6/24/11\", \"331.37\" ], [ \"6/23/11\", \"318.94\" ], [ \"6/22/11\", \"325.16\" ], [ \"6/21/11\", \"316.68\" ], [ \"6/20/11\", \"317.36\" ], [ \"6/17/11\", \"328.99\" ], [ \"6/16/11\", \"326.9\" ], [ \"6/15/11\", \"329.75\" ], [ \"6/14/11\", \"330\" ], [ \"6/13/11\", \"326.6\" ], [ \"6/10/11\", \"330.55\" ], [ \"6/9/11\", \"333.25\" ], [ \"6/8/11\", \"331.78\" ], [ \"6/7/11\", \"338.17\" ], [ \"6/6/11\", \"345.7\" ], [ \"6/3/11\", \"343.18\" ], [ \"6/2/11\", \"346.5\" ], [ \"6/1/11\", \"348.87\" ], [ \"5/31/11\", \"341.1\" ], [ \"5/27/11\", \"334.8\" ], [ \"5/26/11\", \"335.97\" ], [ \"5/25/11\", \"336.78\" ], [ \"5/24/11\", \"335.5\" ], [ \"5/23/11\", \"329.97\" ], [ \"5/20/11\", \"339.56\" ], [ \"5/19/11\", \"342.08\" ], [ \"5/18/11\", \"336.47\" ], [ \"5/17/11\", \"332\" ], [ \"5/16/11\", \"339.2\" ], [ \"5/13/11\", \"345.66\" ], [ \"5/12/11\", \"346.12\" ], [ \"5/11/11\", \"349.02\" ], [ \"5/10/11\", \"348.89\" ], [ \"5/9/11\", \"347.86\" ], [ \"5/6/11\", \"349.69\" ], [ \"5/5/11\", \"348.4\" ], [ \"5/4/11\", \"348.26\" ], [ \"5/3/11\", \"347.99\" ], [ \"5/2/11\", \"349.74\" ], [ \"4/29/11\", \"346.78\" ], [ \"4/28/11\", \"346.19\" ], [ \"4/27/11\", \"352.24\" ], [ \"4/26/11\", \"353.62\" ], [ \"4/25/11\", \"350.34\" ], [ \"4/21/11\", \"355\" ], [ \"4/20/11\", \"343.51\" ], [ \"4/19/11\", \"333.1\" ], [ \"4/18/11\", \"326.1\" ], [ \"4/15/11\", \"333.3\" ], [ \"4/14/11\", \"334.8\" ], [ \"4/13/11\", \"335.02\" ], [ \"4/12/11\", \"330.49\" ], [ \"4/11/11\", \"334.06\" ], [ \"4/8/11\", \"339.92\" ], [ \"4/7/11\", \"338.1\" ], [ \"4/6/11\", \"341.22\" ], [ \"4/5/11\", \"336.99\" ], [ \"4/4/11\", \"344.31\" ], [ \"4/1/11\", \"351.11\" ], [ \"3/31/11\", \"346.36\" ], [ \"3/30/11\", \"350.64\" ], [ \"3/29/11\", \"347.66\" ], [ \"3/28/11\", \"353.15\" ], [ \"3/25/11\", \"348.07\" ], [ \"3/24/11\", \"341.85\" ], [ \"3/23/11\", \"339.28\" ], [ \"3/22/11\", \"342.56\" ], [ \"3/21/11\", \"335.99\" ], [ \"3/18/11\", \"337.13\" ], [ \"3/17/11\", \"336.83\" ], [ \"3/16/11\", \"342\" ], [ \"3/15/11\", \"342.1\" ], [ \"3/14/11\", \"353.18\" ] ] } ";

    var data = JSON.parse(datastring);

    var points = data["points"];

    points.forEach(function(d) {
      d[0] = parseDate(d[0]);
      d[1] = +d[1];
    });

    points.reverse();

    x.domain(d3.extent(points, function(d) { return d[0]; }));
    y.domain(d3.extent(points, function(d) { return d[1]; }));

    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", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Price ($)");

    svg.append("path")
        .datum(points)
        .attr("class", "line")
        .attr("d", line);

    </script>
4

1 回答 1

0
var line = d3.svg.line()
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(d.close); });

应该:

var line = d3.svg.line()
        .x(function(d) { return x(d[0]); })
        .y(function(d) { return y(d[1]); });
于 2013-03-30T00:23:23.253 回答