-
Notifications
You must be signed in to change notification settings - Fork 19.7k
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
[Feature] Add dynamic theme switching support #20705
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,6 +232,10 @@ export interface ResizeOpts { | |
silent?: boolean // by default false. | ||
}; | ||
|
||
export interface SetThemeOpts { | ||
silent?: boolean; | ||
} | ||
|
||
interface PostIniter { | ||
(chart: EChartsType): void | ||
} | ||
|
@@ -404,11 +408,6 @@ class ECharts extends Eventful<ECEventDefinition> { | |
|
||
opts = opts || {}; | ||
|
||
// Get theme by name | ||
if (isString(theme)) { | ||
theme = themeStorage[theme] as object; | ||
} | ||
|
||
this._dom = dom; | ||
|
||
let defaultRenderer = 'canvas'; | ||
|
@@ -459,10 +458,7 @@ class ECharts extends Eventful<ECEventDefinition> { | |
// Expect 60 fps. | ||
this._throttledZrFlush = throttle(bind(zr.flush, zr), 17); | ||
|
||
theme = clone(theme); | ||
theme && backwardCompat(theme as ECUnitOption, true); | ||
|
||
this._theme = theme; | ||
this._updateTheme(theme); | ||
|
||
this._locale = createLocaleObject(opts.locale || SYSTEM_LANG); | ||
|
||
|
@@ -693,10 +689,69 @@ class ECharts extends Eventful<ECEventDefinition> { | |
} | ||
|
||
/** | ||
* @deprecated | ||
* Update theme with name or theme option and repaint the chart. | ||
* @param theme Theme name or theme option. | ||
* @param opts Optional settings | ||
*/ | ||
private setTheme(): void { | ||
deprecateLog('ECharts#setTheme() is DEPRECATED in ECharts 3.0'); | ||
setTheme(theme: string | ThemeOption, opts?: SetThemeOpts): void { | ||
if (this[IN_MAIN_PROCESS_KEY]) { | ||
if (__DEV__) { | ||
error('`setTheme` should not be called during main process.'); | ||
} | ||
return; | ||
} | ||
|
||
if (this._disposed) { | ||
disposedWarning(this.id); | ||
return; | ||
} | ||
|
||
const ecModel = this._model; | ||
if (!ecModel) { | ||
return; | ||
} | ||
|
||
let silent = opts && opts.silent; | ||
let updateParams = null as UpdateLifecycleParams; | ||
|
||
if (this[PENDING_UPDATE]) { | ||
if (silent == null) { | ||
silent = (this[PENDING_UPDATE] as any).silent; | ||
} | ||
updateParams = (this[PENDING_UPDATE] as any).updateParams; | ||
this[PENDING_UPDATE] = null; | ||
} | ||
|
||
this[IN_MAIN_PROCESS_KEY] = true; | ||
|
||
try { | ||
this._updateTheme(theme); | ||
ecModel.setTheme(this._theme); | ||
|
||
prepare(this); | ||
updateMethods.update.call(this, {type: 'setTheme'}, updateParams); | ||
} | ||
catch (e) { | ||
this[IN_MAIN_PROCESS_KEY] = false; | ||
throw e; | ||
} | ||
|
||
this[IN_MAIN_PROCESS_KEY] = false; | ||
|
||
flushPendingActions.call(this, silent); | ||
triggerUpdatedEvent.call(this, silent); | ||
} | ||
|
||
private _updateTheme(theme: string | ThemeOption): void { | ||
if (isString(theme)) { | ||
theme = themeStorage[theme] as object; | ||
} | ||
|
||
if (theme) { | ||
theme = clone(theme); | ||
theme && backwardCompat(theme as ECUnitOption, true); | ||
this._theme = theme; | ||
} | ||
} | ||
|
||
// We don't want developers to use getModel directly. | ||
|
@@ -1660,7 +1715,7 @@ class ECharts extends Eventful<ECEventDefinition> { | |
|
||
prepareAndUpdate(this: ECharts, payload: Payload): void { | ||
prepare(this); | ||
updateMethods.update.call(this, payload, { | ||
updateMethods.update.call(this, payload, payload && { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this change to make the code more robust, although we are not calling this function now. |
||
// Needs to mark option changed if newOption is given. | ||
// It's from MagicType. | ||
// TODO If use a separate flag optionChanged in payload? | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
If we provide an API
setTheme
, I think the behavior should better be similar tosetOption
. that is, it modifies the model, and causes re-render either immediately or in a lazy way.But in the current implementation, re-render will not be triggered.
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.
By calling
updateMethods.prepareAndUpdate
, it will re-render as soon as callingsetTheme
. See the demo of test case:Anything else I need to change?
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.
After reviewing the implementation of
ECharts#resize
, I updated by understanding.silent
is provided, which is supported by all of theECharts#setOption
,ECharts#resize
andECharts#dispatchAction
. So insetTheme
we'd better follow the convention.zr.flush()
to perform zr render before the API return?ECharts#setOption
: if not ssr, callzr.flush()
immediatelyECharts#dispatchAction
provide an parameterflush
to enable customization it.ECharts#resize
does not callzr.flush()
.ECharts#resize
does.IN_MAIN_PROCESS_KEY
is used to prevent from nested calling in bothECharts#setOption
,ECharts#resize
andECharts#dispatchAction
. So I think we can follow that if we do not want to renew the impl.So IMO the impl of setTheme can be (modified based on the impl of
ECharts#resize
):BTW, it seems that there is some defects in the impl of
ECharts#resize
:this[PENDING_UPDATE].updateParams
is omitted inlazyUpdate
scenario. I've no idea whether it does that deliberately or to be a mistake.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.
Thanks a lot for the help. This deepens my understanding a lot.