D3.js向右移动图表区域

D3.js向右移动图表区域,d3.js,D3.js,这是一个愚蠢的问题,不知怎的,我无法通过谷歌找到解决办法 我的代码: //Create canvas var svg = d3.select("svg"), margin = {top: 20, right: 0, bottom: 20, left: 20}, padding = {top: 0, right: 0, bottom: 0, left: 0}, width = +svg.attr("width") - margin.left - margin.right,

这是一个愚蠢的问题,不知怎的,我无法通过谷歌找到解决办法

我的代码:

//Create canvas
var svg = d3.select("svg"),
    margin = {top: 20, right: 0, bottom: 20, left: 20},
    padding = {top: 0, right: 0, bottom: 0, left: 0},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        .attr("transform", "translate(" + padding.left + "," + padding.top + ")");

//Parse date
var parseDate = d3.timeParse("%Y-%m-%d");

//Set the ranges
var x = d3.scaleTime()
    .range([0, width]);
var y = d3.scaleLinear()
    .range([height, 0]);

// Define the line
var valueLine = d3.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(+d.value); });

d3.json("api.php", function(error, data) {
  if (error) throw error;
  data.forEach(e => {
    e.date = parseDate(e.date);
    e.value = +e.close;
    e.stockName = e.stock_name;
    e.stockSymbol = e.stock_symbol;
  });

  //Create nest variable
  var nest = d3.nest()
      .key(function(d){
        return d.stockSymbol;
      })
      .entries(data);
  console.log(nest);
  // Scale the range of the data
  x.domain(d3.extent(data, function(d) { return d.date; }));
  //y.domain([0, d3.max(data, function(d) { return d.value; })]);

  // Set up the x axis
  var xaxis = svg.append("g")
       .attr("transform", "translate(0," + height + ")")
       .attr("class", "x axis")
       .call(d3.axisBottom(x));

  //Create dropdown
  var dropDown = d3.select("#dropDown")
  dropDown
    .append("select")
    .selectAll("option")
      .data(nest)
      .enter()
      .append("option")
      .attr("value", function(d){ return d.key; })
      .text(function(d){ return d.key; })

  // Function to create the initial graph
    var initialGraph = function(stock){

        // Filter the data to include only fruit of interest
        var selectStock = nest.filter(function(d){
                return d.key == stock;
              })

        var selectStockGroups = svg.selectAll(".stockGroups")
            .data(selectStock, function(d){
              return d ? d.key : this.key;
            })
            .enter()
            .append("g")
            .attr("class", "stockGroups")
            .each(function(d){
                y.domain([0, d3.max(data, function(d) { return d.value; })])
            });

        var initialPath = selectStockGroups.selectAll(".line")
            .datum(function(d) { return d.values; })
            .enter()
            .append("path")

        initialPath
            .attr("d", function(d){
                return valueLine(d.values)
            })
            .attr("class", "line")

          // Add the Y Axis
      var yaxis = svg.append("g")
          .attr("class", "y axis")
            .call(d3.axisRight(y)
              .ticks(5)
              .tickSizeInner(0)
              .tickPadding(6)
              .tickSize(0, 0));

          // Add a label to the y axis
          svg.append("text")
                .attr("transform", "rotate(-90)")
                .attr("y", 0 - 60)
                .attr("x", 0 - (height / 2))
                .attr("dy", "1em")
                .style("text-anchor", "middle")
                .text("Price")
                .attr("class", "y axis label");
    }

  // Create initial graph
    initialGraph("1301.T")

    // Update the data
    var updateGraph = function(stock){

        // Filter the data to include only fruit of interest
        var selectStock = nest.filter(function(d){
                return d.key == stock;
              })

        // Select all of the grouped elements and update the data
        var selectStockGroups = svg.selectAll(".stockGroups")
            .datum(selectStock)
            .each(function(d){
                y.domain([0, d3.max(data, function(d) { return d.value; })])
            });

            // Select all the lines and transition to new positions
            selectStockGroups.selectAll("path.line")
               .data(function(d) { return d.values; },
                    function(d){ return d.key; })
               .transition()
                  .duration(1000)
                  .attr("d", function(d){
                    return valueLine(d.values)
                  })

        // Update the Y-axis
            d3.select(".y")
                    .transition()
                    .duration(1500)
                    .call(d3.axisRight(y)
                      .ticks(5)
                      .tickSizeInner(0)
                      .tickPadding(6)
                      .tickSize(0, 0));
    }
  // Run update function when dropdown selection changes
  dropDown.on('change', function(){

    // Find which fruit was selected from the dropdown
    var selectedStock = d3.select(this)
            .select("select")
            .property("value")

        // Run update function with the selected fruit
        updateGraph(selectedStock)
  });
});
我正在尝试从api的数据导入创建标准折线图

我故意使用d3.axisRight来显示屏幕截图,但如果使用d3.axisLeft,则轴比例不会显示在画布上。我一直在摆弄我的边距来向右移动y轴,但还没有找到一种方法

此外,由于某些原因,我的行没有打印,我的控制台上也没有任何错误

提前谢谢


我已经为svg将代码分为2:1)个维度;2) 从给定维度创建svg。我还删除了g变量

    //Set dimensions and margins for the graph
    var margin = {top: 20, right: 20, bottom: 100, left: 70},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    //Create canvas
    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 + ")");

将图表元素添加到应用了边距的
g
(但删除仅为零值的填充应用),而不是
svg
(从外观上看,目前您不使用
g
)。这需要做的一个更改是,您需要将x比例的范围设置为
[0,宽度边距。左边距。右]
(与y比例类似),因为您的比例不会延伸到整个svg。你的建议有点含糊不清,但我遵循链接,将画布的创建分为两部分:设置维度和创建画布。最初我是同时做这一切的。我不必改变我的x刻度。你知道为什么我的线条不在画布上打印吗@安德烈雷德