-
Notifications
You must be signed in to change notification settings - Fork 38
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: add 'Color-Coded Heatmap' widget #4629
Merged
yuda110
merged 7 commits into
cloudforet-io:feature-dashboard-september
from
yuda110:feature/color-coded
Sep 2, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baf7998
feat: add `Color-Coded Heatmap` widget
yuda110 8fc3734
feat: add to widget config list
yuda110 3d91f6e
fix(advanced-format-rules): set init value when mounted
yuda110 6143e60
chore: update language
yuda110 b6873f6
chore: delete unused variable
yuda110 116a9a7
chore: add icon
yuda110 1144a07
chore: apply code review
yuda110 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
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
188 changes: 188 additions & 0 deletions
188
apps/web/src/common/modules/widgets/_widgets/color-coded-heatmap/ColorCodedHeatmap.vue
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,188 @@ | ||
<script setup lang="ts"> | ||
import { | ||
computed, defineExpose, reactive, ref, | ||
} from 'vue'; | ||
|
||
import { orderBy } from 'lodash'; | ||
|
||
import { SpaceConnector } from '@cloudforet/core-lib/space-connector'; | ||
import { numberFormatter } from '@cloudforet/utils'; | ||
|
||
import type { ListResponse } from '@/schema/_common/api-verbs/list'; | ||
import type { PrivateWidgetLoadParameters } from '@/schema/dashboard/private-widget/api-verbs/load'; | ||
import type { PublicWidgetLoadParameters } from '@/schema/dashboard/public-widget/api-verbs/load'; | ||
|
||
import type { APIErrorToast } from '@/common/composables/error/errorHandler'; | ||
import ErrorHandler from '@/common/composables/error/errorHandler'; | ||
import WidgetFrame from '@/common/modules/widgets/_components/WidgetFrame.vue'; | ||
import { useWidgetFrame } from '@/common/modules/widgets/_composables/use-widget-frame'; | ||
import { useWidgetInitAndRefresh } from '@/common/modules/widgets/_composables/use-widget-init-and-refresh'; | ||
import { | ||
getApiQueryDateRange, | ||
getReferenceLabel, | ||
getWidgetBasedOnDate, | ||
getWidgetDateRange, | ||
} from '@/common/modules/widgets/_helpers/widget-date-helper'; | ||
import type { DateRange } from '@/common/modules/widgets/types/widget-data-type'; | ||
import type { WidgetEmit, WidgetExpose, WidgetProps } from '@/common/modules/widgets/types/widget-display-type'; | ||
import type { AdvancedFormatRulesValue, GroupByValue } from '@/common/modules/widgets/types/widget-field-value-type'; | ||
|
||
|
||
type Data = ListResponse<{ | ||
[key: string]: string|number; | ||
}>; | ||
|
||
const boxWrapperRef = ref<any|null>(null); | ||
const BOX_MIN_WIDTH = 112; | ||
const MAX_COUNT = 80; | ||
const props = defineProps<WidgetProps>(); | ||
const emit = defineEmits<WidgetEmit>(); | ||
const state = reactive({ | ||
loading: false, | ||
errorMessage: undefined as string|undefined, | ||
data: null as Data | null, | ||
unit: computed<string|undefined>(() => widgetFrameProps.value.unitMap?.[state.dataField]), | ||
boxWidth: computed<number>(() => { | ||
if (!props.width) return BOX_MIN_WIDTH; | ||
const widgetContentWidth = props.width; | ||
if (props.width >= 990) return widgetContentWidth / 10; | ||
return widgetContentWidth / 8 < BOX_MIN_WIDTH ? BOX_MIN_WIDTH : widgetContentWidth / 8; | ||
}), | ||
boxWrapperHeight: computed(() => (boxWrapperRef.value?.scrollHeight <= (30 * 16) ? 'auto' : '30rem')), | ||
refinedData: computed(() => { | ||
if (!state.data) return []; | ||
const _orderedData = orderBy(state.data.results, [state.dataField], ['desc']); | ||
return _orderedData.map((d) => ({ | ||
name: d[state.groupByField], | ||
value: numberFormatter(d[state.dataField], { minimumFractionDigits: 2 }), | ||
color: getColor(d[state.formatRulesField], state.formatRulesField), | ||
})); | ||
}), | ||
// required fields | ||
granularity: computed<string>(() => props.widgetOptions?.granularity as string), | ||
dataField: computed<string|undefined>(() => props.widgetOptions?.dataField as string), | ||
basedOnDate: computed(() => getWidgetBasedOnDate(state.granularity, props.dashboardOptions?.date_range?.end)), | ||
groupByField: computed<string|undefined>(() => (props.widgetOptions?.groupBy as GroupByValue)?.value as string), | ||
formatRulesValue: computed<AdvancedFormatRulesValue>(() => props.widgetOptions?.advancedFormatRules as AdvancedFormatRulesValue), | ||
formatRulesField: computed<string|undefined>(() => state.formatRulesValue?.field), | ||
dateRange: computed<DateRange>(() => { | ||
const [_start, _end] = getWidgetDateRange(state.granularity, state.basedOnDate, 1); | ||
return { start: _start, end: _end }; | ||
}), | ||
}); | ||
const { widgetFrameProps, widgetFrameEventHandlers } = useWidgetFrame(props, emit, { | ||
dateRange: computed(() => state.dateRange), | ||
errorMessage: computed(() => state.errorMessage), | ||
widgetLoading: computed(() => state.loading), | ||
noData: computed(() => (state.data ? !state.data.results?.length : false)), | ||
}); | ||
|
||
/* Api */ | ||
const fetchWidget = async (): Promise<Data|APIErrorToast|undefined> => { | ||
if (props.widgetState === 'INACTIVE') return undefined; | ||
try { | ||
const _isPrivate = props.widgetId.startsWith('private'); | ||
const _fetcher = _isPrivate | ||
? SpaceConnector.clientV2.dashboard.privateWidget.load<PrivateWidgetLoadParameters, Data> | ||
: SpaceConnector.clientV2.dashboard.publicWidget.load<PublicWidgetLoadParameters, Data>; | ||
const _queryDateRange = getApiQueryDateRange(state.granularity, state.dateRange); | ||
const res = await _fetcher({ | ||
widget_id: props.widgetId, | ||
query: { | ||
granularity: state.granularity, | ||
start: _queryDateRange.start, | ||
end: _queryDateRange.end, | ||
group_by: [state.groupByField, state.formatRulesField], | ||
fields: { | ||
[state.dataField]: { | ||
key: state.dataField, | ||
operator: 'sum', | ||
}, | ||
}, | ||
// field_group: [state.formatRulesField], | ||
// sort: [{ key: `_total_${state.dataField}`, desc: true }], | ||
page: { start: 1, limit: MAX_COUNT }, | ||
}, | ||
vars: props.dashboardVars, | ||
}); | ||
state.errorMessage = undefined; | ||
return res; | ||
} catch (e: any) { | ||
state.loading = false; | ||
state.errorMessage = e.message; | ||
ErrorHandler.handleError(e); | ||
return ErrorHandler.makeAPIErrorToast(e); | ||
} | ||
}; | ||
|
||
/* Util */ | ||
const loadWidget = async (): Promise<Data|APIErrorToast> => { | ||
state.loading = true; | ||
const res = await fetchWidget(); | ||
if (typeof res === 'function') return res; | ||
state.data = res; | ||
state.loading = false; | ||
return state.data; | ||
}; | ||
const getColor = (val: string, field: string): string => { | ||
const _label = getReferenceLabel(props.allReferenceTypeInfo, field, val); | ||
return state.formatRulesValue.value.find((d) => d.text === _label)?.color ?? state.formatRulesValue.baseColor; | ||
}; | ||
|
||
useWidgetInitAndRefresh({ props, emit, loadWidget }); | ||
defineExpose<WidgetExpose<Data>>({ | ||
loadWidget, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<widget-frame v-bind="widgetFrameProps" | ||
class="color-coded-heatmap" | ||
v-on="widgetFrameEventHandlers" | ||
> | ||
<!--Do not delete div element below. It's defense code for redraw--> | ||
<div class="content-wrapper"> | ||
<div class="box-wrapper" | ||
:style="{'grid-template-columns': `repeat(auto-fill, ${state.boxWidth-4}px)`, | ||
}" | ||
> | ||
<div v-for="(data, idx) in state.refinedData" | ||
:key="`box-${idx}`" | ||
v-tooltip.bottom="`${data.name}: ${data.value}`" | ||
class="value-box" | ||
:style="{'background-color': data.color}" | ||
> | ||
<span class="value-text">{{ data.name }}</span> | ||
</div> | ||
</div> | ||
</div> | ||
</widget-frame> | ||
</template> | ||
|
||
<style lang="postcss" scoped> | ||
.color-coded-heatmap { | ||
.content-wrapper { | ||
height: 96%; | ||
overflow-y: auto; | ||
} | ||
.box-wrapper { | ||
display: grid; | ||
grid-auto-flow: row; | ||
gap: 1px; | ||
.value-box { | ||
height: 3.125rem; | ||
font-weight: 500; | ||
padding: 0.5rem; | ||
.value-text { | ||
@apply text-label-sm; | ||
display: -webkit-box; | ||
-webkit-box-orient: vertical; | ||
-webkit-line-clamp: 2; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
text-align: center; | ||
} | ||
} | ||
} | ||
} | ||
</style> |
36 changes: 36 additions & 0 deletions
36
apps/web/src/common/modules/widgets/_widgets/color-coded-heatmap/widget-config.ts
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,36 @@ | ||
import { ADVANCED_FORMAT_RULE_TYPE } from '@/common/modules/widgets/_constants/widget-field-constant'; | ||
import type { WidgetConfig } from '@/common/modules/widgets/types/widget-config-type'; | ||
|
||
|
||
const colorCodedHeatmap: WidgetConfig = { | ||
widgetName: 'colorCodedHeatmap', | ||
meta: { | ||
title: 'Color Coded Heatmap', | ||
sizes: ['full'], | ||
defaultValidationConfig: { | ||
defaultMaxCount: 2, | ||
}, | ||
}, | ||
requiredFieldsSchema: { | ||
granularity: {}, | ||
dataField: {}, | ||
groupBy: { | ||
options: { | ||
dataTarget: 'labels_info', | ||
hideCount: true, | ||
defaultIndex: 0, | ||
excludeDateField: true, | ||
}, | ||
}, | ||
advancedFormatRules: { | ||
options: { | ||
formatRulesType: ADVANCED_FORMAT_RULE_TYPE.field, | ||
description: 'COMMON.WIDGETS.ADVANCED_FORMAT_RULES.COLOR_CODED_HEATMAP_DESC', | ||
}, | ||
}, | ||
}, | ||
optionalFieldsSchema: {}, | ||
}; | ||
|
||
|
||
export default colorCodedHeatmap; |
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
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.
I think..
baseColor: props.value?.baseColor...
is better thanbaseColor: state.proxyValue?.baseColor...
.Maybe not working.