Skip to content

Commit

Permalink
feat: Add Bar Chart Setup and styles (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier-Charles authored Jan 9, 2024
1 parent 3d46729 commit ba32b90
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 15 deletions.
33 changes: 18 additions & 15 deletions packages/ui-kit/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { IonDatetime, setupIonicReact } from "@ionic/react";
import { Arrow } from "./components/arrow/Arrow";
import { Button } from "./components/buttons/Button";
import { IconButton } from "./components/buttons/IconButton";
import {
// IonDatetime,
setupIonicReact,
} from "@ionic/react";
import { ApexBarChart } from "./components/charts/apexchart";
import {
BarChartSampleOptions,
BarChartSampleSeries,
} from "./components/charts/mockData";

setupIonicReact();

Expand All @@ -11,18 +16,16 @@ function App() {
<h1 className="text-primary text-lg font-semibold">
Vite + React + Ionic + Tailwind
</h1>
<IonDatetime className="mt-10" />
<div className="bg-primary m-10 flex flex-col gap-3 p-10">
<Button variant="small">Primary</Button>
<div className="bg-secondaryOrange">
<p className=" fontGroup-highlightSemi m-4">
Tyring out Prose
</p>
{/* <IonDatetime className="mt-10" /> */}
<div className="custom-charts-widget">
<div className="barchart-styles bg-background w-[600px] h-[500px] flex flex-col gap-3 py-10 border-primary border">
<ApexBarChart
options={BarChartSampleOptions}
series={BarChartSampleSeries}
width="100%"
height="100%"
/>
</div>
<Arrow direction="up" />
<Arrow direction="down" />

<IconButton variant="star" />
</div>
</div>
);
Expand Down
201 changes: 201 additions & 0 deletions packages/ui-kit/src/components/charts/mockData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import moment from "moment";
import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { themeColors } from "src/globalStyles/themes";

type TApexChartWindow = {
globals: {
seriesNames: string[];
};
};

const renderToString = (node: JSX.Element): string => {
const wrapper = document.createElement("div");
const root = createRoot(wrapper);
flushSync(() => root.render(node));
return wrapper.innerHTML;
};

const BarChartData = [
["01-Feb-2024 GMT+1", 14],
["02-Feb-2024 GMT+1", 14],
["03-Feb-2024 GMT+1", 20],
["04-Feb-2024 GMT+1", 16],
["05-Feb-2024 GMT+1", 11],
["06-Feb-2024 GMT+1", 19],
["07-Feb-2024 GMT+1", 14],
["08-Feb-2024 GMT+1", 8],
["09-Feb-2024 GMT+1", 12],
["10-Feb-2024 GMT+1", 20],
["11-Feb-2024 GMT+1", 14],
["12-Feb-2024 GMT+1", 7],
["13-Feb-2024 GMT+1", 6],
["14-Feb-2024 GMT+1", 21],
];

const BarChartOptions = {
chart: {
type: "area",
stacked: true,
zoom: {
enabled: true,
type: "x",
autoScaleYaxis: false,
zoomedArea: {
fill: {
color: themeColors.btnRingVariant400,
opacity: 0.4,
},
stroke: {
color: themeColors.btnRingVariant200,
opacity: 0.4,
width: 1,
},
},
},
toolbar: {
show: false,
},
redrawOnParentResize: true,
},
colors: ["rgba(109, 210, 48, 0.5)", "rgba(109, 210, 48, 0.5)"],
dataLabels: {
enabled: false,
},
stroke: {
curve: "smooth",
width: 1.5,
},
fill: {
type: "gradient",
gradient: {
type: "vertical",
gradientToColors: [themeColors.success, themeColors.success],
shadeIntensity: 1,
opacityFrom: 0.85,
opacityTo: 0.85,
stops: [0, 100],
},
},
legend: {
show: false,
},
xaxis: {
type: "datetime",
tooltip: {
enabled: false,
},
axisTicks: {
show: false,
},
tickPlacement: "between",
tickAmount: Math.min(BarChartData.length, 8) - 1,
labels: {
datetimeUTC: false,
formatter(_val: string, timestamp: number) {
return moment(timestamp).format("DD-MM-YYYY");
},
style: {
colors: themeColors.btnRingVariant500,
fontSize: "10px",
fontFamily: "Arial, sans-serif",
fontWeight: 700,
cssClass: "apexcharts-xaxis-label",
},
},
},
yaxis: {
show: false,
tickAmount: 3,
min: 0,
max: (max: number) => {
return max * 1.001;
},
decimalsInFloat: false,
},
grid: {
borderColor: themeColors.btnRingVariant500,
strokeDashArray: 5,
xaxis: {
lines: {
show: true,
},
},
yaxis: {
lines: {
show: false,
},
},
column: {
colors: themeColors.btnRingVariant500,
opacity: 1,
},
},
tooltip: {
fillSeriesColor: themeColors.white,
title: {
formatter: (seriesName: string) => `$${seriesName}`,
},
y: {
formatter: undefined,
title: {
formatter: (val: string) => {
return renderToString(
<span className="text-white">${val}:</span>
);
},
},
},
custom: ({
series,
seriesIndex,
dataPointIndex,
w,
}: {
series: number[][];
seriesIndex: number;
dataPointIndex: number;
w: TApexChartWindow;
}) => {
return renderToString(
<div className="px-3 py-2 flex flex-col break-word bg-primaryVariant200 fontGroup-support text-primary">
<span className="fontGroup-supportBold [&_span]:fontGroup-support">
{w.globals.seriesNames[0]}: {}
{new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 2,
}).format(series[seriesIndex][dataPointIndex])}
</span>
<span className="pt-[1px]">
{moment(BarChartData[dataPointIndex][0]).format(
"YYYY-MM-DD HH:mm"
)}
</span>
</div>
);
},
},
responsive: [
{
breakpoint: 575,
options: {
chart: {
height: 200,
},
xaxis: {
show: false,
},
},
},
],
};

export const BarChartSampleSeries = [
{
name: "Price",
data: BarChartData,
},
];

export const BarChartSampleOptions = BarChartOptions;
52 changes: 52 additions & 0 deletions packages/ui-kit/src/globalStyles/charts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,55 @@
}
}
}

.custom-charts-widget {
.barchart-styles {
position: relative;
svg {
position: absolute;
left: -5px;
transform: translate(0px, 0px) scale(1.04);
}

.apexcharts-xcrosshairs.apexcharts-active {
opacity: 0;
}

.apexcharts-xaxis-texts-g {
transform: translate(0px, -5px) scale(1.0);
tspan {
fill: theme("colors.primaryVariant200");
}
}

.apexcharts-inner.apexcharts-graphical {
& > line {
stroke: theme("colors.btnRingVariant500");
}

.apexcharts-gridlines-vertical {
line {
stroke: theme("colors.btnRingVariant500");

// hide last grid line in chart because the datalabel is hidden by apexcarts
&:nth-last-of-type(1){
display: none;
}
}

}

// .apexcharts-xaxis-texts-g {
// incase it's needed in the final chart
// shift x-axis labels to the right so that the label is not hidden by the chart
// transform: translate(10px, 0px);
// }
}

.apexcharts-grid-borders{
line {
stroke: theme("colors.btnRingVariant500");
}
}
}
}

0 comments on commit ba32b90

Please sign in to comment.