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

chr*_*con 10 javascript css charts nvd3.js

我有以下nvd3堆积面积图:

在此输入图像描述

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

在此输入图像描述 在此输入图像描述

我一直在研究渲染的html但是无法通过css访问边距值,即使我尝试使用firefox的控制台内联.我已经能够用这个css更改font-family和color:

#chart1
  height: 300px
  text
    fill: #1a1f22
    font-size: 0.7em
    font-family: 'Source Sans Pro', sans-serif
Run Code Online (Sandbox Code Playgroud)

但是,无论我试图将风格附加到哪个元素(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;
});
Run Code Online (Sandbox Code Playgroud)

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

Luc*_*cas 14

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

 chart.legend.margin().bottom = 50;
Run Code Online (Sandbox Code Playgroud)

或者:

 chart.legend.margin({top: 5, right: 0, bottom: 50, left: 0});
Run Code Online (Sandbox Code Playgroud)

您可以使用常规D3 tickPadding函数在刻度线和轴之间添加空格:

 chart.yAxis.tickPadding(25);
 chart.xAxis.tickPadding(25);
Run Code Online (Sandbox Code Playgroud)

就像在这个Plunker.