json 应用画笔功能时不显示旋转的x轴标签

xu3bshqb  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(62)

我使用D3创建一个面积图,在旋转的x轴上使用画笔功能,旋转Angular 为45度,因为它们是重叠的,但它不起作用。最初显示图形时,x轴标签将旋转。当应用画笔到图表时,画笔是可用的,但x轴标签消失,不再显示,即使我双击重新初始化图表。

<script>
    var margin = { top: 10, right: 30, bottom: 30, left: 60 },
      width = 999 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;

    var svg = d3
      .select("#input_viz")
      .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 data = <?php echo $jsonInput; ?>;

    var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S");

    data.forEach(function (d) {
      d.date = parseDate(d[0]);
      d.value = +d[1];
    });

    var customTimeFormat = d3.timeFormat("%Y-%m-%d %H:%M:%S");

    var x = d3.scaleTime()
      .domain(d3.extent(data, function (d) { return d.date; }))
      .range([0, width]);
    var xAxis = svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x).tickFormat(customTimeFormat))
      .selectAll("text") // Select all the text elements for the x-axis ticks
      .style("text-anchor", "end") // Set the text anchor to end
      .attr("dx", "-1em") // Adjust the x position to move the labels left
      .attr("dy", "-0.5em") // Adjust the y position to move the labels up
      .attr("transform", "rotate(-45)"); // Rotate the labels by -45 degrees

    var y = d3.scaleLinear()
      .domain([0, d3.max(data, function (d) { return d.value; })])
      .range([height, 0]);
    var yAxis = svg.append("g")
      .call(d3.axisLeft(y));

    var clip = svg.append("defs").append("svg:clipPath")
      .attr("id", "clip")
      .append("svg:rect")
      .attr("width", width)
      .attr("height", height)
      .attr("x", 0)
      .attr("y", 0);

    var brush = d3.brushX()
      .extent([[0, 0], [width, height]])
      .on("end", updateChart);

    var area = svg.append('g')
      .attr("clip-path", "url(#clip)");

    var areaGenerator = d3.area()
      .x(function (d) { return x(d.date); })
      .y0(y(0))
      .y1(function (d) { return y(d.value); });

    area.append("path")
      .datum(data)
      .attr("class", "myArea") // I add the class myArea to be able to modify it later on.
      .attr("fill", "#009901")
      .attr("fill-opacity", .3)
      .attr("stroke", "#006601")
      .attr("stroke-width", 1)
      .attr("d", areaGenerator);

    area.append("g")
      .attr("class", "brush")
      .call(brush);

    var idleTimeout;

    function idled() {
      idleTimeout = null;
    }

    function updateChart() {
      // What are the selected boundaries?
      extent = d3.event.selection;

      if (!extent) {
        if (!idleTimeout) return idleTimeout = setTimeout(idled, 350);
        x.domain(d3.extent(data, function (d) { return d.date; }));
      } else {
        x.domain([x.invert(extent[0]), x.invert(extent[1])]);
        area.select(".brush").call(brush.move, null);
      }

      xAxis.transition().duration(1000).call(d3.axisBottom(x).tickFormat(customTimeFormat))
        .selectAll("text") // Select all the text elements for the x-axis ticks
        .style("text-anchor", "end") // Set the text anchor to end
        .attr("dx", "-1em") // Adjust the x position to move the labels left
        .attr("dy", "-0.5em") // Adjust the y position to move the labels up
        .attr("transform", "rotate(-45)"); // Rotate the labels by -45 degrees
      area
        .select('.myArea')
        .transition()
        .duration(1000)
        .attr("d", areaGenerator);
    }

    svg.on("dblclick", function () {
      x.domain(d3.extent(data, function (d) { return d.date; }));
      xAxis.transition().call(d3.axisBottom(x).tickFormat(customTimeFormat))
        .selectAll("text") // Select all the text elements for the x-axis ticks
        .style("text-anchor", "end") // Set the text anchor to end
        .attr("dx", "-1em") // Adjust the x position to move the labels left
        .attr("dy", "-0.5em") // Adjust the y position to move the labels up
        .attr("transform", "rotate(-45)"); // Rotate the labels by -45 degrees
      area
        .select('.myArea')
        .transition()
        .attr("d", areaGenerator);
    });

  </script>

字符串

7lrncoxx

7lrncoxx1#

给你https://codepen.io/lary-Test/pen/RwqvqpV
唯一的问题是xAxis变量值不是<g>标记。您试图从文本选择更新。

var xAxis = svg.append("g")
      .attr('class','xAxis')
      .attr("transform", "translate(0," + height + ")"); 
 // here now xAxis is the original <g> containing the whole axis rendering
 xAxis.call(d3.axisBottom(x).tickFormat(customTimeFormat))
      .selectAll("text") // Select all the text elements for the x-axis ticks
      .style("text-anchor", "end") // Set the text anchor to end
      .attr("dx", "-1em") // Adjust the x position to move the labels left
      .attr("dy", "-0.5em") // Adjust the y position to mofve the labels up
      .attr("transform", "rotate(-45)"); // Rotate the labels by -45 degrees

字符串
注意我使用的是d3 7.8.5,所以我必须更改

function updateChart({selection}) {
  extent = selection;
  ...


因为我不知道你用的是哪个d3版本。
希望这对你有帮助。

相关问题