Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript tr(“宽度”,30) .attr(“高度”,20) .attr(“填充”、“白色”) .样式(“不透明度”,0.5); 工具提示 .append(“文本”) .attr(“x”,15) .attr(“dy”、“1.0em”) .style(“文本锚定”、“中间”) .attr(“字体大小”,“12px”) .attr(“字体重量”、“粗体”); } render(){ 返回( ); } } render(,document.getElementById('root'));_Javascript_Reactjs_D3.js_Components_Web Component - Fatal编程技术网

Javascript tr(“宽度”,30) .attr(“高度”,20) .attr(“填充”、“白色”) .样式(“不透明度”,0.5); 工具提示 .append(“文本”) .attr(“x”,15) .attr(“dy”、“1.0em”) .style(“文本锚定”、“中间”) .attr(“字体大小”,“12px”) .attr(“字体重量”、“粗体”); } render(){ 返回( ); } } render(,document.getElementById('root'));

Javascript tr(“宽度”,30) .attr(“高度”,20) .attr(“填充”、“白色”) .样式(“不透明度”,0.5); 工具提示 .append(“文本”) .attr(“x”,15) .attr(“dy”、“1.0em”) .style(“文本锚定”、“中间”) .attr(“字体大小”,“12px”) .attr(“字体重量”、“粗体”); } render(){ 返回( ); } } render(,document.getElementById('root'));,javascript,reactjs,d3.js,components,web-component,Javascript,Reactjs,D3.js,Components,Web Component,看起来您使用的API来自d3版本3(当前版本为5)。因此,我建议卸载d3依赖项并安装d3@3. 您也不需要安装像“d3形状”这样的模块,因为它们是在版本5中引入的 下面是代码的工作示例:Yes@Yaroslav您是对的。糟糕的是,我使用的是D3版本的3.5.17,它自动更新到5.7.0,我没有注意到。这就是为什么会有这个问题。谢谢你的帮助。事实上,我在3-4小时前就想出来了,但仍然如此。非常感谢:) import React, { Component } from "react"; import

看起来您使用的API来自d3版本3(当前版本为5)。因此,我建议卸载d3依赖项并安装d3@3. 您也不需要安装像“d3形状”这样的模块,因为它们是在版本5中引入的


下面是代码的工作示例:

Yes@Yaroslav您是对的。糟糕的是,我使用的是D3版本的3.5.17,它自动更新到5.7.0,我没有注意到。这就是为什么会有这个问题。谢谢你的帮助。事实上,我在3-4小时前就想出来了,但仍然如此。非常感谢:)
import React, { Component } from "react";
import * as d3 from "d3";
// import d3 from "d3-shape";
// import d3 from "d3";
// import * as stack from 'd3-shape';
import "d3-tip";
import { render } from 'react-dom';
import barData from "./JSON/barData.jsx";

class App extends Component {
    componentDidMount() {
        console.log("componentDidMount", this.props.id);
        console.log(barData);
        var margin = { top: 20, right: 160, bottom: 35, left: 0 };

        var width = 500 - margin.left - margin.right,
            height = 162 - margin.top - margin.bottom;

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

        /* Data in strings like it would be if imported from a csv */

        var data = barData;

        //var parse = d3.time.format("%Y").parse;

        // Transpose the data into layers
        var dataset = d3.layout.stack()(
            ["view"].map(function(fruit) {
                return data.map(function(d) {
                    return { x: d.year, y: +d[fruit] };
                });
            })
        );

        // Set x, y and colors
        var x = d3.scale
            .ordinal()
            .domain(
                dataset[0].map(function(d) {
                    return d.x;
                })
            )
            .rangeRoundBands([10, width - 10], 0.02);

        var y = d3.scale
            .linear()
            .domain([
                0,
                d3.max(dataset, function(d) {
                    return d3.max(d, function(d) {
                        return d.y0 + d.y;
                    });
                })
            ])
            .range([height, 0]);

        var colors = ["#39d9F3"];

        // Define and draw axes
        var yAxis = d3.svg
            .axis()
            .scale(y)
            .orient("left")
            .ticks(5)
            .tickSize(-width, 0, 0)
            .tickFormat(function(d) {
                return d;
            });

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

        svg
            .append("g")
            .attr("class", "y axis")
            .call(yAxis);

        svg
            .append("g")
            .attr("class", "x axis legend_texts_two")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

        // Create groups for each series, rects for each segment
        var groups = svg
            .selectAll("g.cost")
            .data(dataset)
            .enter()
            .append("g")
            .attr("class", "cost")
            .style("fill", function(d, i) {
                return colors[i];
            });

        var rect = groups
            .selectAll("rect")
            .data(function(d) {
                return d;
            })
            .enter()
            .append("rect")
            .attr("x", function(d) {
                return x(d.x);
            })
            .attr("y", function(d) {
                return y(d.y0 + d.y);
            })
            .attr("height", function(d) {
                return y(d.y0) - y(d.y0 + d.y);
            })
            .attr("width", "20px")
            .on("mouseover", function() {
                tooltip.style("display", null);
            })
            .on("mouseout", function() {
                tooltip.style("display", "none");
            })
            .on("mousemove", function(d) {
                var xPosition = d3.mouse(this)[0] - 15;
                var yPosition = d3.mouse(this)[1] - 25;
                tooltip.attr(
                    "transform",
                    "translate(" + xPosition + "," + yPosition + ")"
                );
                tooltip.select("text").text(d.y);
            })
            .style("fill", function(d, i) {
                return colors[i];
            });

        // Draw legend
        /*var legend = svg
            .selectAll(".legend")
            .data(colors)
            .enter()
            .append("g")
            .attr("class", "legend")
            .attr("transform", function(d, i) {
                return "translate(30," + i * 19 + ")";
            });

        legend
            .append("rect")
            .attr("x", width - 18)
            .attr("width", 18)
            .attr("height", 18)
            .style("fill", function(d, i) {
                return colors.slice().reverse()[i];
            });

        legend
            .append("text")
            .attr("x", width + 5)
            .attr("y", 9)
            .attr("dy", ".35em")
            .style("text-anchor", "start")
            .text(function(d, i) {
                switch (i) {
                    case 0:
                        return "View";
                    case 0:
                        return "View";
                    case 2:
                        return "Click";
                }
            });*/

        // Prep the tooltip bits, initial display is hidden
        var tooltip = svg
            .append("g")
            .attr("class", "tooltip")
            .style("display", "none");

        tooltip
            .append("rect")
            .attr("width", 30)
            .attr("height", 20)
            .attr("fill", "white")
            .style("opacity", 0.5);

        tooltip
            .append("text")
            .attr("x", 15)
            .attr("dy", "1.0em")
            .style("text-anchor", "middle")
            .attr("font-size", "12px")
            .attr("font-weight", "bold");
    }

    render() {
        return (
            <div id="dashboard_bar_graph_101" style={{ textAlign: "center" }} />
        );
    }
}

render(<App />, document.getElementById('root'));