Skip to content

Commit

Permalink
feat(all): support world chart type
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysSullivan committed Feb 23, 2024
1 parent 6294b6a commit 7371ac6
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/docs",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"scripts": {
"build": "next build",
Expand Down
6 changes: 3 additions & 3 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/nextjs-example",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"type": "module",
"scripts": {
Expand All @@ -14,8 +14,8 @@
"@headlessui/tailwindcss": "^0.2.0",
"@t3-oss/env-nextjs": "^0.7.1",
"@tremor/react": "^3.11.1",
"@typelytics/posthog": "0.1.2",
"@typelytics/tremor": "0.1.2",
"@typelytics/posthog": "0.1.3",
"@typelytics/tremor": "0.1.3",
"dotenv-cli": "^7.3.0",
"next": "14.0.4",
"react": "18.2.0",
Expand Down
6 changes: 6 additions & 0 deletions examples/nextjs/src/app/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use client";
import { Chart } from "@typelytics/tremor";

export function Client(props: any) {
return <Chart {...props} />;
}
5 changes: 3 additions & 2 deletions examples/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Chart } from "@typelytics/tremor";
import { events } from "~/data/events";
import { PostHog } from "@typelytics/posthog";
import { DashboardExample } from "./dashboard";
import { Client } from "./client";

export default async function PageViewLineChart() {
const posthog = new PostHog({
Expand All @@ -18,7 +19,7 @@ export default async function PageViewLineChart() {
label: "Page View",
})
.execute({
type: "line",
type: "world",
});

const questionsSolved = await posthog.query().addSeries("Solved Question", {
Expand All @@ -31,5 +32,5 @@ export default async function PageViewLineChart() {
label: "Page View",
});

return <Chart {...pageViews} />;
return <Client {...pageViews} />;
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/core",
"version": "0.1.2",
"version": "0.1.3",
"author": "Rhys Sullivan",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type NumberChartType = "number";
export type PieChartType = "pie";
export type BarTotalChartType = "bar-total";
export type TableChartType = "table";
// export type WorldChartType = "world";
export type WorldChartType = "world";

export type ChartType =
| LineChartType
Expand All @@ -16,8 +16,8 @@ export type ChartType =
| NumberChartType
| PieChartType
| BarTotalChartType
| TableChartType;
// | WorldChartType;
| TableChartType
| WorldChartType;

export type TimeSeriesChartTypes =
| LineChartType
Expand Down
4 changes: 2 additions & 2 deletions packages/posthog-generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/posthog-generator",
"version": "0.1.2",
"version": "0.1.3",
"author": "Rhys Sullivan",
"license": "MIT",
"type": "module",
Expand All @@ -24,6 +24,6 @@
},
"dependencies": {
"@clack/prompts": "^0.7.0",
"@typelytics/posthog": "0.1.2"
"@typelytics/posthog": "0.1.3"
}
}
4 changes: 2 additions & 2 deletions packages/posthog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/posthog",
"version": "0.1.2",
"version": "0.1.3",
"author": "Rhys Sullivan",
"license": "MIT",
"main": "./dist/index.js",
Expand All @@ -24,6 +24,6 @@
"typescript": "^5.0.4"
},
"dependencies": {
"@typelytics/core": "0.1.2"
"@typelytics/core": "0.1.3"
}
}
11 changes: 7 additions & 4 deletions packages/posthog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ type PostHogDisplayType =
| "ActionsBar"
| "ActionsBarValue"
| "ActionsLineGraphCumulative"
| "ActionsAreaGraph";
// | "WorldMap";
| "ActionsAreaGraph"
| "WorldMap";

type FilterLogicalOperator = "AND" | "OR";
type PropertyFilterValue = string | number | (string | number)[] | null;
Expand Down Expand Up @@ -360,7 +360,7 @@ const chartTypeToPostHogType: Record<ChartType, PostHogDisplayType> = {
number: "BoldNumber",
pie: "ActionsPie",
table: "ActionsTable",
// world: "WorldMap",
world: "WorldMap",
} as const;

type AllLabelsOrNames<
Expand Down Expand Up @@ -442,7 +442,10 @@ class PostHogQuery<
options = {
// TODO: Don't reassign in future
...this.config.executionOptions,
...options,
...options
}
if (options.type === 'world') {
options.breakdown = '$geoip_country_code'
}
const toPostHogPropertyFilter = (group: PostHogFilterGroup<string, Events>) => {
const asArray = Array.isArray(group.filters) ? group.filters : [group.filters];
Expand Down
7 changes: 4 additions & 3 deletions packages/tremor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typelytics/tremor",
"version": "0.1.2",
"version": "0.1.3",
"author": "Rhys Sullivan",
"license": "MIT",
"main": "./dist/index.js",
Expand All @@ -26,7 +26,8 @@
},
"dependencies": {
"@tremor/react": "^3.11.1",
"@typelytics/core": "0.1.2",
"react": "18.2.0"
"@typelytics/core": "0.1.3",
"react": "18.2.0",
"react-svg-worldmap": "2.0.0-alpha.16"
}
}
8 changes: 7 additions & 1 deletion packages/tremor/src/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NumberChartType,
PieChartType,
TableChartType,
WorldChartType,
} from "@typelytics/core";
import {
LineChart,
Expand All @@ -27,6 +28,7 @@ import {
Table,
TableProps,
} from "./cumulative";
import { WorldMap, WorldMapProps } from "./world-map";

export function Chart<
const Type extends ChartType,
Expand All @@ -49,7 +51,9 @@ export function Chart<
? TableProps<Labels, IsBreakdown>
: Type extends NumberChartType
? NumberChartProps<Labels, IsBreakdown>
: `!!!${Type} is not supported!`) & { type: Type }
: Type extends WorldChartType
? WorldMapProps<Labels, IsBreakdown>
: `!!!${Type} is not supported!`) & { type: Type }
) {
const type = props.type;
switch (type) {
Expand Down Expand Up @@ -81,6 +85,8 @@ export function Chart<
);
case "table":
return <Table {...(props as TableProps<Labels, IsBreakdown>)} />;
case "world":
return <WorldMap {...(props as WorldMapProps<Labels, IsBreakdown>)} />;
}
throw new Error(`Unknown chart type: ${type}`);
}
22 changes: 22 additions & 0 deletions packages/tremor/src/world-map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Chart, ChartData, WorldChartType } from "@typelytics/core";
import { WorldMap as SVGWorldMap } from "react-svg-worldmap";

export type WorldMapProps<Labels extends string> = Pick<
Chart<WorldChartType, Labels, true>,
"results"
> &
Parameters<typeof SVGWorldMap>[0];
export function WorldMap<Labels extends string>(props: WorldMapProps<Labels>) {
const firstEntry = Object.values(props.results)[0] as Record<
Labels,
ChartData
>;
const formattedWorldMap = Object.keys(firstEntry).map((key) => {
const casted = firstEntry[key as Labels] as ChartData;
return {
country: key,
value: casted?.aggregated_value ?? 0,
};
});
return <SVGWorldMap data={formattedWorldMap} color="blue" />;
}
54 changes: 49 additions & 5 deletions pnpm-lock.yaml

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

0 comments on commit 7371ac6

Please sign in to comment.