Skip to content

Commit

Permalink
feat: isThreshold as part of LegendItem [MA-3442]
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-peteu committed Dec 4, 2024
1 parent 6008472 commit e6ee577
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@
ref="legendItemsRef"
@click="handleLegendItemClick(datasetIndex, index)"
>
<template v-if="text !== i18n.t('chartLabels.threshold')">
<div
class="square-marker"
:style="{ background: fillStyle, 'border-color': strokeStyle }"
/>
<div
class="label-container"
:class="{ 'strike-through': !isDatasetVisible(datasetIndex, index) }"
>
<div
class="square-marker"
:style="{ background: fillStyle, 'border-color': strokeStyle }"
/>
class="label"
:class="{ 'truncate-label' : shouldTruncate }"
:title="text"
>
{{ text }}
</div>
<div
class="label-container"
:class="{ 'strike-through': !isDatasetVisible(datasetIndex, index) }"
v-if="value && showValues"
class="sub-label"
>
<div
class="label"
:class="{ 'truncate-label' : shouldTruncate }"
:title="text"
>
{{ text }}
</div>
<div
v-if="value && showValues"
class="sub-label"
>
{{ value.formatted }}
</div>
{{ value.formatted }}
</div>
</template>
</div>
</li>
</ul>
</template>
Expand All @@ -46,9 +44,6 @@ import { Chart, type LegendItem } from 'chart.js'
import { inject, onBeforeUnmount, onMounted, ref, watch, type PropType, computed } from 'vue'
import { KUI_SPACE_100, KUI_SPACE_80, KUI_SPACE_110 } from '@kong/design-tokens'
import { debounce } from '../../utils'
import composables from '../../composables'
const { i18n } = composables.useI18n()
const props = defineProps({
id: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ const htmlLegendPlugin = {
// @ts-ignore - ChartJS types are incomplete
legendItems.value = chart.options.plugins.legend.labels.generateLabels(chart)
.map(e => ({ ...e, value: props.legendValues && props.legendValues[e.text] }))
.filter(e => !e.value.isThreshold)
.sort(props.chartLegendSortFn)
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ export default function useChartLegendValues(chartData: Ref<KChartData>, chartTy
})) || `${approxNum(raw, { capital: true, ...(metricUnit.value === 'usd' && { prefix: '$' }) })} ${metricUnit.value}`
}

return { ...a, [v.label as string]: { raw, formatted } }
return {
...a,
[v.label as string]: {
raw,
formatted,
isThreshold: v.isThreshold,
},
}
}, {})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export default function useExploreResultToTimeDataset(
datasets.push({
type: 'line',
rawMetric: key,
isThreshold: true,
label: i18n.t('chartLabels.threshold'),
borderColor: KUI_COLOR_BACKGROUND_NEUTRAL,
borderWidth: 1.25,
Expand Down
11 changes: 9 additions & 2 deletions packages/analytics/analytics-chart/src/types/chart-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import type { ChartType, SimpleChartType } from './chart-types'
import type { ExploreAggregations } from '@kong-ui-public/analytics-utilities'

// Chart.js extendend interfaces
export type Dataset = ChartDataset & { rawDimension: string, rawMetric?: string, total?: number, lineTension?: number, fill?: boolean }
export type Dataset = ChartDataset & { rawDimension: string,
rawMetric?: string,
total?: number,
lineTension?: number,
fill?: boolean,
isThreshold?: boolean
}

export interface KChartData extends ChartData {
datasets: Dataset[]
Expand All @@ -32,7 +38,8 @@ export interface AnalyticsChartColors {

export interface LegendValueEntry {
raw: number,
formatted: string
formatted: string,
isThreshold?: boolean,
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ const dashboardConfig: DashboardConfig = {
chart: {
type: 'timeseries_line',
chartTitle: 'Timeseries line chart of mock data',
threshold: { 'request_count': 3200 } as Record<ExploreAggregations, number>,
threshold: {
'request_count': 3200,
} as Record<ExploreAggregations, number>,
},
query: {
datasource: 'basic',
Expand Down

0 comments on commit e6ee577

Please sign in to comment.