Skip to content
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(insights): add % of total option to line, bar and area trend insights (fixes #13728, #11276) #16699

Merged
merged 21 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
44e5cfc
show percentage stack view for bar graph
Arun-chaitanya Jul 20, 2023
ad8baa0
add percent stack view for bar graph
Arun-chaitanya Jul 20, 2023
febcb4f
Merge branch 'PostHog:master' into master
Arun-chaitanya Jul 21, 2023
d9533ec
constant datalabel when stacked100 is enabled
Arun-chaitanya Jul 21, 2023
b143b61
ternary operation instead of if else
Arun-chaitanya Jul 21, 2023
20acc84
Merge branch 'PostHog:master' into master
Arun-chaitanya Jul 23, 2023
6d2b738
adding percentStackView to Line and area graph
Arun-chaitanya Jul 23, 2023
9564947
Y-axis label, hiding unitsBox for percentStackView
Arun-chaitanya Jul 23, 2023
60a247a
removing from stickiness graph
Arun-chaitanya Jul 23, 2023
d60f811
PR comment related changes
Arun-chaitanya Jul 26, 2023
7e8a513
renaming formatYAxisLabel to formatPercentStackAxisValue
Arun-chaitanya Jul 26, 2023
022abaa
renaming to formatPercentStackAxisValue
Arun-chaitanya Jul 26, 2023
ccee100
persisting the checkbox value after saving insight
Arun-chaitanya Jul 26, 2023
c2e1f08
passing true false to checkbox
Arun-chaitanya Jul 26, 2023
ed84b5d
line graph stacked not working bug solved
Arun-chaitanya Jul 27, 2023
214b507
review amendmends
thmsobrmlr Jul 28, 2023
1cc2889
Merge branch 'master' into Arun-chaitanya/master
thmsobrmlr Jul 28, 2023
3540866
update schema
thmsobrmlr Jul 28, 2023
650fe0b
update visual regression tests
thmsobrmlr Jul 28, 2023
27cfe97
update more snapshots
thmsobrmlr Jul 28, 2023
e000cab
update more snapshots
thmsobrmlr Jul 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ChartFilter } from 'lib/components/ChartFilter'
import { FunnelDisplayLayoutPicker } from 'scenes/insights/views/Funnels/FunnelDisplayLayoutPicker'
import { FunnelBinsPicker } from 'scenes/insights/views/Funnels/FunnelBinsPicker'
import { ValueOnSeriesFilter } from 'scenes/insights/EditorFilters/ValueOnSeriesFilter'
import { PercentStackView } from 'scenes/insights/EditorFilters/PercentStackView'

interface InsightDisplayConfigProps {
disableTable: boolean
Expand All @@ -28,6 +29,7 @@ export function InsightDisplayConfig({ disableTable }: InsightDisplayConfigProps
disableDateRange,
showCompare,
showValueOnSeries,
showPercentStackView,
showUnit,
showChart,
showInterval,
Expand Down Expand Up @@ -83,6 +85,12 @@ export function InsightDisplayConfig({ disableTable }: InsightDisplayConfigProps
<ValueOnSeriesFilter />
</ConfigFilter>
)}

{showPercentStackView && (
<ConfigFilter>
<PercentStackView />
</ConfigFilter>
)}
</div>
<div className="flex items-center space-x-4 flex-wrap my-2 grow justify-end">
{showUnit && (
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/queries/nodes/InsightViz/insightDisplayConfigLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ export const insightDisplayConfigLogic = kea<insightDisplayConfigLogicType>([
}
},
],
showPercentStackView: [
(s) => [s.isTrends, s.isStickiness, s.isLifecycle, s.display],
(isTrends, isStickiness, isLifecycle, display) => {
if (isTrends || isStickiness) {
return display === ChartDisplayType.ActionsBar
} else if (isLifecycle) {
return true
} else {
return false
}
Arun-chaitanya marked this conversation as resolved.
Show resolved Hide resolved
},
],
showUnit: [(s) => [s.supportsDisplay, s.isTrends], (supportsDisplay, isTrends) => supportsDisplay && isTrends],
showChart: [(s) => [s.supportsDisplay], (supportsDisplay) => supportsDisplay],
showInterval: [
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/queries/nodes/InsightViz/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ export const getShowValueOnSeries = (query: InsightQueryNode): boolean | undefin
}
}

export const getShowPercentStackView = (query: InsightQueryNode): boolean | undefined => {
if (isLifecycleQuery(query)) {
return query.lifecycleFilter?.show_percent_stack_view
} else if (isStickinessQuery(query)) {
return query.stickinessFilter?.show_percent_stack_view
} else if (isTrendsQuery(query)) {
return query.trendsFilter?.show_percent_stack_view
} else {
return undefined
}
}

export const getCachedResults = (
cachedInsight: Partial<InsightModel> | undefined | null,
query: InsightQueryNode
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/scenes/insights/EditorFilters/PercentStackView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useActions, useValues } from 'kea'
import { LemonCheckbox } from 'lib/lemon-ui/LemonCheckbox'
import { insightLogic } from '../insightLogic'
import { percentStackViewLogic } from './percentStackViewLogic'

export function PercentStackView(): JSX.Element {
const { insightProps } = useValues(insightLogic)
const { showPercentStackView } = useValues(percentStackViewLogic(insightProps))
const { setShowPercentStackView } = useActions(percentStackViewLogic(insightProps))

return (
<LemonCheckbox
checked={showPercentStackView}
onChange={setShowPercentStackView}
label={<span className="font-normal">Show 100% Stacked</span>}
bordered
size="small"
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { actions, connect, kea, key, listeners, path, props, selectors } from 'kea'
import { InsightLogicProps, TrendsFilterType } from '~/types'
import { insightVizDataLogic } from '../insightVizDataLogic'
import { keyForInsightLogicProps } from '../sharedUtils'

import type { percentStackViewLogicType } from './percentStackViewLogicType'

export const percentStackViewLogic = kea<percentStackViewLogicType>([
props({} as InsightLogicProps),
key(keyForInsightLogicProps('new')),
path((key) => ['scenes', 'insights', 'EditorFilters', 'percentStackViewLogic', key]),

connect((props: InsightLogicProps) => ({
values: [insightVizDataLogic(props), ['isTrends', 'isStickiness', 'isLifecycle', 'insightFilter']],
actions: [insightVizDataLogic(props), ['updateInsightFilter']],
})),

actions({
setShowPercentStackView: (checked: boolean) => ({ checked }),
}),

selectors({
showPercentStackView: [
(s) => [s.isTrends, s.isStickiness, s.isLifecycle, s.insightFilter],
(isTrends, isStickiness, isLifecycle, insightFilter) => {
return !!(
(isTrends || isStickiness || isLifecycle) &&
(insightFilter as TrendsFilterType)?.show_percent_stack_view
Arun-chaitanya marked this conversation as resolved.
Show resolved Hide resolved
)
},
],
}),

listeners(({ actions }) => ({
setShowPercentStackView: ({ checked }) => {
actions.updateInsightFilter({ show_percent_stack_view: checked })
},
})),
])
2 changes: 2 additions & 0 deletions frontend/src/scenes/insights/insightVizDataLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
getInterval,
getSeries,
getShownAs,
getShowPercentStackView,
getShowValueOnSeries,
} from '~/queries/nodes/InsightViz/utils'
import { DISPLAY_TYPES_WITHOUT_LEGEND } from 'lib/components/InsightLegend/utils'
Expand Down Expand Up @@ -116,6 +117,7 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
samplingFactor: [(s) => [s.querySource], (q) => (q ? q.samplingFactor : null)],
shownAs: [(s) => [s.querySource], (q) => (q ? getShownAs(q) : null)],
showValueOnSeries: [(s) => [s.querySource], (q) => (q ? getShowValueOnSeries(q) : null)],
showPercentStackView: [(s) => [s.querySource], (q) => (q ? getShowPercentStackView(q) : undefined)],

insightFilter: [(s) => [s.querySource], (q) => (q ? filterForQuery(q) : null)],
trendsFilter: [(s) => [s.querySource], (q) => (isTrendsQuery(q) ? q.trendsFilter : null)],
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/scenes/insights/views/LineGraph/LineGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { themeLogic } from '~/layout/navigation-3000/themeLogic'
import { SeriesLetter } from 'lib/components/SeriesGlyph'
import { TrendsFilter } from '~/queries/schema'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import ChartjsPluginStacked100 from 'chartjs-plugin-stacked100'

export function ensureTooltipElement(): HTMLElement {
let tooltipEl = document.getElementById('InsightTooltipWrapper')
Expand Down Expand Up @@ -211,6 +212,7 @@ export interface LineGraphProps {
formula?: string | null
compare?: boolean | null
showValueOnSeries?: boolean | null
showPercentStackView?: boolean
}

export const LineGraph = (props: LineGraphProps): JSX.Element => {
Expand Down Expand Up @@ -239,6 +241,7 @@ export function LineGraph_({
trendsFilter,
formula,
showValueOnSeries,
showPercentStackView,
}: LineGraphProps): JSX.Element {
let datasets = _datasets

Expand Down Expand Up @@ -371,6 +374,7 @@ export function LineGraph_({
},
},
plugins: {
stacked100: { enable: showPercentStackView && type === GraphType.Bar, precision: 1 },
datalabels: {
color: 'white',
anchor: (context) => {
Expand Down Expand Up @@ -614,7 +618,7 @@ export function LineGraph_({
}
options.indexAxis = 'y'
}

Chart.register(ChartjsPluginStacked100)
const newChart = new Chart(canvasRef.current?.getContext('2d') as ChartItem, {
type: (isBar ? GraphType.Bar : type) as ChartType,
data: { labels, datasets },
Expand Down
1 change: 1 addition & 0 deletions frontend/src/scenes/trends/trendsDataLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const trendsDataLogic = kea<trendsDataLogicType>([
'breakdown',
'shownAs',
'showValueOnSeries',
'showPercentStackView',
'trendsFilter',
'lifecycleFilter',
'isTrends',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/scenes/trends/viz/ActionsLineGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function ActionsLineGraph({
interval,
shownAs,
showValueOnSeries,
showPercentStackView,
trendsFilter,
isLifecycle,
isStickiness,
Expand All @@ -46,6 +47,7 @@ export function ActionsLineGraph({
trendsFilter={trendsFilter}
formula={formula}
showValueOnSeries={showValueOnSeries}
showPercentStackView={showPercentStackView}
tooltip={
isLifecycle
? {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ export interface TrendsFilterType extends FilterType {
shown_as?: ShownAsValue
display?: ChartDisplayType
show_values_on_series?: boolean
show_percent_stack_view?: boolean
breakdown_histogram_bin_count?: number // trends breakdown histogram bin count
}
export interface StickinessFilterType extends FilterType {
Expand All @@ -1664,6 +1665,7 @@ export interface StickinessFilterType extends FilterType {
shown_as?: ShownAsValue
display?: ChartDisplayType
show_values_on_series?: boolean
show_percent_stack_view?: boolean
Arun-chaitanya marked this conversation as resolved.
Show resolved Hide resolved
}
export interface FunnelsFilterType extends FilterType {
funnel_viz_type?: FunnelVizType // parameter sent to funnels API for time conversion code path
Expand Down Expand Up @@ -1720,6 +1722,7 @@ export interface RetentionFilterType extends FilterType {
export interface LifecycleFilterType extends FilterType {
shown_as?: ShownAsValue
show_values_on_series?: boolean
show_percent_stack_view?: boolean
Arun-chaitanya marked this conversation as resolved.
Show resolved Hide resolved
toggledLifecycles?: LifecycleToggle[]
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"chartjs-adapter-dayjs-3": "^1.2.3",
"chartjs-plugin-crosshair": "^1.2.0",
"chartjs-plugin-datalabels": "^2.2.0",
"chartjs-plugin-stacked100": "^1.4.0",
"chokidar": "^3.5.3",
"clsx": "^1.1.1",
"core-js": "3.15.2",
Expand Down
15 changes: 13 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.