【问题标题】:d3.js - shifting y-axis on stacked graph chartd3.js - 在堆叠图表上移动 y 轴
【发布时间】:2012-06-28 20:45:48
【问题描述】:

我还在 Google Group https://groups.google.com/forum/?fromgroups#!topic/d3-js/DYiVeC544ws 上发布了这个问题,但看到它更喜欢在这里提出帮助问题。我只是无法将 y 轴移动到图表的左侧,以免妨碍第一列。我在 google 小组中提供了一张图片,以便您可以看到我在说什么。

创建坐标轴的代码如下:

   var MARGINS = {top: 20, right: 20, bottom: 20, left: 60}; // margins around the graph
var xRange = d3.scale.linear().range([MARGINS.left, width - MARGINS.right]), // x range function
    yRange = d3.scale.linear().range([height - MARGINS.top, MARGINS.bottom]), // y range function

    xAxis = d3.svg.axis().scale(xRange).tickSize(16).tickSubdivide(true), // x axis function
    yAxis = d3.svg.axis().scale(yRange).tickSize(10).orient("right").tickSubdivide(true); // y axis function


// create the visualization chart
var vis = d3.select("#chart")
   .append("svg")
    .attr("width", width)
    .attr("height", height + margin);


    // add in the x axis
  vis.append("svg:g") // container element
    .attr("class", "x axis") // so we can style it with CSS
    .attr("transform", "translate(0," + height + ")") // move into position
   // .call(xAxis); // add to the visualisation

  // add in the y axis
  vis.append("svg:g") // container element
    .attr("class", "y axis") // so we can style it with CSS
    .call(yAxis); // add to the visualisation

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    使用 transform attribute 移动 y 轴,就像您移动 x 轴一样。例如:

    svg.append("g")
        .attr("class", "y axis")
        .attr("transform", "translate(" + width + ",0)")
        .call(yAxis);
    

    只有在使用正确方向时才需要这样做。当轴向左时,默认定位通常很好。

    另外,我建议对边距使用以下约定:

    • 将 SVG 元素的原点设置为内部图表区域的左上角。
    • widthheight 定义为内部宽度和高度。

    例如:

    var margin = {top: 20, right: 10, bottom: 20, left: 10},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;
    
    var x = d3.scale.linear()
        .range([0, width]);
    
    var y = d3.scale.linear()
        .range([height, 0]);
    
    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    

    如果您愿意,还可以存储内部和外部尺寸,例如,

    var outerWidth = 960,
        outerHeight = 500,
        innerWidth = outerWidth - margin.left - margin.right,
        innerHeight = outerHeight - margin.top - margin.bottom;
    

    这是对约定的直观解释:

    【讨论】:

    • 感谢您的回复,我现在正在使用 .attr("transform", "translate(" + width + ",0)"),但是我现在遇到了问题我的堆积条形图在最左边的一列被截断。这样做的原因是我必须将图形向右绘制 35 px,这样它就不会与 x 轴重叠。当我尝试增加画布时,图形只会缩放到更大的尺寸。是否可以在不缩放图形的情况下增加画布大小。
    • 听起来你在某个地方有一个错误,可能忘记减去一个边距。您尝试过我描述的约定吗?
    • 经过一番琢磨之后,我想我明白了。是的,我目前正在使用您上面概述的约定。感谢您的帮助,我在 d3.js 上推荐您。这是一个很棒的图书馆。
    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多