Skip to content

Commit

Permalink
Add multi color support to continuous color legend
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewma7 committed Apr 19, 2018
1 parent 0baf387 commit df5749a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/color-legend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ $(function () {
base: Math.E,
domain: [100, 10000],
colors: ['blue', 'olive']
},
{
name: 'Continuous multicolor',
type: 'continuous',
scale: 'linear',
domain: [100, 1000],
colors: ['red', 'blue', 'green', 'orange']
}
]);

Expand Down
7 changes: 6 additions & 1 deletion src/ui/colorLegendWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,14 @@ var colorLegendWidget = function (arg) {
gradient.append('stop')
.attr('offset', '0%')
.attr('stop-color', category.colors[0]);
for (var i = 1; i < category.colors.length - 1; i++) {
gradient.append('stop')
.attr('offset', (100 / (category.colors.length - 1) * i).toFixed(6) + '%')
.attr('stop-color', category.colors[i]);
}
gradient.append('stop')
.attr('offset', '100%')
.attr('stop-color', category.colors[1]);
.attr('stop-color', category.colors[category.colors.length - 1]);
svg.append('rect')
.attr('fill', 'url(#gradient' + id + ')')
.attr('width', width)
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/colorLegend.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ describe('color legend', function () {
base: Math.E,
domain: [100, 10000],
colors: ['blue', 'olive']
},
{
name: 'Continuous multicolor',
type: 'continuous',
scale: 'pow',
domain: [100, 1000],
colors:  ['red', 'blue', 'green', 'orange']
}
];

Expand Down Expand Up @@ -164,6 +171,10 @@ describe('color legend', function () {
expect($(legends[7]).find('svg g.tick text').toArray().map(function (text) {
return $(text).text();
}).join(', ')).toBe('150, 400, 1.1k, 3.0k, 8.1k');

expect($(legends[8]).find('svg>defs stop').length).toBe(4);
expect($(legends[8]).find('svg>defs stop:nth-child(2)').attr('offset')).toBe('33.333333%');
expect($(legends[8]).find('svg>defs stop:nth-child(3)').attr('offset')).toBe('66.666667%');
});

it('test mouse events', function () {
Expand Down

0 comments on commit df5749a

Please sign in to comment.