Skip to content

Commit

Permalink
Semi
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepzh committed Aug 4, 2024
1 parent 69b82cd commit fad7ad9
Show file tree
Hide file tree
Showing 24 changed files with 607 additions and 425 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"countup",
"daterange",
"echarts",
"emsp",
"ensp",
"filemanager",
"Hengyang",
"Kanban",
Expand All @@ -38,4 +40,4 @@
// Ignore i18n resources
"src/i18n/message/**/*.json",
],
}
}
22 changes: 2 additions & 20 deletions src/app/components/Analysis/components/Trend/Dimension/Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import { use } from "echarts/core"
import { LineChart } from "echarts/charts"
import { SVGRenderer } from "echarts/renderers"
import { TitleComponent, TooltipComponent, GridComponent } from "echarts/components"

import { ValueFormatter } from "@app/components/Analysis/util"
import { getSecondaryTextColor } from "@util/style"
import { EchartsWrapper } from "@hooks"
import { ZRColor } from "echarts/types/dist/shared"
import { getLineSeriesPalette } from "@app/util/echarts"

use([
LineChart,
Expand Down Expand Up @@ -50,24 +49,7 @@ type ValueItem = LineSeriesOption["data"][0] & {
_data: DimensionEntry
}

const THIS_COLOR: ZRColor = {
type: "linear",
x: 0, y: 0,
x2: 0, y2: 1,
colorStops: [
{ offset: 0, color: 'rgb(55, 162, 255)' },
{ offset: 1, color: 'rgb(116, 21, 219)' },
],
}
const PREV_COLOR: ZRColor = {
type: "linear",
x: 0, y: 0,
x2: 0, y2: 1,
colorStops: [
{ offset: 0, color: 'rgb(255, 0, 135)' },
{ offset: 1, color: 'rgb(135, 0, 157)' },
],
}
const [THIS_COLOR, PREV_COLOR] = getLineSeriesPalette()

const createTooltipLine = (param: any, valueFormatter: ValueFormatter) => {
const data = param.data as ValueItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { BASE_TITLE_OPTION } from "../../common"
import { getAppPageUrl } from "@util/constant/url"
import { REPORT_ROUTE } from "@app/router/constants"
import { createTabAfterCurrent } from "@api/chrome/tab"
import { getHeatColors, getStepColors } from "@app/util/echarts"
import { getStepColors } from "@app/util/echarts"

type _Value = [
x: number,
Expand Down
11 changes: 5 additions & 6 deletions src/app/components/Dashboard/components/MonthOnMonth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import statService from "@service/stat-service"
import { groupBy, sum } from "@util/array"
import { BASE_TITLE_OPTION } from "../common"
import { t } from "@app/locale"
import { getCssVariable, getPrimaryTextColor } from "@util/style"
import { getPrimaryTextColor } from "@util/style"
import DateIterator from "@util/date-iterator"
import { cvt2LocaleTime } from "@app/util/time"
import { tooltipDot } from "@app/util/echarts"
import { getCompareColor, getDiffColor, tooltipDot } from "@app/util/echarts"
import { EchartsWrapper, useEcharts } from "@hooks"

type EcOption = ComposeOption<
Expand All @@ -50,10 +50,9 @@ type _Value = {
function optionOf(lastPeriodItems: Row[], thisPeriodItems: Row[]): EcOption {
const textColor = getPrimaryTextColor()

const color1 = getCssVariable('--echarts-bar-compare-color-1')
const color2 = getCssVariable('--echarts-bar-compare-color-2')
const incColor = getCssVariable('--echarts-increase-color')
const decColor = getCssVariable('--echarts-decrease-color')
const [color1, color2] = getCompareColor()
const [incColor, decColor] = getDiffColor()

return {
title: {
...BASE_TITLE_OPTION,
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/Habit/components/HabitFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useState } from "@hooks"
type ShortCutProp = [label: string, dayAgo: number]

const shortcutProps: ShortCutProp[] = [
[t(msg => msg.calendar.range.lastHours, { n: 24 }), 1],
[t(msg => msg.calendar.range.today), 0],
[t(msg => msg.calendar.range.lastDays, { n: 3 }), 3],
[t(msg => msg.calendar.range.lastDays, { n: 7 }), 7],
[t(msg => msg.calendar.range.lastDays, { n: 15 }), 15],
Expand Down
176 changes: 176 additions & 0 deletions src/app/components/Habit/components/Site/DailyTrend/Wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { EchartsWrapper } from "@hooks"
import {
ComposeOption,
GridComponentOption,
TitleComponentOption,
TooltipComponentOption,
LegendComponentOption,
LineSeriesOption,
LinearGradientObject,
} from "echarts"
import { getAllDatesBetween } from "@util/time"
import { groupBy, sum } from "@util/array"
import { t } from "@app/locale"
import { GridComponent, LegendComponent, TitleComponent, TooltipComponent } from "echarts/components"
import { BarChart, LineChart, PieChart } from "echarts/charts"
import { use } from "echarts/core"
import { SVGRenderer } from "echarts/renderers"
import { TopLevelFormatterParams, YAXisOption } from "echarts/types/dist/shared"
import { generateTitleOption } from "../common"
import { getLineSeriesPalette, tooltipDot } from "@app/util/echarts"
import { cvt2LocaleTime, periodFormatter } from "@app/util/time"

use([SVGRenderer, GridComponent, LegendComponent, TooltipComponent, TitleComponent, LineChart, PieChart, BarChart])

type EcOption = ComposeOption<
| GridComponentOption
| TooltipComponentOption
| TitleComponentOption
| LineSeriesOption
| LegendComponentOption
>

const [FOCUS_COLORS, VISIT_COLORS, COUNT_COLORS] = getLineSeriesPalette()
const FOCUS_LEGEND = t(msg => msg.item.focus)
const VISIT_LEGEND = t(msg => msg.item.time)
const COUNT_LEGEND = t(msg => msg.habit.site.trend.siteCount)
const LEGEND_COLOR_MAP = {
[FOCUS_LEGEND]: FOCUS_COLORS,
[VISIT_LEGEND]: VISIT_COLORS,
[COUNT_LEGEND]: COUNT_COLORS,
}

export type BizOption = {
rows: timer.stat.Row[]
dateRange?: [Date, Date]
timeFormat?: timer.app.TimeFormat
}

const valueYAxis = (): YAXisOption => ({
type: 'value',
axisLabel: { show: false },
axisLine: { show: false },
axisTick: { show: false },
splitLine: { show: false },
})

const formatTimeTooltip = (params: TopLevelFormatterParams, format: timer.app.TimeFormat) => {
if (!Array.isArray(params)) return ''
const date = params?.[0]?.name
if (!date) return ''
const dateLine = `<b>${cvt2LocaleTime(date)}</b>`
const valueLines = params?.reverse?.()?.map(param => {
const { value, seriesName } = param
const color = LEGEND_COLOR_MAP[seriesName]
if (!color) return ''
let valueStr: string | number = seriesName === FOCUS_LEGEND
? periodFormatter(value as number, { format })
: (value as number)
return `${tooltipDot(color?.colorStops?.[0]?.color)}&ensp;${valueStr}&emsp;|&emsp;${seriesName}`
})?.filter(line => !!line)?.join('<br/>')

return `${dateLine}<br/>${valueLines}`
}

const lineOptionOf = (
areaColor: LinearGradientObject,
baseOption: Required<Pick<LineSeriesOption, 'data' | 'name' | 'yAxisIndex'>>
): LineSeriesOption => {
return {
type: 'line',
areaStyle: { color: areaColor },
showSymbol: false,
lineStyle: { width: 0 },
emphasis: { focus: "self" },
...baseOption,
}
}

function generateOption(bizOption: BizOption): EcOption {
const { dateRange, rows, timeFormat } = bizOption || {}

const [start, end] = dateRange || []
const allDates = getAllDatesBetween(start, end)
const focusMap = groupBy(rows, r => r.date, l => sum(l.map(e => e.focus)))
const visitMap = groupBy(rows, r => r.date, l => sum(l.map(e => e.time)))
const siteMap = groupBy(rows, r => r.date, l => new Set(l.map(e => e.host)).size)
const countData = allDates.map(date => ({ date, value: siteMap[date] ?? 0 }))
const visitData = allDates.map(date => ({ date, value: visitMap[date] ?? 0 }))
const focusData = allDates.map(date => ({ date, value: focusMap[date] ?? 0 }))
return {
title: generateTitleOption(t(msg => msg.habit.site.trend.title)),
grid: {
bottom: '2%',
top: '26%',
right: 0,
left: 20,
},
series: [
lineOptionOf(COUNT_COLORS, {
name: COUNT_LEGEND,
data: countData,
yAxisIndex: 0
}),
lineOptionOf(VISIT_COLORS, {
name: VISIT_LEGEND,
data: visitData,
yAxisIndex: 1
}),
lineOptionOf(FOCUS_COLORS, {
name: FOCUS_LEGEND,
data: focusData,
yAxisIndex: 2
}),
],
tooltip: {
trigger: 'axis',
axisPointer: { type: "line" },
formatter: (params: any[]) => formatTimeTooltip(params, timeFormat),
},
xAxis: {
type: 'category',
data: allDates,
axisLabel: { show: false },
axisTick: { show: false },
axisLine: { show: false },
},
yAxis: [
valueYAxis(),
valueYAxis(),
valueYAxis(),
],
legend: {
right: '2%',
top: '16%',
icon: 'roundRect',
itemWidth: 20,
itemGap: 5,
textStyle: {
// Hide text
fontSize: 0,
},
tooltip: {
show: true,
formatter: (params: any) => (params?.name as string),
},
data: [{
name: COUNT_LEGEND,
itemStyle: { color: COUNT_COLORS },
}, {
name: VISIT_LEGEND,
itemStyle: { color: VISIT_COLORS },
}, {
name: FOCUS_LEGEND,
itemStyle: { color: FOCUS_COLORS },
}]
},
}
}

export default class Wrapper extends EchartsWrapper<BizOption, EcOption> {
generateOption = generateOption

protected rewrite(): boolean {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import { StyleValue, computed, defineComponent } from "vue"
import { useRows } from "./context"
import { useHabitFilter } from "../context"
import { useEcharts } from "@hooks"
import TimelineWrapper, { BizOption } from "./TimelineWrapper"
import TimelineWrapper, { BizOption } from "./Wrapper"
import { useHabitFilter } from "../../context"
import { useRows } from "../context"

const CONTAINER_STYLE: StyleValue = {
width: "100%",
Expand All @@ -20,11 +20,10 @@ const _default = defineComponent(() => {
const rows = useRows()
const filter = useHabitFilter()
const bizOption = computed<BizOption>(() => {
const dateRange = filter.value?.dateRange
return {
rows: rows.value,
timeFormat: filter.value?.timeFormat,
dateRange,
dateRange: filter.value?.dateRange,
}
})
const { elRef } = useEcharts(TimelineWrapper, bizOption, { manual: true })
Expand Down
Loading

0 comments on commit fad7ad9

Please sign in to comment.