Skip to content

Commit

Permalink
✨ (slope) add support for entity name annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Nov 21, 2024
1 parent 8e6546e commit 60d5738
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Time,
SeriesStrategy,
EntityName,
PrimitiveType,
} from "@ourworldindata/types"
import { ChartInterface } from "../chart/ChartInterface"
import { ChartManager } from "../chart/ChartManager"
Expand Down Expand Up @@ -613,6 +614,28 @@ export class SlopeChart
}
}

// todo: for now just works with 1 y column
@computed private get annotationsMap(): Map<
PrimitiveType,
Set<PrimitiveType>
> {
return this.inputTable
.getAnnotationColumnForColumn(this.yColumnSlugs[0])
?.getUniqueValuesGroupedBy(this.inputTable.entityNameSlug)
}

private getAnnotationsForSeries(
seriesName: SeriesName
): string | undefined {
const annotationsMap = this.annotationsMap
const annos = annotationsMap?.get(seriesName)
return annos
? Array.from(annos.values())
.filter((anno) => anno)
.join(" & ")
: undefined
}

private getColorKey(
entityName: EntityName,
columnName: string,
Expand Down Expand Up @@ -671,10 +694,13 @@ export class SlopeChart
)
)

const annotation = this.getAnnotationsForSeries(seriesName)

return {
seriesName,
color,
values: sortedValues,
annotation,
} as SlopeChartSeries
})
.filter((series) => series.values.length >= 2)
Expand Down Expand Up @@ -792,11 +818,12 @@ export class SlopeChart

@computed get labelSeries(): LineLabelSeries[] {
return this.series.map((series) => {
const { seriesName, color, values } = series
const { seriesName, color, values, annotation } = series
return {
color,
seriesName,
label: seriesName,
annotation,
yValue: values[1].y,
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface SlopeChartValue {
export interface SlopeChartSeries extends ChartSeries {
size: number
values: SlopeChartValue[]
annotation?: string
}

export const DEFAULT_SLOPE_CHART_COLOR = "#ff7f0e"
Expand Down

0 comments on commit 60d5738

Please sign in to comment.