If you want to set the regions statically, you can do it like this:
To set them dynamically, you just need to utilize the API:
for example.
chart.regions([
{axis: 'x', start: 5, class: 'regionX'},
{axis: 'y', end: 50, class: 'regionY'}
]);
To create alternating stripes, construct a regions array in the following way after creating your chart
object:
// Determine range of all data
var allData = d3.merge (chart.data().map(function(d) {
return d.values.map (function(dd) { return dd.value; });
}));
var dataRange = d3.extent(allData);
dataRange.min = Math.min (dataRange[0], 0);
dataRange.extent = dataRange[1] - dataRange.min;
// Set number of pairs of stripes
var stripeCount = 5;
var step = dataRange.extent / stripeCount;
var newRegions = [];
// Divide the data range into pairs of stripes
d3.range(0,stripeCount).forEach (function(d) {
newRegions.push ({axis: 'y', start: dataRange.min+(step*d), end: dataRange.min+(step*(d+0.5)), class: 'stripe1'});
newRegions.push ({axis: 'y', start: dataRange.min+(step*(d+0.5)), end: dataRange.min+(step*(d+1)), class: 'stripe2'});
});
// Apply the newly created regions to the chart object
chart.regions(newRegions);
CSS styling:
.c3-region.stripe1 {
fill: #f00;
}
.c3-region.stripe2 {
fill: #0f0;
}
(Alternatively, if you prefer to add a new stripe pair every 10 units on the y scale, simply set step=10;
and adjust the d3.range to
d3.range(0,dataRange.extent/step)
)
I modified someone's bar chart on jsfiddle by adding stripes --> http://jsfiddle.net/k9c0peax/