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
feat: map chart structure #671
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3a13f98
chore: add babel, tsconfig
cb5daf5
wip: add skeleton
cbdbe14
chore: add shared type
4aa2509
feat: add store, component, chart skeleton
1de4193
feat: add geoFeature model
e7c1513
chore: add world story
9c38c32
feat: add responder model
7c9a1dc
chore: add responder type
b78e27a
chore: apply review
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,26 @@ | ||
import { GeoPath } from 'd3-geo'; | ||
import { setSize } from '@src/helpers/painter'; | ||
import { GeoFeatureModel } from '@t/components/geoFeature'; | ||
|
||
export function geoFeature(ctx: CanvasRenderingContext2D, gp: GeoPath, model: GeoFeatureModel) { | ||
const { feature, color } = model; | ||
const areaCanvas = document.createElement('canvas'); | ||
const areaCtx = areaCanvas.getContext('2d')!; | ||
const [[x, y], [x2, y2]] = gp.bounds(feature); | ||
const width = Math.max(x2 - x, 1); | ||
const height = Math.max(y2 - y, 1); | ||
setSize(areaCanvas, areaCtx, width, height); | ||
|
||
areaCtx.beginPath(); | ||
areaCtx.translate(-x, -y); | ||
|
||
gp.context(areaCtx)(feature); | ||
|
||
areaCtx.lineWidth = 1; | ||
areaCtx.fillStyle = color; | ||
|
||
areaCtx.fill(); | ||
areaCtx.stroke(); | ||
|
||
ctx.drawImage(areaCanvas, x, y, width, height); | ||
} |
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,125 @@ | ||
import Store from './store/store'; | ||
import * as outlineBrush from './brushes/geoFeature'; | ||
import { debounce, EventEmitter } from '@toast-ui/shared'; | ||
import { ChartProps } from '@t/store'; | ||
import Painter from '@src/painter'; | ||
import GeoFeature from '@src/component/geoFeature'; | ||
import ComponentManager from '@src/component/componentManager'; | ||
import { responderDetectors } from '@src/responderDetectors'; | ||
|
||
import root from '@src/store/root'; | ||
import theme from '@src/store/theme'; | ||
import series from '@src/store/series'; | ||
import layout from '@src/store/layout'; | ||
|
||
export default class MapChart { | ||
store!: Store; | ||
|
||
readonly componentManager: ComponentManager; | ||
|
||
readonly containerEl: HTMLElement; | ||
|
||
el: HTMLDivElement; | ||
|
||
ctx!: CanvasRenderingContext2D; | ||
|
||
painter = new Painter(this); | ||
|
||
readonly eventBus: EventEmitter = new EventEmitter(); | ||
|
||
constructor(props: ChartProps) { | ||
const { el, options } = props; | ||
|
||
this.containerEl = el; | ||
this.el = this.createChartWrapper(); | ||
this.containerEl.appendChild(this.el); | ||
this.store = new Store({ options }); | ||
this.componentManager = new ComponentManager({ | ||
store: this.store, | ||
eventBus: this.eventBus, | ||
}); | ||
this.eventBus.on( | ||
'needDraw', | ||
debounce(() => this.draw(), 10) | ||
); | ||
this.initialize(); | ||
this.store.observe(() => this.painter.setup()); | ||
|
||
// @TODO need to be called from animator | ||
this.draw(); | ||
} | ||
|
||
createChartWrapper() { | ||
const el = document.createElement('div'); | ||
el.classList.add('toastui-map-chart-wrapper'); | ||
|
||
return el; | ||
} | ||
|
||
draw() { | ||
this.painter.beforeFrame(); | ||
|
||
this.componentManager.forEach((component) => { | ||
if (!component.isShow) { | ||
return; | ||
} | ||
|
||
this.painter.beforeDraw(component.rect.x, component.rect.y); | ||
component.beforeDraw?.(this.painter); | ||
|
||
component.draw(this.painter); | ||
this.painter.afterDraw(); | ||
}); | ||
} | ||
|
||
handleEvent(event: MouseEvent) { | ||
const { clientX, clientY, type: eventType } = event; | ||
|
||
const delegationMethod = `on${eventType[0].toUpperCase() + eventType.substring(1)}`; | ||
|
||
const canvas = this.painter.ctx.canvas; | ||
const canvasRect = canvas.getBoundingClientRect(); | ||
// Calculate scale for chart affected by a CSS transform. | ||
const scaleX = canvasRect.width / canvas.offsetWidth; | ||
const scaleY = canvasRect.height / canvas.offsetHeight; | ||
|
||
const mousePosition = { | ||
x: (clientX - canvasRect.left) / scaleX, | ||
y: (clientY - canvasRect.top) / scaleY, | ||
}; | ||
|
||
this.componentManager.forEach((component) => { | ||
if (!component[delegationMethod]) { | ||
return; | ||
} | ||
|
||
if (!responderDetectors.rect({ mousePosition, model: component.rect })) { | ||
return; | ||
} | ||
|
||
const detected = (component.responders || []).filter((model) => { | ||
return responderDetectors[model.responderType]({ | ||
mousePosition, | ||
model, | ||
componentRect: component.rect, | ||
projection: this.painter.projection, | ||
}); | ||
}); | ||
|
||
component[delegationMethod]({ mousePosition, responders: detected }, event); | ||
}); | ||
} | ||
|
||
protected initStore() { | ||
[root, theme, series, layout].forEach((module) => this.store.setModule(module)); | ||
} | ||
|
||
protected initialize() { | ||
this.initStore(); | ||
this.store.dispatch('initChartSize', this.containerEl); | ||
|
||
this.componentManager.add(GeoFeature); | ||
|
||
this.painter.addGroups([outlineBrush]); | ||
} | ||
} |
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,68 @@ | ||
import { ChartState, Rect } from '@t/store'; | ||
import Store from '@src/store/store'; | ||
import Painter from '@src/painter'; | ||
import { EventEmitter } from '@toast-ui/shared'; | ||
import { ResponderModel } from '@t/responders'; | ||
import { GeoFeatureModel } from '@t/components/geoFeature'; | ||
|
||
export type ComponentType = 'component' | 'geoFeature'; | ||
type ComponentModels = GeoFeatureModel[]; | ||
|
||
export default abstract class Component { | ||
name = 'Component'; | ||
|
||
type: ComponentType = 'component'; | ||
|
||
rect: Rect = { | ||
x: 0, | ||
y: 0, | ||
height: 0, | ||
width: 0, | ||
}; | ||
|
||
isShow = true; | ||
|
||
store: Store; | ||
|
||
eventBus: EventEmitter; | ||
|
||
models!: ComponentModels; | ||
|
||
drawModels!: ComponentModels; | ||
|
||
responders!: ResponderModel[]; | ||
|
||
constructor({ store, eventBus }: { store: Store; eventBus: EventEmitter }) { | ||
this.store = store; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
abstract initialize(args: any): void; | ||
|
||
abstract render(state: ChartState, computed: Record<string, any>): void; | ||
|
||
beforeDraw?(painter: Painter): void; | ||
|
||
onClick?(responseData: any): void; | ||
|
||
onMousemove?(responseData: any): void; | ||
|
||
onMouseenterComponent?(): void; | ||
|
||
onMouseoutComponent?(): void; | ||
|
||
onMousedown?(responseData: any): void; | ||
|
||
onMouseup?(responseData: any): void; | ||
|
||
draw(painter: Painter) { | ||
const models = this.drawModels ? this.drawModels : this.models; | ||
if (Array.isArray(models)) { | ||
painter.paintForEach(models); | ||
} else if (models) { | ||
Object.keys(models).forEach((item) => { | ||
painter.paintForEach(models[item]); | ||
}); | ||
} | ||
} | ||
} |
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.
Feature 뭔가 어색한 늬낌인데. 걍 geo하면 어때요?
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.
geoJSON에서 제공하는 feature collection을 가지고 그리는 브러시라 컴포넌트랑 같이 getFeature라는 이름으로 통일 시켰는데요.
개인적으로는 feature라는 단어가 들어가야 명확할 것 같은데 혹시 다른 분들 의견은 어떠신가요?