Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebasing region label work from PR 1853 #2703

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/reference.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,8 @@
%p The values must be an array for each data and it should include an object that has <span class="code">start</span>, <span class="code">end</span>, <span class="code">style</span>. If <span class="code">start</span> is not set, the start will be the first data point. If <span class="code">end</span> is not set, the end will be the last data point.
%br
%p Currently this option supports only line chart and dashed style. If this option specified, the line will be dashed only in the regions.
%br
%p An optional <span class="code">label</span> property can be provided to display a label for the region. If a label option is not specified, no label will be displayed for the region. For each region, you may also specify the <span class="code">paddingY</span> and <span class="code">paddingX</span> options to control the position of label text. Finally, a <span class="code">vertical</span> option can be used to identify whether or not the label text should be rotated 90 degrees.
%h5 Default:
<code>{}</code>
%h5 Format:
Expand All @@ -1087,7 +1089,10 @@
%code.html.javascript
data: {
&nbsp;&nbsp;regions: {
&nbsp;&nbsp;&nbsp;&nbsp;data1: [{'start':1, 'end':2, 'style':'dashed'},{'start':3}],
&nbsp;&nbsp;&nbsp;&nbsp;data1: [
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'start':1, 'end':2, 'style':'dashed'},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'start':3, label:"Region 2", paddingX:2, paddingY:2, vertical=true}
&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;...
&nbsp;&nbsp;}
}
Expand Down
14 changes: 7 additions & 7 deletions htdocs/samples/regions.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
}
},
regions: [
{end:1,class:'region1'},
{start:2,end:4,class:'region1'},
{start:5,class:'region1'},
{end:50,axis:'y'},
{start:100,end:200,axis:'y'},
{start:300,axis:'y'},
{end: 1, class: 'region1', label: 'Region 1', paddingY: 15},
{start: 2, end: 4, class: 'region1', label: 'Region 2', paddingY: 20, paddingX: 10, vertical: true},
{start: 5, class: 'region1', label: 'Region 3', paddingY: 15},
{end: 50, axis: 'y'},
{start: 100, end: 200, axis: 'y'},
{start: 300, axis: 'y'},
],
zoom: {
// enabled: true
Expand Down Expand Up @@ -64,7 +64,7 @@
setTimeout(function () {
chart.regions.add([
{start:3,end:3.5,class:"region3 hoge"},
{start:4,end:4.5,class:"region4 hoge"},
{start:4,end:4.5,class:"region4 hoge",label:"Region 4"},
{start:0,end:0.5,class:"region5 hogehoge"},
]);
}, 7000);
Expand Down
11 changes: 9 additions & 2 deletions spec/api.region-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('c3 api region', function () {
start: 300,
end: 400,
class: 'green',
label: 'Region 1',
},
{
axis: 'y',
Expand All @@ -40,13 +41,15 @@ describe('c3 api region', function () {
axis: 'y',
start: 250,
end: 350,
class: 'red'
class: 'red',
label: 'Region 1'
},
{
axis: 'y',
start: 25,
end: 75,
class: 'red'
class: 'red',
label: ''
}
],
regions;
Expand All @@ -59,18 +62,22 @@ describe('c3 api region', function () {

regions.each(function (d, i) {
var region = d3.select(this),
label = region.select('text'),
y = +region.attr('y'),
height = +region.attr('height'),
expectedClass = 'red',
expectedLabel = expectedRegions[i].label,
unexpectedClass = 'green',
expectedStart = Math.round(chart.internal.y(expectedRegions[i].start)),
expectedEnd = Math.round(chart.internal.y(expectedRegions[i].end)),
expectedY = expectedEnd,
expectedHeight = expectedStart - expectedEnd;

expect(y).toBeCloseTo(expectedY, -1);
expect(height).toBeCloseTo(expectedHeight, -1);
expect(region.classed(expectedClass)).toBeTruthy();
expect(region.classed(unexpectedClass)).toBeFalsy();
expect(label.text()).toBe(expectedLabel);
});
}, 500);

Expand Down
39 changes: 33 additions & 6 deletions src/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@ ChartInternal.prototype.updateRegion = function (duration) {

var mainRegion = $$.main.select('.' + CLASS.regions).selectAll('.' + CLASS.region)
.data(config.regions);
var mainRegionEnter = mainRegion.enter().append('rect')
var g = mainRegion.enter().append('g');
g.append('rect')
.attr("x", $$.regionX.bind($$))
.attr("y", $$.regionY.bind($$))
.attr("width", $$.regionWidth.bind($$))
.attr("height", $$.regionHeight.bind($$))
.style("fill-opacity", 0);
$$.mainRegion = mainRegionEnter.merge(mainRegion)
.style("fill-opacity", function (d) { return isValue(d.opacity) ? d.opacity : 0.1; });
g.append('text')
.text($$.labelRegion.bind($$));
$$.mainRegion = g.merge(mainRegion)
.attr('class', $$.classRegion.bind($$));
mainRegion.exit().transition().duration(duration)
.style("opacity", 0)
.remove();
};
ChartInternal.prototype.redrawRegion = function (withTransition, transition) {
var $$ = this, regions = $$.mainRegion;
return [(withTransition ? regions.transition(transition) : regions)
var $$ = this,
regions = $$.mainRegion,
regionLabels = $$.mainRegion.selectAll('text');
return [
(withTransition ? regions.transition(transition) : regions)
.attr("x", $$.regionX.bind($$))
.attr("y", $$.regionY.bind($$))
.attr("width", $$.regionWidth.bind($$))
.attr("height", $$.regionHeight.bind($$))
.style("fill-opacity", function (d) { return isValue(d.opacity) ? d.opacity : 0.1; })
.style("fill-opacity", function (d) { return isValue(d.opacity) ? d.opacity : 0.1; }),
(withTransition ? regionLabels.transition(transition) : regionLabels)
.attr("x", $$.labelOffsetX.bind($$))
.attr("y", $$.labelOffsetY.bind($$))
.attr("transform", $$.labelTransform.bind($$))
.attr("style", 'text-anchor: left;')
];
};
ChartInternal.prototype.regionX = function (d) {
Expand Down Expand Up @@ -81,3 +92,19 @@ ChartInternal.prototype.regionHeight = function (d) {
ChartInternal.prototype.isRegionOnX = function (d) {
return !d.axis || d.axis === 'x';
};
ChartInternal.prototype.labelRegion = function (d) {
return 'label' in d ? d.label : '';
};
ChartInternal.prototype.labelTransform = function (d) {
return ('vertical' in d && d.vertical) ? "rotate(90)" : "";
};
ChartInternal.prototype.labelOffsetX = function (d) {
var paddingX = 'paddingX' in d ? d.paddingX : 3;
var paddingY = 'paddingY' in d ? d.paddingY : 3;
return ('vertical' in d && d.vertical) ? this.regionY(d) + paddingY : (this.regionX(d) + paddingX);
};
ChartInternal.prototype.labelOffsetY = function (d) {
var paddingX = 'paddingX' in d ? d.paddingX : 3;
var paddingY = 'paddingY' in d ? d.paddingY : 3;
return ('vertical' in d && d.vertical) ? -(this.regionX(d) + paddingX) : this.regionY(d) + 10 + paddingY;
};
4 changes: 4 additions & 0 deletions src/scss/region.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.c3-region {
fill: steelblue;
fill-opacity: .1;

text {
fill-opacity: 1;
}
}