javascript - 通过刷多路径图表无法绘制 d3.js Focus+Context

标签 javascript jquery d3.js

我已经用了几个星期了,但似乎无法弄清楚如何绘制下面的多路径图表。 Focus+Context via Brushing chart

我试图创建一个 jsfiddle,但无法复制我得到的屏幕。在这一点上,我所拥有的与原始图表相似,只是只有一条路径而不是区域,并且刷牙工作正常。基本上尝试结合焦点图和多系列折线图 Multiseries chart .

但是,当我尝试添加另一条路径时,没有任何效果。请提出我需要做出的任何想法或更改以使其正常工作。还有其他类似的图表(或图表示例)我可以看一下吗?可以以任何方式或形式重新排列数据以使其起作用。

Jsfiddle

<div id='dashboardChart'>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
id="svg">
</div>

path {
fill:none;
stroke:white;
stroke-width:2px;
}
.axis path, .axis line {
    fill: none;
    stroke: #CCC;
    shape-rendering: crispEdges;
}
.brush .extent {
    stroke: #fff;
    fill-opacity: .125;
    shape-rendering: crispEdges;
}
.path_green {
    stroke:green;
}
.path_red {
    stroke:red;
}
.path_yellow {
    stroke:yellow;
}

function drawChart() {
    var margin = {
        top: 5,
        right: 10,
        bottom: 100,
        left: 50
    },
    margin2 = {
        top: 200,
        right: 10,
        bottom: 20,
        left: 50
    },
    width = 1075 - margin.left - margin.right,
        height = 280 - margin.top - margin.bottom,
        height2 = 280 - margin2.top - margin2.bottom;

    var parseDate = d3.time.format("%Y-%m-%d").parse;

    var x = d3.time.scale().range([0, width]),
        x2 = d3.time.scale().range([0, width]),
        y = d3.scale.linear().range([height, 0]),
        y2 = d3.scale.linear().range([height2, 0]);

    var xAxis = d3.svg.axis().scale(x).orient("bottom"),
        xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
        yAxis = d3.svg.axis().scale(y).orient("left");

    var brush = d3.svg.brush()
        .x(x2)
        .on("brush", brush);

    var area = d3.svg.area()
        .interpolate("monotone")
        .x(function (d) {
        return x(d.date);
    })
        .y0(height)
        .y1(function (d) {
        return y(d.red);
    });

    var area2 = d3.svg.area()
        .interpolate("monotone")
        .x(function (d) {
        return x2(d.date);
    })
        .y0(height2)
        .y1(function (d) {
        return y2(d.red);
    });

    var svg = d3.select("#dashboardChart #svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom);

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

    var focus = svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    var context = svg.append("g")
        .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

    var data = [{
        "date": "2013-02-08T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 1
    }, {
        "date": "2013-02-07T05:00:00.000Z",
        "data": null,
        "red": 485,
        "yellow": 0,
        "green": 491
    }, {
        "date": "2013-02-06T05:00:00.000Z",
        "data": null,
        "red": 2884,
        "yellow": 0,
        "green": 2881
    }, {
        "date": "2013-02-05T05:00:00.000Z",
        "data": null,
        "red": 3191,
        "yellow": 0,
        "green": 3188
    }, {
        "date": "2013-02-04T05:00:00.000Z",
        "data": null,
        "red": 180,
        "yellow": 0,
        "green": 184
    }, {
        "date": "2013-02-03T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-02-02T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-02-01T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-31T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-30T05:00:00.000Z",
        "data": null,
        "red": 1,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-29T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 2
    }, {
        "date": "2013-01-28T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-27T05:00:00.000Z",
        "data": null,
        "red": 1,
        "yellow": 1,
        "green": 1
    }, {
        "date": "2013-01-26T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 1
    }, {
        "date": "2013-01-25T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-24T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-23T05:00:00.000Z",
        "data": null,
        "red": 49,
        "yellow": 0,
        "green": 45
    }, {
        "date": "2013-01-22T05:00:00.000Z",
        "data": null,
        "red": 59,
        "yellow": 0,
        "green": 64
    }, {
        "date": "2013-01-21T05:00:00.000Z",
        "data": null,
        "red": 119,
        "yellow": 1,
        "green": 125
    }, {
        "date": "2013-01-20T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 1,
        "green": 0
    }, {
        "date": "2013-01-19T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-18T05:00:00.000Z",
        "data": null,
        "red": 84,
        "yellow": 0,
        "green": 81
    }, {
        "date": "2013-01-17T05:00:00.000Z",
        "data": null,
        "red": 76,
        "yellow": 1,
        "green": 77
    }, {
        "date": "2013-01-16T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 1,
        "green": 0
    }, {
        "date": "2013-01-15T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-14T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-13T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-12T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-11T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }, {
        "date": "2013-01-10T05:00:00.000Z",
        "data": null,
        "red": 0,
        "yellow": 0,
        "green": 0
    }];

    x.domain(d3.extent(data.map(function (d) {
        return d.date;
    })));
    y.domain([0, d3.max(data.map(function (d) {
        return d.red;
    }))]);
    x2.domain(x.domain());
    y2.domain(y.domain());

    focus.append("path")
        .datum(data)
        .attr("clip-path", "url(#clip)")
        .attr("d", area)
        .attr("class", "path_red");

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

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

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

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

    context.append("g")
        .attr("class", "x brush")
        .call(brush)
        .selectAll("rect")
        .attr("y", -6)
        .attr("height", height2 + 7);

    function brush() {
        x.domain(brush.empty() ? x2.domain() : brush.extent());
        focus.select("path").attr("d", area);
        focus.select(".x.axis").call(xAxis);
    }
}
drawChart();

最佳答案

根据您的评论,您能够绘制这三个区域,但难以它们。我这里有一个工作示例:http://jsfiddle.net/BVzyq/1/其中,我添加了三个<path>数据中三种颜色对应的元素:['red', 'yellow', 'green'] .

我抽象出可以接受颜色并返回适当的 d 的函数值:

var area = function (color) {
    return d3.svg.area()
        .interpolate("monotone")
        .x(function (d) {
          return x(d.date);
        })
        .y0(height)
        .y1(function (d) {
          return y(d[color]);
        });
};

var area2 = function (color) {
    return d3.svg.area()
        .interpolate("monotone")
        .x(function (d) {
          return x2(d.date);
        })
        .y0(height2)
        .y1(function (d) {
          return y2(d[color]);
        });
};

它们可以进一步抽象,但这些最接近您编写的代码。 创建路径时使用这些函数:

focus.selectAll('path')
    .data(['red', 'yellow', 'green'])
  .enter()
    .append('path')
    .attr('clip-path', 'url(#clip)')
    .attr('d', function (col) {
      return area(col)(data);
    })
    .attr('class', function (col) {
      return "path_" + col + " data";
    });

// ...

context.selectAll('path')
    .data(['red', 'yellow', 'green'])
  .enter()
    .append('path')
    .attr('d', function (col) {
      return area2(col)(data);
    })
    .attr('class', function (col) {
      return "path_" + col;
    });

CSS 类似乎暗示了这种形式的数据连接。我还添加了另一个类 data对应于时间序列图的路径。这样就很容易区分这些 <path> s 来自轴的那些。

最后在刷函数中,重新计算d所有属性 path.data元素:

function brush() {
    x.domain(brush.empty() ? x2.domain() : brush.extent());
    focus.selectAll("path.data").attr("d", function (col) { 
      return area(col)(data); 
    });
    focus.select(".x.axis").call(xAxis);
}

请注意,我更改了 data 中的一些值使所有三种颜色可见。

关于javascript - 通过刷多路径图表无法绘制 d3.js Focus+Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777518/

相关文章:

javascript - 无法获取上下文元素文本

javascript - 如果付款成功或不成功,如何在重定向页面顶部显示横幅

javascript - 处理从ajax post下载的文件

javascript - D3日期格式抛出 'd.getFullYear is not a function'

javascript - 在 ng-options 上使用自定义排序过滤器时出现奇怪的排序行为

javascript - VueJS 3 - 模板/v-for 中的 substr/chop ?

jquery - 向左浮动,然后在换行时向右浮动

javascript - D3 : adding a line when clicking on a circle in scatter plot

javascript - D3 : d3. 行为.拖动

javascript - 如何在 javascript 中切换两个元素而不删除