d3在使用解析方法时显示错误tp解析日期。 “TypeError:t.substring不是函数”

时间:2016-02-15 08:56:29

标签: javascript d3.js

是什么导致我这个错误..我是在错误地解析日期还是其他什么?实际上我正在尝试定期更新数据并将其传递给生成d3图的javascript函数....是我的更新方法错写的???请帮帮我....

<script type="text/javascript">
    //<![CDATA[
    $.noConflict();
    jQuery(document)
        .ready(
            function($) {
                var x=(${rawEventsMB.jsonString});
                var data1=JSON.stringify(x);
                var data = JSON.parse(data1);

                function InitChart(data) {
                    try {
                        //alert("data is:: "+data);
                        var parseDate = d3.time
                                    .format("%Y-%m-%d %H:%M:%S").parse;
                        bisectDate = d3.bisector(function(d) {
                            return d.time;
                        }).left;

                        data.forEach(function(d) {
                            d.time = parseDate(d.time);
                            d.count = +d.count;
                        });

                        var vis = d3.select("#visualisation"), WIDTH = 1000, HEIGHT = 300, MARGINS = {
                            top : 20,
                            right : 20,
                            bottom : 20,
                            left : 50
                        },

                        xScale = d3.time.scale().domain(
                                    [ d3.min(data, function(d) {
                                        return d.time;
                                    }), d3.max(data, function(d) {
                                        return d.time;
                                    }) ]).range(
                                    [ MARGINS.left,
                                            WIDTH - MARGINS.right ]),

                            yScale = d3.scale.linear()
                                    .range([ HEIGHT - MARGINS.top,MARGINS.bottom ])
                                    .domain(d3.extent(data, function(d) {return d.count;})),

                            xAxis = d3.svg.axis().scale(xScale)
                                    .orient("bottom")
                                    .ticks(10)
                                    .tickFormat(d3.time.format("%d %b %X")),
                            yAxis = d3.svg.axis().scale(yScale)
                                    .orient("left");
                                vis.append("svg:g")
                                    .attr("class", "x axis")
                                    .attr("transform","translate(0,"+ (HEIGHT - MARGINS.bottom)+ ")")
                                    .call(xAxis)
                                    .selectAll("text").style("text-anchor","end")
                                    .attr("dx","-.6em")
                                    .attr("dy","-.55em")
                                    .attr("transform","rotate(-90)"),
                                vis.append("text")
                                    .attr("transform","translate("+ (WIDTH / 2)+ " ,"+ (HEIGHT + MARGINS.bottom*4)+ ")")
                                    .style("text-anchor","middle")
                                    .style("fill", "red")
                                    .style("font-weight", "bold")
                                    .text("Time");

                            vis.append("svg:g")
                                    .attr("class", "y axis")
                                    .attr("transform","translate("+ (MARGINS.left)+ ",0)")
                                    .call(yAxis), 
                            vis.append("text")
                                    .attr("transform","rotate(-90 "+ MARGINS.left* 1.4 + " "+ HEIGHT / 5 + ")")
                                    .style("text-anchor", "middle")
                                    .style("fill", "red")
                                    .text("Number of events ")
                                    .style("font-weight", "bold");

                            var focus = vis.append("g").style(
                                    "display", "none");

                            focus.append("circle").attr("class", "y")
                                    .style("fill", "blue").style(
                                            "stroke", "blue").attr("r",
                                            4);

                            /* focus.append("text").attr("class","y").style("text-anchor","middle")
                            .style("fill", "red").style("font-weight", "bold"); */

                            focus.append("foreignObject")
                            .attr("width", 180)
                            .attr("height", 500).style("font", "bold 11px 'Helvetica Neue'").style("fill", "red").attr("class", "tooltip");

                            // append the rectangle to capture mouse              
                            vis.append("rect").attr("width", WIDTH)
                                    .attr("height", HEIGHT).style(
                                            "fill", "none").style(
                                            "pointer-events", "all")
                                    .on("mouseover", function() {
                                        focus.style("display", null);
                                    }).on("mouseout", function() {
                                        focus.style("display", "none");
                                    }).on("mousemove", mousemove);

                            var lineGen = d3.svg.line().x(function(d) {
                                return xScale(d.time);
                            }).y(function(d) {
                                return yScale(d.count);
                            }).interpolate("interpolation");

                            vis.append('svg:path').attr('d',
                                    lineGen(data)).attr('stroke',
                                    'green').attr('stroke-width', 2)
                                    .attr('fill', 'none');
                            } catch (ex) {
                                alert(ex);
                            }
                            function mousemove() {
                                var formatTime = d3.time.format("%e %b %X");
                                var x0 = xScale.invert(d3.mouse(this)[0]), i = bisectDate(
                                        data, x0, 1), d0 = data[i - 1], d1 = data[i], d2 = x0
                                        - d0.time > d1.time - x0 ? d1 : d0;
                                focus.select("circle.y").attr(
                                        "transform",
                                        "translate(" + xScale(d2.time)
                                                + "," + yScale(d2.count)
                                                + ")"),
                                /* focus.select("text")
                                        .attr("transform","translate(" + xScale(d2.time)+ "," + yScale(d2.count)+ ")"); */
                                focus.select("foreignObject").attr("transform","translate(" + xScale(d2.time)+ "," + yScale(d2.count)+ ")")
                                .html("time:"+formatTime(d2.time)+", <br/> " + "count:"+d2.count);

                            }
                            InitChart(data);
                            var myVar=setTimeout(function() { InitChart(data); }, 1000);
                        }

                    });
    //]]>
</script>`

0 个答案:

没有答案