-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Update bar-sparkline playground example
- Loading branch information
1 parent
810ffb3
commit 38e4fb9
Showing
2 changed files
with
98 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,97 @@ | ||
<template> | ||
<LumeBarChart v-bind="$props" /> | ||
<lume-chart | ||
v-bind="$props" | ||
:options="options" | ||
:style="{ width: `${width}px` }" | ||
class-list="bar-sparkline" | ||
> | ||
<template #groups="groupProps"> | ||
<lume-bar-group v-bind="groupProps" /> | ||
|
||
<g> | ||
<rect | ||
v-for="(_l, index) in groupProps.labels.length" | ||
v-bind="getBarCapProps(index, groupProps)" | ||
:key="`cap_${index}`" | ||
/> | ||
</g> | ||
</template> | ||
</lume-chart> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
<script setup lang="ts"> | ||
import { computed, toRefs } from 'vue'; | ||
import LumeBarChart from '@/components/charts/lume-bar-chart'; | ||
import LumeChart from '@/components/core/lume-chart'; | ||
import LumeBarGroup from '@/components/groups/lume-bar-group'; | ||
import { withChartProps } from '@/composables/props'; | ||
export default defineComponent({ | ||
components: { LumeBarChart }, | ||
props: { | ||
...withChartProps(), | ||
import type { ChartOptions } from '@/types/options'; | ||
const props = defineProps({ | ||
...withChartProps(), | ||
barWidth: { | ||
type: Number, | ||
required: true, | ||
}, | ||
setup() { | ||
return {}; | ||
gap: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}); | ||
const { barWidth, gap } = toRefs(props); | ||
const barCount = computed(() => props.data[0].values.length); | ||
const width = computed( | ||
() => barCount.value * barWidth.value + (barCount.value - 1) * gap.value | ||
); | ||
const options = computed<ChartOptions>(() => ({ | ||
margins: { top: 0, right: 0, bottom: 0, left: 0 }, | ||
noMinSize: true, | ||
paddingInner: gap.value / (barWidth.value + gap.value), | ||
paddingOuter: 0, | ||
startOnZero: true, | ||
withAxes: false, | ||
withLegend: false, | ||
})); | ||
function getBarCapProps(index: number, { data, xScale, yScale }) { | ||
const values = data[0].values; | ||
return { | ||
width: barWidth.value, | ||
height: 2, | ||
x: xScale(index), | ||
y: yScale(values[index].value), | ||
class: [ | ||
'bar-sparkline__cap', | ||
values[index].value > 70 | ||
? 'bar-sparkline__cap--red' | ||
: 'bar-sparkline__cap--green', | ||
], | ||
}; | ||
} | ||
</script> | ||
|
||
<style lang="scss"></style> | ||
<style lang="scss"> | ||
.bar-sparkline { | ||
opacity: 0.3; | ||
transition: opacity 0.1s ease-in-out !important; | ||
&:hover { | ||
opacity: 0.5; | ||
} | ||
&__cap { | ||
&--green { | ||
fill: var(--lume-color--green); | ||
} | ||
&--red { | ||
fill: var(--lume-color--red); | ||
} | ||
} | ||
} | ||
</style> |