多折线图中的图例-d3 [英] Legend in Multi line chart - d3

查看:74
本文介绍了多折线图中的图例-d3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将图例添加到多线图中时,我的结果是:

While adding a legend to my multi line graph my result is:

通过,失败等...可见一半. 代码:

The passed ,failed etc... are half visible. code:

   var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 1090 - margin.left - margin.right,
height = 339 - margin.top - margin.bottom;

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

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

  var color = d3.scale.category10();

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

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

  var line = d3.svg.line()  
.interpolate("linear")
.x(function(d) { return x(d.days); })
.y(function(d) { return y(d.testRuns); });

 var svg = d3.select("#application_status_graph").append("svg")
.attr("width", width + margin.left + margin.right + '0%')
.attr("height", height + margin.top + margin.bottom)
 .append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

 d3.json("js/lineChartData.json", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) {return key !== "days"; }));
   data.forEach(function(d) {
    d.days = parseInt(d.days);
   });

   var status = color.domain().map(function(name){
return{
    name: name,
    values: data.map(function(d){
        return {days: d.days, testRuns: +d[name]};
    }) 
}
   });
    x.domain(d3.extent(data, function(d) { return d.days; }));

y.domain([0, d3.max(status, function(c) { return d3.max(c.values, function(v) {           return v.testRuns; }); })]);
   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("Number of Test Runs");

var legend = svg.selectAll('.city')
    .data(status)
    .enter().append('g')
    .attr('class', 'legend');

legend.append("path")
  .attr("class", "line")
  .attr("d", function(d) { return line(d.values); })
  .style("stroke", function(d) { return color(d.name); });

legend.append('rect')
    .attr('x', width - 20)
    .attr('y', function(d, i){ return i *  20;})
    .attr('width', 10)
    .attr('height', 10)
    .style('fill', function(d) { 
      return color(d.name);
    });

legend.append('text')
    .attr('x', width - 8)
    .attr('y', function(d, i){ return (i *  20) + 9;})
    .text(function(d){ return d.name; });

 });

我在代码周围玩了很多.但我无法实现以下目标:

I have played around the width all around the code. But i couldn't acheive the following:

有什么建议吗?谢谢.

推荐答案

要实现此目的,您需要做两件事.首先,限制图形本身的x范围,以使其不会延伸得太远.最简单的方法是调整x标尺的range:

To achieve this, you need to do two things. First, restrict the x range of the graph itself so it doesn't extend too far. The easiest way to do this is by adjusting the range of your x scale:

var x = d3.scale.linear()
          .range([0, width-20]);

第二,您需要调整图例的坐标:

Second, you need to adjust the coordinates of the legend:

legend.append('rect').attr('x', width - 30)

,其他元素也类似.一般而言,您可能希望移动整个图例组,而不是分别移动每个元素,即,您将像这样移动组:

and similarly for the other elements. On a general note, you may prefer moving the entire legend group instead of each element individually, i.e. you would move the group like so:

var legend = svg.append("g")
                .attr("transform", "translate(" + (width - 30) + ",20)")
                .selectAll('.city')
                .data(status)
                ...

然后,图例元素的坐标在该组中是局部的,移动整个图例仅需要调整g元素的坐标.

Then the coordinates of the legend elements are local to this group and moving the entire legend only requires adjusting the coordinates of the g element.

这篇关于多折线图中的图例-d3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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