C3.js fill area between curves

你。 提交于 2019-12-24 19:09:03

问题


i have chart like this

I need to fill 3 area between dashed line. for example : fill area between 2 red dashed line, 2 blue dashed line and 2 green dashed line.

I try do it like this:

function fillArea(){
    var d = {};
    var x = [];
    var y1 = [];
    var y0 = [];

    for(var i = 0; i < chartJson.length; i++){
        x.push(chartJson[i].run_date);
        y0.push(chartJson[i].diviationMinus);
        y1.push(chartJson[i].diviationPlus);
    }
    d.x = x;
    d.y1 = y1;
    d.y0 = y0;


    var area = d3.svg.area()
                 .x(function(d) {return x(d.x); })
                 .y0(function(d) { return y(d.y0); })
                 .y1(function(d) { return y(d.y1); });  

}
fillArea();

but nothing happen. Here is jsfiddle https://jsfiddle.net/1xnc6y58/


回答1:


Change the fillArea() function into the code below:

function fillArea(){
    var yscale = chart.internal.y; 
    var xscale = chart.internal.x; 
var indexies = d3.range( chartJson.length );

var area = d3.svg.area()
                         .interpolate("linear")
             .x(function(d) {return xscale(chartJson[d].x); })
             .y0(function(d) { return yscale(chartJson[d].y0); })
             .y1(function(d) { return yscale(chartJson[d].y1); }); 

 d3.select("#chart svg g")
  .append('path')
  .datum(indexies)
  .attr('class', 'area')
  .attr('d', area);}

Added css:

path.area { 
  stroke: lightgreen;
  fill: #e6ffe6;
  opacity: 0.9   }

Working fiddle: fill between lines c3.js

However if the X axis is a timeseries, the xscale(here should be a date object instead of the string). Here is an timeseries example: Timeseries C3.js fill between lines



来源:https://stackoverflow.com/questions/33164568/c3-js-fill-area-between-curves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!