简体   繁体   English

d3 追加简单区域

[英]d3 append simple area

I want to append simple area to my chart with end points of area values as constants.我想将简单的面积附加到我的图表中,并将面积值的端点作为常量。

percentArea = d3.svg.area()
      .interpolate('linear')
      .x0(0)
      .x1(w)
      .y0(h/4)
      .y1(h);

I want to append this area directly and give it a color fill.我想直接附加这个区域并给它一个颜色填充。 Thanks for help.感谢帮助。 I am having specially hard time trying to get the area fill.我在尝试填充区域时特别困难。 The chart shows a line over the area but no fill when I tried.该图表在该区域上显示一条线,但在我尝试时没有填充。

Edit: Trying to make the question more clear and specific : The issue I face is only when I try to call my function without passing data.编辑:试图使问题更加清晰和具体:我面临的问题仅是当我尝试在不传递数据的情况下调用我的函数时。 The area fill works as expected when I call percentArea(data).当我调用percentArea(data) 时,区域填充按预期工作。 However I am unable to understand why I must pass data when endpoints of my area are constant.但是,当我的区域的端点不变时,我无法理解为什么我必须传递数据。

You need to ensure that the path you have rendered is being filled.您需要确保您渲染的路径正在被填充。 There are two ways you can do this:有两种方法可以做到这一点:

Set a Style设置样式

path.style("fill", "steelblue");

Set a class and leverage CSS设置一个类并利用 CSS

path.attr("class", "filled-area");

// In CSS
.filled-area {
    fill: steelblue;
}

Here's a quick example:这是一个快速示例:

Area Chart with a Stroke:带笔划的面积图:

带笔划的面积图

Area Chart with no fill没有填充的面积图

As you can see this defaults to black如您所见,默认为黑色

没有填充的面积图

Area Chart with correct fill正确填充的面积图

正确填充的面积图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM