无法通过具有多个路径的刷图绘制d3.js焦点+上下文 [英] Having trouble drawing d3.js Focus+Context via Brushing chart with multiple paths

查看:115
本文介绍了无法通过具有多个路径的刷图绘制d3.js焦点+上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里几个星期,似乎没有弄明白如何绘制下面的图表与多个路径。
通过刷图聚焦+上下文

I have been at it for a couple of weeks and cant seem to figure out how to draw the below chart with multiple paths. Focus+Context via Brushing chart

我试图创建一个jsfiddle,但不能复制我得到的屏幕。在这一点上,我有类似于原来的图表只有一条路径,而不是区域和刷牙的工作。基本上尝试合并焦点图表和多系列线形图多图表

I have tried to create a jsfiddle but was not able to replicate the screen that i do get. At this point what i have is similar to the original chart just with one path instead of area and the brushing works. Basically trying to combine the Focus chart and Multi-Series Line Chart Multiseries chart .

但是,当我尝试添加另一个路径没有什么作品。请提出我需要为其工作的任何想法或改变。也有任何其他类似的图表(或图表exaples),我可以看看。数据可以以任何方式或形式重新排列以便工作。

However when I try to add another path nothing works. Please suggest any ideas or changes that I need to make for it to work. Also are there any other similar charts (or chart exaples) that I can look at. The data could be rearranged in any way or form for this to work.

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' ]

As per your comment, you were able to plot the three areas but had difficulty in brushing them. I have an working example here: http://jsfiddle.net/BVzyq/1/ wherein, I have added three <path> elements corresponding to the three colors in the data: ['red', 'yellow', 'green'].

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

I abstracted out the functions which could take in a color and return the appropriate d value:

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]);
        });
};

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

They can be abstracted further, but these are closest to the code you have written. These functions are used while creating the paths:

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> 与用于轴的那些。

The CSS classes seemed to suggest this form of data-join. I also added another class data to the paths which correspond to the time-series plots. This makes it easy to distinguish these <path>s from those meant for the axis.

刷函数,为所有 path.data 元素重新计算 d 属性:

Finally, in the brush function, recalculate the d attribute for all path.data elements:

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);
}

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

Note that I changed some of the values in data to make all the three colours visible.

这篇关于无法通过具有多个路径的刷图绘制d3.js焦点+上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆