Skip to content

Commit

Permalink
feat(theme): add dark and update academy theme (#5584)
Browse files Browse the repository at this point in the history
* feat(theme): add dark theme

* feat(theme): update academy
  • Loading branch information
pearmini authored Sep 22, 2023
1 parent 7a799ec commit 051d719
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 828 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 2 additions & 8 deletions __tests__/plots/static/github-star-interval-theme-academy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import { githubStar } from '../../data/github-star';
export function githubStarIntervalThemeAcademy(): G2Spec {
return {
type: 'interval',
data: {
value: githubStar,
transform: [{ type: 'sortBy', fields: [['star', true]] }],
},
legend: { color: false },
theme: 'academy',
data: githubStar,
encode: {
x: 'name',
y: 'star',
color: 'name',
},
theme: {
type: 'academy',
},
};
}
3 changes: 2 additions & 1 deletion __tests__/unit/lib/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
Sequential,
Constant as ConstantScale,
} from '../../../src/scale';
import { Classic, ClassicDark, Academy, Light } from '../../../src/theme';
import { Classic, ClassicDark, Academy, Light, Dark } from '../../../src/theme';
import {
AxisX,
AxisY,
Expand Down Expand Up @@ -254,6 +254,7 @@ describe('corelib', () => {
'theme.classicDark': ClassicDark,
'theme.academy': Academy,
'theme.light': Light,
'theme.dark': Dark,
'component.axisX': AxisX,
'component.axisY': AxisY,
'component.legendCategory': LegendCategory,
Expand Down
3 changes: 2 additions & 1 deletion __tests__/unit/lib/std.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
Sequential,
Constant as ConstantScale,
} from '../../../src/scale';
import { Classic, ClassicDark, Academy, Light } from '../../../src/theme';
import { Classic, ClassicDark, Academy, Light, Dark } from '../../../src/theme';
import {
AxisX,
AxisY,
Expand Down Expand Up @@ -280,6 +280,7 @@ describe('stdlib', () => {
'theme.classicDark': ClassicDark,
'theme.academy': Academy,
'theme.light': Light,
'theme.dark': Dark,
'component.axisX': AxisX,
'component.axisY': AxisY,
'component.legendCategory': LegendCategory,
Expand Down
2 changes: 1 addition & 1 deletion site/.dumi/theme/components/ConfigPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import './ConfigPanel.less';

const BUILT_THEMES = [
{ label: '默认风格', key: 'light' },
{ label: '暗色风格', key: 'classicDark' },
{ label: '暗色风格', key: 'dark' },
{ label: '学术风格', key: 'academy' },
];

Expand Down
1 change: 1 addition & 0 deletions site/.dumi/theme/examples/bar-dodged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const BarDodged = ({ container, theme, width, height }) => {
.encode('x', 'state')
.encode('y', 'population')
.encode('color', 'age')
.scale('y', { nice: true })
.axis('y', { labelFormatter: '~s' })
.interaction('tooltip', { shared: true });

Expand Down
1 change: 1 addition & 0 deletions site/.dumi/theme/examples/bar-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const BarRange = ({ container, theme, width, height }) => {
.encode('color', (d) =>
d.month === 'Total' ? 'Total' : d.profit > 0 ? 'Increase' : 'Decrease',
)
.scale('y', { nice: true })
.axis('y', { labelFormatter: '~s' });

chart.render();
Expand Down
1 change: 1 addition & 0 deletions site/.dumi/theme/examples/grouped-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const groupedBox = ({ container, theme, width, height }) => {
.scale('x', { paddingInner: 0.2, paddingOuter: 0.1 })
.scale('y', { zero: true })
.scale('series', { paddingInner: 0.3, paddingOuter: 0.1 })
.scale('y', { nice: true })
.tooltip([
{ name: 'min', channel: 'y' },
{ name: 'q1', channel: 'y1' },
Expand Down
5 changes: 3 additions & 2 deletions site/.dumi/theme/examples/line-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const lineBar = ({ container, theme, width, height }) => {
.interval()
.encode('x', 'time')
.encode('y', 'waiting')
.axis('y', { titleFill: '#5B8FF9', title: 'Waiting' });
.axis('y', { titleFill: '#5B8FF9', title: 'Waiting' })
.scale('y', { nice: true });

chart
.line()
Expand All @@ -34,7 +35,7 @@ export const lineBar = ({ container, theme, width, height }) => {
.encode('shape', 'smooth')
.style('stroke', '#fdae6b')
.style('lineWidth', 2)
.scale('y', { independent: true })
.scale('y', { independent: true, nice: true })
.axis('y', {
position: 'right',
grid: null,
Expand Down
1 change: 1 addition & 0 deletions site/.dumi/theme/examples/line-connect-nulls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const lineConnectNulls = ({ container, theme, width, height }) => {
})
.encode('x', 'date')
.encode('y', 'close')
.scale('y', { nice: true })
.style('connectNulls', true);

chart.render();
Expand Down
1 change: 1 addition & 0 deletions site/.dumi/theme/examples/missing-data-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const missingDataArea = ({ container, theme, width, height }) => {
.encode('x', (d) => new Date(d.date))
// Mock missing data. Set NaN from Jan. to Mar.
.encode('y', (d) => (new Date(d.date).getUTCMonth() <= 3 ? NaN : d.close))
.scale('y', { nice: true })
.style('connect', true)
.style('connectFill', 'grey')
.style('connectFillOpacity', 0.15);
Expand Down
2 changes: 2 additions & 0 deletions site/.dumi/theme/examples/point-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const pointLog = ({ container, theme, width, height }) => {
.scale('size', { type: 'log', range: [4, 20] })
.style('fillOpacity', 0.3)
.style('lineWidth', 1)
.scale('x', { nice: true })
.scale('y', { nice: true })
.legend('size', false)
.interaction('fisheye', true);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
Sequential as ScaleSequential,
Constant as ScaleConstant,
} from '../scale';
import { Classic, ClassicDark, Academy, Light } from '../theme';
import { Classic, ClassicDark, Academy, Light, Dark } from '../theme';
import {
AxisX,
AxisY,
Expand Down Expand Up @@ -252,6 +252,7 @@ export function corelib() {
'theme.classicDark': ClassicDark,
'theme.academy': Academy,
'theme.light': Light,
'theme.dark': Dark,
'component.axisX': AxisX,
'component.axisY': AxisY,
'component.legendCategory': LegendCategory,
Expand Down
38 changes: 32 additions & 6 deletions src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,12 @@ export async function plot<T extends G2ViewTree>(
for (const typeOption of inferInteraction(options)) {
const [type, option] = typeOption;
if (option) {
const interaction = useInteraction({ type, ...(option as any) });
const interaction = useThemeInteraction(
target.view,
type,
option as Record<string, any>,
useInteraction,
);
const destroy = interaction(
target,
enterViewInstances,
Expand All @@ -303,7 +308,12 @@ export async function plot<T extends G2ViewTree>(

// Apply new interaction.
if (option) {
const interaction = useInteraction({ type, ...(option as any) });
const interaction = useThemeInteraction(
target.view,
type,
option as Record<string, any>,
useInteraction,
);
const destroy = interaction(
target,
updateViewInstances,
Expand Down Expand Up @@ -402,10 +412,12 @@ function updateTooltip(
if (!tooltipOptions[1]) return;

// Apply new tooltip interaction.
const applyTooltip = useInteraction({
type: 'tooltip',
...(tooltipOptions[1] as any),
});
const applyTooltip = useThemeInteraction(
view,
'tooltip',
tooltipOptions[1] as any,
useInteraction,
);
const target = {
options,
view,
Expand Down Expand Up @@ -593,6 +605,20 @@ async function initializeMarks(
return markState;
}

function useThemeInteraction(
view: G2ViewDescriptor,
type: string,
option: Record<string, any>,
useInteraction: (options: G2InteractionOptions, context?: any) => Interaction,
): Interaction {
const theme = view.theme;
const defaults = typeof type === 'string' ? theme[type] || {} : {};
const interaction = useInteraction(
deepMix(defaults, { type, ...(option as any) }),
);
return interaction;
}

function initializeState(
markState: Map<G2Mark, G2MarkState>,
options: G2View,
Expand Down
Loading

0 comments on commit 051d719

Please sign in to comment.