Skip to content

Commit

Permalink
fix(geo): highlight choropleth (#5095)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored May 25, 2023
1 parent 67d88f4 commit 7a4d06d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/plots/interaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ export { aaplLineSliderFilterTranspose } from './appl-line-slider-filter-transpo
export { alphabetIntervalFunnelLegendFilter } from './alphabet-interval-funnel-legend-filter';
export { penguinsPointBrushHandleStyle } from './penguins-point-brush-handle-style';
export { penguinsPointBrushHandleCustom } from './penguins-point-brush-handle-custom';
export { unemploymentChoropleth } from './unemployment-choropleth';
47 changes: 47 additions & 0 deletions __tests__/plots/interaction/unemployment-choropleth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { tsv } from 'd3-fetch';
import { feature } from 'topojson';
import { autoType } from 'd3-dsv';
import { G2Spec, ELEMENT_CLASS_NAME } from '../../../src';
import { step } from './utils';

export async function unemploymentChoropleth(): Promise<G2Spec> {
const us = await fetch('data/us-10m.json').then((res) => res.json());
const unemployment = await tsv('data/unemployment.tsv', autoType);
const counties = feature(us, us.objects.counties).features;
return {
type: 'geoPath',
coordinate: { type: 'albersUsa' },
data: {
value: counties,
transform: [
{
type: 'join',
join: unemployment,
on: ['id', 'id'],
select: ['rate'],
},
],
},
scale: {
color: {
unknown: '#fff',
},
},
state: {
active: { fill: 'red' },
},
encode: {
color: 'rate',
},
legend: { color: { layout: { justifyContent: 'center' } } },
interaction: { elementHighlight: true },
};
}

unemploymentChoropleth.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const e = elements.find((d) => d.__data__.title === 6071);
console.log(e.__data__);
return [step(e, 'pointerover')];
};
4 changes: 3 additions & 1 deletion __tests__/unit/api/composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('Composition', () => {
expectToCreateMarks(node);
});

it('GeoView() should specify options by API', () => {
it('GeoPath() should specify options by API', () => {
const node = new GeoPath();
node
.attr('paddingBottom', 10)
Expand All @@ -327,6 +327,7 @@ describe('Composition', () => {
.attr('marginTop', 30)
.attr('marginRight', 40)
.encode('x', 'a')
.state('active', { fill: 'red' })
.scale('color', { type: 'linear' })
.attr('key', 'composition')
.style('plotFill', 'red')
Expand All @@ -352,6 +353,7 @@ describe('Composition', () => {
theme: { defaultColor: 'red' },
scale: { color: { type: 'linear' } },
encode: { x: 'a' },
state: { active: { fill: 'red' } },
});
expectToCreateMarks(node);
});
Expand Down
2 changes: 2 additions & 0 deletions src/api/composition/geoPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface GeoPath extends Mark, Composition {
scale: ObjectAttribute<GeoPathSpec['scale'], GeoPath>;
encode: ObjectAttribute<GeoPathSpec['encode'], GeoPath>;
legend: ObjectAttribute<GeoPathSpec['legend'], GeoPath>;
state: ObjectAttribute<GeoPathSpec['state'], GeoPath>;
}

@defineProps([
Expand All @@ -26,6 +27,7 @@ export interface GeoPath extends Mark, Composition {
{ type: 'object', name: 'scale' },
{ type: 'object', name: 'encode' },
{ type: 'object', name: 'legend' },
{ type: 'object', name: 'state' },
...nodeProps(mark),
])
export class GeoPath extends CompositionNode<GeoPathComposition> {
Expand Down
4 changes: 3 additions & 1 deletion src/geo/geoPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type GeoPathOptions = Omit<GeoPathComposition, 'type'>;
*/
export const GeoPath: CC<GeoPathOptions> = () => {
return (options) => {
const { type, data, scale, encode, style, animate, key, ...rest } = options;
const { type, data, scale, encode, style, animate, key, state, ...rest } =
options;
return [
{
type: 'geoView',
Expand All @@ -24,6 +25,7 @@ export const GeoPath: CC<GeoPathOptions> = () => {
encode,
style,
animate,
state,
},
],
},
Expand Down

0 comments on commit 7a4d06d

Please sign in to comment.