在nvd3图表中更改边距和填充

时间:2015-04-19 21:56:58

标签: javascript css charts nvd3.js

我有以下nvd3堆积面积图:

enter image description here

我想在数字/日期和图表之间添加边距,以及在顶部和图表中添加图例。 (请看图片,我用红线标记了位置:

enter image description here enter image description here

我一直在研究渲染的html但是无法通过css访问边距值,即使我尝试使用firefox的控制台内联。 我已经能够用这个css改变字体系列和颜色:

#chart1
  height: 300px
  text
    fill: #1a1f22
    font-size: 0.7em
    font-family: 'Source Sans Pro', sans-serif

但是,无论我试图附加一种风格的元素(text,g,svg,...),保证金方面都没有变化。

以下是图表的javascript代码:

nv.addGraph(function() {
    var histcatexplong = [
        <?= $array ?>

    ];


    var colors = d3.scale.category20();
    var keyColor = function(d, i) {return colors(d.key)};

    var chart;
chart = nv.models.stackedAreaChart()
.useInteractiveGuideline(true)
.showControls(false)
.x(function(d) { return d[0] })
.y(function(d) { return d[1] })
.color(keyColor)

.transitionDuration(300);




chart.xAxis
.tickFormat(function(d) { return d3.time.format('%e.%m.%y')(new Date(d)) });



chart.yAxis
.tickFormat(d3.format(',.2f'));

d3.select('#chart1')
.datum(histcatexplong)
.transition().duration(1000)
.call(chart)


.each('start', function() {
setTimeout(function() {
d3.selectAll('#chart1 *').each(function() {

if(this.__transition__)
this.__transition__.duration = 1;
})
}, 0)
});

nv.utils.windowResize(chart.update);


return chart;
});

我一直在阅读nvd3的所有示例和文档,但仍然找不到上面说的操作方法。有人知道一种方法吗?

2 个答案:

答案 0 :(得分:14)

关于您的保证金问题,您可以通过调用以下方式更改图例周围的边距:

 chart.legend.margin().bottom = 50;

或者:

 chart.legend.margin({top: 5, right: 0, bottom: 50, left: 0});

您可以使用常规D3 tickPadding function

在刻度线和轴之间添加空格
 chart.yAxis.tickPadding(25);
 chart.xAxis.tickPadding(25);

就像在this Plunker中一样。

答案 1 :(得分:1)

.nvd3 .nv-axis.nv-x path.domain {
  stroke-opacity: 1;
  stroke: red;
}

CSS可能是要走的路。 (默认情况下,NVD3使x轴不可见,因此您首先要将其不透明度设置为1.)

this Plunker中的示例。