Skip to content

Commit

Permalink
Add color picker to display settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rossgray committed Oct 30, 2024
1 parent 6e8bb94 commit 1f89827
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
5 changes: 5 additions & 0 deletions frontend/src/lib/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export function getSeriesColor(index: number = 0): string {
return getColorVar(`data-${dataColorVars[adjustedIndex]}`)
}

/** Returns all color options for series */
export function getSeriesColorPalette(): string[] {
return dataColorVars.map((colorVar) => getColorVar(`data-${colorVar}`))
}

/** Return the background color for the given series index. */
export function getSeriesBackgroundColor(index: number): string {
return `${getSeriesColor(index)}30`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const LineGraph = (): JSX.Element => {
const data: ChartData = {
labels: xData.data,
datasets: yData.map(({ data, settings }, index) => {
const color = getSeriesColor(index)
const color = settings?.display?.color ?? getSeriesColor(index)
const backgroundColor = isAreaChart ? hexToRGBA(color, 0.5) : color

const graphType = getGraphType(visualizationType, settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ const DEFAULT_PICKER_COLORS = [
export const ColorPickerButton = ({
color,
onColorSelect: propOnColorSelect,
colorChoices = DEFAULT_PICKER_COLORS,
}: {
color: string
onColorSelect?: (color: string) => void
colorChoices?: string[]
}): JSX.Element => {
const [pickerOpen, setPickerOpen] = useState(false)
const { isDarkModeOn } = useValues(themeLogic)
Expand All @@ -35,14 +37,12 @@ export const ColorPickerButton = ({
propOnColorSelect(colorResult.hex)
}

if (DEFAULT_PICKER_COLORS.includes(colorResult.hex)) {
if (colorChoices.includes(colorResult.hex)) {
setPickerOpen(false)
}
}

const colors = isDarkModeOn
? DEFAULT_PICKER_COLORS.map((n) => RGBToHex(lightenDarkenColor(n, -30)))
: DEFAULT_PICKER_COLORS
const colors = isDarkModeOn ? colorChoices.map((n) => RGBToHex(lightenDarkenColor(n, -30))) : colorChoices

return (
<Popover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import {
} from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { Form } from 'kea-forms'
import { getSeriesColor } from 'lib/colors'
import { getSeriesColor, getSeriesColorPalette } from 'lib/colors'
import { SeriesGlyph } from 'lib/components/SeriesGlyph'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { hexToRGBA, lightenDarkenColor, RGBToRGBA } from 'lib/utils'

import { themeLogic } from '~/layout/navigation-3000/themeLogic'

import { AxisSeries, dataVisualizationLogic } from '../dataVisualizationLogic'
import { ColorPickerButton } from './ColorPickerButton'
import { ySeriesLogic, YSeriesLogicProps, YSeriesSettingsTab } from './ySeriesLogic'

export const SeriesTab = (): JSX.Element => {
Expand Down Expand Up @@ -98,7 +99,7 @@ const YSeries = ({ series, index }: { series: AxisSeries<number>; index: number
const { setSettingsOpen, submitFormatting, submitDisplay, setSettingsTab } = useActions(seriesLogic)

const { isDarkModeOn } = useValues(themeLogic)
const seriesColor = getSeriesColor(index)
const seriesColor = series.settings?.display?.color ?? getSeriesColor(index)

const columnsInOptions = showTableSettings ? columns : numericalColumns
const options = columnsInOptions.map(({ name, type }) => ({
Expand Down Expand Up @@ -219,9 +220,20 @@ const YSeriesDisplayTab = ({ ySeriesLogicProps }: { ySeriesLogicProps: YSeriesLo

return (
<Form logic={ySeriesLogic} props={ySeriesLogicProps} formKey="display" className="space-y-4">
<LemonField name="label" label="Label">
<LemonInput />
</LemonField>
<div className="flex gap-3">
<LemonField name="color" label="Color">
{({ value, onChange }) => (
<ColorPickerButton
color={value}
onColorSelect={onChange}
colorChoices={getSeriesColorPalette()}
/>
)}
</LemonField>
<LemonField name="label" label="Label">
<LemonInput />
</LemonField>
</div>
{!showTableSettings && (
<>
<LemonField name="trendLine" label="Trend line">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import { getSeriesColor } from 'lib/colors'

import {
AxisSeries,
Expand Down Expand Up @@ -75,6 +76,7 @@ export const ySeriesLogic = kea<ySeriesLogicType>([
},
display: {
defaults: {
color: props.series?.settings?.display?.color ?? getSeriesColor(props.seriesIndex),
label: props.series?.settings?.display?.label ?? '',
trendLine: props.series?.settings?.display?.trendLine ?? false,
yAxisPosition: props.series?.settings?.display?.yAxisPosition ?? 'left',
Expand All @@ -83,6 +85,7 @@ export const ySeriesLogic = kea<ySeriesLogicType>([
submit: async (display) => {
actions.updateSeriesIndex(props.seriesIndex, props.series.column.name, {
display: {
color: display.color,
label: display.label,
trendLine: display.trendLine,
yAxisPosition: display.yAxisPosition,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ export interface ChartSettingsFormatting {
}

export interface ChartSettingsDisplay {
color?: string
label?: string
trendLine?: boolean
yAxisPosition?: 'left' | 'right'
Expand Down

0 comments on commit 1f89827

Please sign in to comment.