This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
feat(plugin-chart-word-cloud): convert word cloud to use encodable #258
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd80652
feat: convert word cloud to use encodable
kristw 5aae2e6
fix: minor bugs
kristw c4ed595
feat: bump dependencies
kristw f99e762
feat: use field
kristw f5e429e
fix: defaultProps
kristw debd1e4
fix: unit test
kristw 3d4a66f
docs: update storybook
kristw a7046a9
refactor: move files
kristw 264ef84
docs: update storybook
kristw 29034e9
fix: unit test
kristw 06b2e61
fix: address comments
kristw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 0 additions & 116 deletions
116
packages/superset-ui-plugin-chart-word-cloud/src/WordCloud.tsx
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
packages/superset-ui-plugin-chart-word-cloud/src/WordCloudFormData.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/superset-ui-plugin-chart-word-cloud/src/chart/Encoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createEncoderFactory } from 'encodable'; | ||
import { DeriveEncoding } from 'encodable/lib/types/Encoding'; | ||
|
||
type WordCloudEncodingConfig = { | ||
color: ['Color', string]; | ||
fontFamily: ['Category', string]; | ||
fontSize: ['Numeric', number]; | ||
fontWeight: ['Category', string | number]; | ||
text: ['Text', string]; | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const wordCloudEncoderFactory = createEncoderFactory<WordCloudEncodingConfig>({ | ||
channelTypes: { | ||
color: 'Color', | ||
fontFamily: 'Category', | ||
fontSize: 'Numeric', | ||
fontWeight: 'Category', | ||
text: 'Text', | ||
}, | ||
defaultEncoding: { | ||
color: { value: 'black' }, | ||
fontFamily: { value: 'Helvetica' }, | ||
fontSize: { value: 20 }, | ||
fontWeight: { value: 'bold' }, | ||
text: { value: '' }, | ||
}, | ||
}); | ||
|
||
export type WordCloudEncoding = DeriveEncoding<WordCloudEncodingConfig>; |
128 changes: 128 additions & 0 deletions
128
packages/superset-ui-plugin-chart-word-cloud/src/chart/WordCloud.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React from 'react'; | ||
import cloudLayout, { Word } from 'd3-cloud'; | ||
import { PlainObject } from 'encodable/lib/types/Data'; | ||
import { WordCloudEncoding, wordCloudEncoderFactory } from './Encoder'; | ||
|
||
export const ROTATION = { | ||
flat: () => 0, | ||
/* eslint-disable-next-line no-magic-numbers */ | ||
random: () => Math.floor(Math.random() * 6 - 3) * 30, | ||
/* eslint-disable-next-line no-magic-numbers */ | ||
square: () => Math.floor(Math.random() * 2) * 90, | ||
}; | ||
|
||
export type RotationType = keyof typeof ROTATION; | ||
|
||
/** | ||
* These props should be stored when saving the chart. | ||
*/ | ||
export interface WordCloudVisualProps { | ||
encoding?: Partial<WordCloudEncoding>; | ||
rotation?: RotationType; | ||
} | ||
|
||
export interface WordCloudProps extends WordCloudVisualProps { | ||
data: PlainObject[]; | ||
height: number; | ||
width: number; | ||
} | ||
|
||
interface State { | ||
words: Word[]; | ||
} | ||
|
||
const defaultProps = { | ||
encoding: {}, | ||
rotation: 'flat', | ||
}; | ||
|
||
export default class WordCloud extends React.PureComponent< | ||
WordCloudProps & typeof defaultProps, | ||
State | ||
> { | ||
isMounted: boolean = false; | ||
state: State = { | ||
words: [], | ||
}; | ||
|
||
createEncoder = wordCloudEncoderFactory.createSelector(); | ||
|
||
static defaultProps = defaultProps; | ||
|
||
componentDidMount() { | ||
this.isMounted = true; | ||
this.update(); | ||
} | ||
|
||
componentDidUpdate(prevProps: WordCloudProps) { | ||
const { data, encoding, width, height, rotation } = this.props; | ||
|
||
if ( | ||
prevProps.data !== data || | ||
prevProps.encoding !== encoding || | ||
prevProps.width !== width || | ||
prevProps.height !== height || | ||
prevProps.rotation !== rotation | ||
) { | ||
this.update(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.isMounted = false; | ||
} | ||
|
||
setWords = (words: Word[]) => { | ||
if (this.isMounted) { | ||
this.setState({ words }); | ||
} | ||
}; | ||
|
||
update() { | ||
const { data, width, height, rotation, encoding } = this.props; | ||
|
||
const encoder = this.createEncoder(encoding); | ||
encoder.channels.fontSize.setDomainFromDataset(data); | ||
|
||
cloudLayout() | ||
.size([width, height]) | ||
.words(data) | ||
/* eslint-disable-next-line no-magic-numbers */ | ||
.padding(5) | ||
.rotate(ROTATION[rotation] || ROTATION.flat) | ||
.text(d => encoder.channels.text.getValueFromDatum(d)) | ||
.font(d => encoder.channels.fontFamily.encodeDatum(d, 'Helvetica')) | ||
.fontWeight(d => encoder.channels.fontWeight.encodeDatum(d, 'normal')) | ||
.fontSize(d => encoder.channels.fontSize.encodeDatum(d, 0)) | ||
.on('end', this.setWords) | ||
.start(); | ||
} | ||
|
||
render() { | ||
const { width, height, encoding } = this.props; | ||
const { words } = this.state; | ||
|
||
const encoder = this.createEncoder(encoding); | ||
encoder.channels.color.setDomainFromDataset(words); | ||
|
||
return ( | ||
<svg width={width} height={height}> | ||
<g transform={`translate(${width / 2},${height / 2})`}> | ||
{words.map(w => ( | ||
<text | ||
key={w.text} | ||
fontSize={`${w.size}px`} | ||
fontWeight={w.weight} | ||
fontFamily={w.font} | ||
fill={encoder.channels.color.encodeDatum(w, '')} | ||
textAnchor="middle" | ||
transform={`translate(${w.x}, ${w.y}) rotate(${w.rotate})`} | ||
> | ||
{w.text} | ||
</text> | ||
))} | ||
</g> | ||
</svg> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/superset-ui-plugin-chart-word-cloud/src/legacy/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { t } from '@superset-ui/translation'; | ||
import { ChartMetadata, ChartPlugin } from '@superset-ui/chart'; | ||
import buildQuery from '../buildQuery'; | ||
import transformProps from './transformProps'; | ||
import thumbnail from '../images/thumbnail.png'; | ||
import { LegacyWordCloudFormData } from './types'; | ||
|
||
const metadata = new ChartMetadata({ | ||
credits: ['https://github.com/jasondavies/d3-cloud'], | ||
description: '', | ||
name: t('Word Cloud'), | ||
thumbnail, | ||
useLegacyApi: true, | ||
}); | ||
|
||
export default class WordCloudChartPlugin extends ChartPlugin<LegacyWordCloudFormData> { | ||
constructor() { | ||
super({ | ||
buildQuery, | ||
loadChart: () => import('../chart/WordCloud'), | ||
metadata, | ||
transformProps, | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe leave a comment to explain that this calculates a random rotation between -90 and 90 degrees. I'm assuming the unit is degrees since square is
* 90
, but it would be helpful to read it explicitly when skimming the code.