更新d3.js按钮按下

时间:2018-09-29 16:05:49

标签: javascript html d3.js

我正在遵循本指南来构建d3图形,该图形可以通过按下按钮http://bl.ocks.org/d3noob/7030f35b72de721622b8更新数据。这是我的图形,其中有变量datadatax,它们用两个区域表示:

var svg = d3.select("svg"),
        margin = {top: 20, right: 20, bottom: 110, left: 40},
        margin2 = {top: 430, right: 20, bottom: 30, left: 40},
        width = +svg.attr("width") - margin.left - margin.right - 150, 
        height = +svg.attr("height") - margin.top - margin.bottom,  
        height2 = +svg.attr("height") - margin2.top - margin2.bottom;

    var parseDate = d3.timeParse("%b %Y")
    bisectDate = d3.bisector(function(d) { return d.date; }).left;;

    var x = d3.scaleTime().range([0, width]),
        x2 = d3.scaleTime().range([0, width]),
        y = d3.scaleLinear().range([height, 0]),
        y2 = d3.scaleLinear().range([height2, 0]);

    var xAxis = d3.axisBottom(x),
        xAxis2 = d3.axisBottom(x2),
        yAxis = d3.axisLeft(y);


    var area = d3.area()
        .curve(d3.curveMonotoneX)
        .x(function(d) { return x(d.date); })
        .y0(height)
        .y1(function(d) { return y(d.price); });

    var area2 = d3.area()
        .curve(d3.curveMonotoneX)
        .x(function(d) { return x2(d.date); })
        .y0(height2)
        .y1(function(d) { return y2(d.price); });

    svg.append("defs").append("clipPath")
        .attr("id", "clip")
        .append("rect")
        .attr("width", width)
        .attr("height", height);

    var g = svg.append("g")  // riferito al primo piano
    //.attr("class", "focus")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    var context = svg.append("g") // riferito al secondo piano
        .attr("class", "context")
        .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");


        var data = [{date: 'Jan 2007', price: 10409}, {date: 'Feb 2007', price:11790},{date: 'Mar 2007', price: 13161}, {date: 'Apr 2007', price: 11569}, {date: 'May 2007', price: 12380}, {date: 'Jun 2007', price: 8971}, {date: 'Jul 2007', price: 7361}, {date: 'Aug 2007', price: 7853}, {date: 'Sep 2007', price: 6968}, {date: 'Oct 2007', price: 7014}, {date: 'Nov 2007', price: 10063}, {date: 'Dec 2007', price: 7529}]

        var datax = [{date: 'Jan 2007', price: 0}, {date: 'Feb 2007', price:0},{date: 'Mar 2007', price:0}, {date: 'Apr 2007', price: 0}, {date: 'May 2007', price: 0}, {date: 'Jun 2007', price: 0}, {date: 'Jul 2007', price: 0}, {date: 'Aug 2007', price: 0}, {date: 'Sep 2007', price: 0}, {date: 'Oct 2007', price: 0}, {date: 'Nov 2007', price: 0}, {date: 'Dec 2007', price: 0}];

        data.forEach(function (d) {
            d.date = parseDate(d.date);
            d.price = d.price;
        });

        datax.forEach(function (d) {
            d.date = parseDate(d.date);
            d.price = d.price;
        });


    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, Math.max(d3.max(data, function(d) { return d.price; }), d3.max(datax, function(d) { return d.price; })) ]);

    x2.domain(x.domain());
    y2.domain(y.domain());

    g.append("path")
        .datum(data)
        .attr("class", "area")
        .attr("d", area);

    g.append("path")
        .datum(datax)
        .attr("class", "areax")
        .attr("d", area);

    g.append("g")
        .attr("class", "axis axis--x")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    g.append("g")
        .attr("class", "axis axis--y")
        .call(yAxis);


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

    focus.append("line")
        .attr("class", "x-hover-line hover-line")
        .attr("y1", 0)  // punto di inizio della linea
        .attr("y2", height); // punto di fine della linea, dicendomi quanto deve essere lunga

    focus.append("line")
        .attr("class", "y-hover-line hover-line")
        .attr("x1", width)
        .attr("x2", width);

    focus.append("circle")
        .attr("r", 0);

    focus.append("text")
        .attr("x", 15)   // mi dice la distanza dall'asse verticale
        .attr("dy", ".31em");

    context.append("path")
        .datum(data)
        .attr("class", "area")
        .attr("d", area2);

    context.append("path")
        .datum(datax)
        .attr("class", "areax")
        .attr("d", area2);

    context.append("g")
        .attr("class", "axis axis--x")
        .attr("transform", "translate(0," + height2 + ")")
        .call(xAxis2);

    context.append("g")
        .attr("class", "brush")
        .call(brush)
        .call(brush.move, x.range());

    svg.append("rect")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        .attr("class", "overlay")
        .attr("class", "zoom")
        .attr("width", width)
        .attr("height", height)
        .call(zoom)
        // .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        .on("mouseover", function() { focus.style("display", null); })
        .on("mouseout", function() { focus.style("display", "none"); })
        .on("mousemove", mousemove);

我的方法updateData()应该如何更新唯一的var data

0 个答案:

没有答案