javascript - 如何将图表放在页面中间?

标签 javascript jquery css d3.js nvd3.js

我试图将我的图表放在页面中间,但由于某些原因我发现很难。

另外,我怎样才能使工具提示悬停在蓝色条上,因为它现在显示在页面的一 Angular 。

谁能帮我把图表放在中间,让工具提示悬停在图表中的蓝色条上。

谢谢!

这是我的CSS:

.axis path, .axis line {
    fill: none;
    stroke: black;
    shape-rendering: crispEdges;
}

#title {
    font-family: sans-serif;
    font-weight: bold; 
    font-size: 14px;
    text-align: center;
    margin-top: 20px;
}

.axis text {
    font-family: sans-serif;
    font-size: 14px;
}
#tooltip {
    position: absolute;
    text-align: center;
    width: 40px;
    height: auto;
    padding: 10px;
    background-color: white;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    -webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
    -moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
    box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
    pointer-events: none;
}
#tooltip.hidden {
    display: none;
}
#tooltip p {
    margin: 0;
    font-family: sans-serif;
    font-size: 16px;
    line-height: 20px;
}

这是我的 d3.js:

<script>
var margins = {
    top: 12,
    left: 150,
    right: 24,
    bottom: 24
},
legendPanel = {
    width: 0
},
width = 800 - margins.left - margins.right - legendPanel.width,
    height = 300 - margins.top - margins.bottom,
    dataset = [{
        data: [{
            month: 'Jan Sales',
            count: 35 
        }, {
            month: 'XMAS',
            count:5
        }
         ]
    }

    ],
    series = dataset.map(function (d) {
        return d.name;
    }),
    dataset = dataset.map(function (d) {
        return d.data.map(function (o, i) {
            // Structure it so that your numeric
            // axis (the stacked amount) is y
            return {
                y: o.count,
                x: o.month
            };
        });
    }),
    stack = d3.layout.stack();

stack(dataset);

var dataset = dataset.map(function (group) {
    return group.map(function (d) {
        // Invert the x and y values, and y0 becomes x0
        return {
            x: d.y,
            y: d.x,
            x0: d.y0
        };
    });
}),
    svg = d3.select('body')
        .append('svg')
        .attr('width', width + margins.left + margins.right + legendPanel.width)
        .attr('height', height + margins.top + margins.bottom)
        .append('g')
        .attr('transform', 'translate(' + margins.left + ',' + margins.top + ')'),
    xMax = d3.max(dataset, function (group) {
        return d3.max(group, function (d) {
            return d.x + d.x0;
        });
    }),
    xScale = d3.scale.linear()
        .domain([0, xMax])
        .range([0, width]),
    months = dataset[0].map(function (d) {
        return d.y;
    }),
    _ = console.log(months),
    yScale = d3.scale.ordinal()
        .domain(months)
        .rangeRoundBands([0, height], .5),
    xAxis = d3.svg.axis()
        .scale(xScale)
        .orient('bottom'),
    yAxis = d3.svg.axis()
        .scale(yScale)
        .orient('left'),
    colours = d3.scale.category10(),
    groups = svg.selectAll('g')
        .data(dataset)
        .enter()
        .append('g')
        .style('fill', function (d, i) {
        return colours(i);
    }),
    rects = groups.selectAll('rect')
        .data(function (d) {
        return d;
    })
        .enter()
        .append('rect')
        .attr('x', function (d) {
        return xScale(d.x0);
    })
        .attr('y', function (d, i) {
        return yScale(d.y);
    })
        .attr('height', function (d) {
        return yScale.rangeBand();
    })
        .attr('width', function (d) {
        return xScale(d.x);
    })
        .on('mouseover', function (d) {
        var xPos = parseFloat(d3.select(this).attr('x')) / 2 + width / 2;
        var yPos = parseFloat(d3.select(this).attr('y')) + yScale.rangeBand() / 2;

        d3.select('#tooltip')
            .style('left', xPos + 'px')
            .style('top', yPos + 'px')
            .select('#value')
            .text(d.x);

        d3.select('#tooltip').classed('hidden', false);
    })
        .on('mouseout', function () {
        d3.select('#tooltip').classed('hidden', true);
    })

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

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

svg.append('rect')
    .attr('fill', 'yellow')
    .attr('width', 160)
    .attr('height', 30 * dataset.length)
    .attr('x', width + margins.left)
    .attr('y', 0);

series.forEach(function (s, i) {
    svg.append('text')
        .attr('fill', 'black')
        .attr('x', width + margins.left + 8)
        .attr('y', i * 24 + 24)
        .text(s);
    svg.append('rect')
        .attr('fill', colours(i))
        .attr('width', 60)
        .attr('height', 20)
        .attr('x', width + margins.left + 90)
        .attr('y', i * 24 + 6);
});

</script>

这是我的工具提示 html:

<div id="tooltip" class="hidden">
    <p>
       <span id="value">100</span>
    </p>
</div>

最佳答案

要在页面中间放置一个 block ,您可以使用非常简单的 css 设置。由于您希望图表位于中间,因此将以下内容添加到您的 css

    #tooltip {
      margin: 0 auto
    }

要将悬停功能添加到类或 ID,您可以使用以下 CSS。只需将值更改为您相应的 id 或 class

#example:hover {
background-color: blue;
}

希望这对您有所帮助!

关于javascript - 如何将图表放在页面中间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803310/

相关文章:

javascript - 无法选择和无法复制的 HTML 文本

javascript - jQuery:fadeOut() 异步行为

javascript - 获取关键代码/点击/触发更改事件

javascript - 从特定类的所有选定选项创建数组

jquery - 如何使用.load和.fadeIn更改DIV内容?

css - 如何在 2 个 float div 之间居中放置一个 div

javascript - 使用 react 双击编辑输入的文本

javascript - 如何使用一个标签选择多个具有不同名称的单选按钮?

javascript - css 和 javascript 像 iOS 照片应用程序一样滚动

css - Spring Boot 不加载 css 文件 : Request method 'GET' not supported