-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
348 additions
and
71 deletions.
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
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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
import { ChartData } from 'chart.js/auto' | ||
import { ChartData, ChartOptions } from 'chart.js/auto' | ||
import { VNode } from 'vue-demi' | ||
import Line from './line' | ||
import Pie from './pie' | ||
|
||
export type ChartType = 'line' | 'bar' | 'pie' | ||
|
||
const CHART_ADAPTER_MAP: Record<ChartType, (vnode: VNode[]) => ChartData> = { | ||
export interface ChartAdapter { | ||
getStyle: () => ChartOptions, | ||
getDatasets: (vnodes: VNode[]) => ChartData, | ||
} | ||
|
||
const CHART_ADAPTER_MAP: Record<ChartType, ChartAdapter> = { | ||
line: Line, | ||
bar : Line, | ||
pie : Pie, | ||
} | ||
|
||
export default function getDatasets (type: ChartType, vnodes: VNode[]): ChartData { | ||
return CHART_ADAPTER_MAP[type](vnodes) | ||
export function defineAdapter (adapter: ChartAdapter): ChartAdapter { | ||
return adapter | ||
} | ||
|
||
export default function getAdapter (type: ChartType): ChartAdapter { | ||
return CHART_ADAPTER_MAP[type] | ||
} |
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 |
---|---|---|
@@ -1,37 +1,72 @@ | ||
import { ChartDataset, ChartData } from 'chart.js' | ||
import { startCase } from 'lodash' | ||
import { Slots, VNode } from 'vue-demi' | ||
import { defineAdapter } from '.' | ||
import { colorHash } from '../../avatar/utils/color-hash' | ||
import { findAllChildren } from '../../utils/vnode' | ||
|
||
export default function getDatasets (vnodes: VNode[]): ChartData { | ||
const sets = findAllChildren(vnodes, 'ChartSet') | ||
const datasets = new Map<string, ChartDataset>() | ||
const labels = [] as string[] | ||
export default defineAdapter({ | ||
getDatasets (vnodes: VNode[]): ChartData { | ||
const sets = findAllChildren(vnodes, 'ChartSet') | ||
const datasets = new Map<string, ChartDataset>() | ||
const labels = [] as string[] | ||
|
||
for (const set of sets) { | ||
const values = findAllChildren((set.children as Slots).default(), 'ChartVal') | ||
for (const set of sets) { | ||
const values = findAllChildren((set.children as Slots).default(), 'ChartVal') | ||
|
||
for (const value of values) { | ||
const item = datasets.get(value.props.name) | ||
const color = value.props.color ?? colorHash(value.props.name).at(1) | ||
for (const value of values) { | ||
const item = datasets.get(value.props.name) | ||
const color = value.props.color ?? colorHash(value.props.name).at(1) | ||
|
||
if (item) { | ||
item.data.push(value.props.value); | ||
(item.borderColor as string[]).push(color); | ||
(item.backgroundColor as string[]).push(color) | ||
} else { | ||
datasets.set(value.props.name, { | ||
label : startCase(value.props.name), | ||
data : [value.props.value], | ||
borderColor : [color], | ||
backgroundColor: [color], | ||
}) | ||
if (item) { | ||
item.data.push(value.props.value); | ||
(item.borderColor as string[]).push(color); | ||
(item.backgroundColor as string[]).push(color) | ||
} else { | ||
datasets.set(value.props.name, { | ||
label : startCase(value.props.name), | ||
data : [value.props.value], | ||
borderColor : [color], | ||
backgroundColor: [color], | ||
}) | ||
} | ||
} | ||
|
||
labels.push(startCase(set.props.name)) | ||
} | ||
|
||
labels.push(startCase(set.props.name)) | ||
} | ||
return { labels, datasets: [...datasets.values()] } | ||
}, | ||
|
||
return { labels, datasets: [...datasets.values()] } | ||
} | ||
getStyle () { | ||
return { | ||
scales: { | ||
x: { | ||
ticks: { | ||
color: '#9CA3AF', | ||
font : { | ||
family: 'DM Sans', | ||
size : 12, | ||
weight: '600', | ||
}, | ||
}, | ||
grid: { borderColor: '#BFBFBF' }, | ||
}, | ||
y: { | ||
ticks: { | ||
color: '#9CA3AF', | ||
font : { | ||
family: 'DM Sans', | ||
size : 12, | ||
weight: '600', | ||
}, | ||
}, | ||
grid: { | ||
borderColor: '#BFBFBF', | ||
borderDash : [4], | ||
}, | ||
}, | ||
}, | ||
} | ||
}, | ||
}) |
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 |
---|---|---|
@@ -1,35 +1,42 @@ | ||
import { ChartData, ChartDataset } from 'chart.js' | ||
import { startCase } from 'lodash' | ||
import { Slots, VNode } from 'vue-demi' | ||
import { defineAdapter } from '.' | ||
import { colorHash } from '../../avatar/utils/color-hash' | ||
import { findAllChildren } from '../../utils/vnode' | ||
|
||
export default function getDatasets (vnodes: VNode[]): ChartData { | ||
const sets = findAllChildren(vnodes, 'ChartSet') | ||
const datasets = new Map<string, ChartDataset>() | ||
const labels = [] as string[] | ||
export default defineAdapter({ | ||
getDatasets (vnodes: VNode[]): ChartData { | ||
const sets = findAllChildren(vnodes, 'ChartSet') | ||
const datasets = new Map<string, ChartDataset>() | ||
const labels = [] as string[] | ||
|
||
for (const set of sets) { | ||
const values = findAllChildren((set.children as Slots).default(), 'ChartVal') | ||
for (const set of sets) { | ||
const values = findAllChildren((set.children as Slots).default(), 'ChartVal') | ||
|
||
for (const value of values) { | ||
const item = datasets.get(set.props.name) | ||
const color = value.props.color ?? colorHash(value.props.name).at(1) | ||
for (const value of values) { | ||
const item = datasets.get(set.props.name) | ||
const color = value.props.color ?? colorHash(value.props.name).at(1) | ||
|
||
if (item) { | ||
item.data.push(value.props.value); | ||
(item.backgroundColor as string[]).push(color) | ||
} else { | ||
datasets.set(set.props.name, { | ||
label : startCase(set.props.name), | ||
data : [value.props.value], | ||
backgroundColor: [color], | ||
}) | ||
} | ||
if (item) { | ||
item.data.push(value.props.value); | ||
(item.backgroundColor as string[]).push(color) | ||
} else { | ||
datasets.set(set.props.name, { | ||
label : startCase(set.props.name), | ||
data : [value.props.value], | ||
backgroundColor: [color], | ||
}) | ||
} | ||
|
||
labels.push(startCase(value.props.name)) | ||
labels.push(startCase(value.props.name)) | ||
} | ||
} | ||
} | ||
|
||
return { labels, datasets: [...datasets.values()] } | ||
} | ||
return { labels, datasets: [...datasets.values()] } | ||
}, | ||
|
||
getStyle () { | ||
return {} | ||
}, | ||
}) |
Oops, something went wrong.