Skip to content

Commit 003c232

Browse files
committed
docs: add example of custom date formatting on x axis
1 parent 97f2e0b commit 003c232

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

apps/docs/docs/examples/line-chart.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ This example demonstrates a Material Design Line Chart with default settings.
158158
)}
159159
</ContextProvider>
160160

161+
### Dates in x axis with custom formatting
162+
163+
<ContextProvider>
164+
{({ branch, theme }) => (
165+
<iframe
166+
src={`https://codesandbox.io/embed/github/RakanNimer/react-google-charts/tree/${branch}/sandboxes/line-chart/formatted-date?fontsize=14&hidenavigation=1&module=%2FApp.tsx&theme=${theme}`}
167+
style={{
168+
width: "100%",
169+
height: "500px",
170+
border: 0,
171+
borderRadius: "4px",
172+
overflow: "hidden",
173+
}}
174+
title="Dates in x axis with custom formatting Line Chart"
175+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
176+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
177+
loading="lazy"
178+
></iframe>
179+
)}
180+
</ContextProvider>
181+
161182
### Material Design Dual Y-Axis Chart
162183

163184
This example shows how to create a Material Design Line Chart with dual Y-axes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from "react";
2+
import { Chart } from "react-google-charts";
3+
4+
export const data = [
5+
["Time", "Sales", "Expenses"],
6+
[new Date(2022, 4, 15, 9, 30), 1030, 540],
7+
[new Date(2022, 4, 17, 10, 30), 1000, 400],
8+
[new Date(2022, 5, 13, 11, 30), 1170, 460],
9+
[new Date(2022, 6, 13, 12, 30), 660, 1120],
10+
];
11+
12+
export const options = {
13+
title: "Company Performance",
14+
curveType: "function",
15+
legend: { position: "bottom" },
16+
hAxis: {
17+
title: "Date",
18+
format: "MMM dd, yyyy", // Custom date format
19+
gridlines: { count: 3 }, // Controls the number of gridlines
20+
},
21+
};
22+
23+
export function App() {
24+
return (
25+
<Chart
26+
chartType="LineChart"
27+
width="100%"
28+
height="400px"
29+
data={data}
30+
options={options}
31+
formatters={[
32+
{
33+
column: 0,
34+
type: "DateFormat",
35+
options: {
36+
timeZone: 0,
37+
},
38+
},
39+
]}
40+
/>
41+
);
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from "react";
2+
import { render } from "react-dom";
3+
4+
import { App } from "./App";
5+
6+
const rootElement = document.getElementById("root");
7+
render(<App />, rootElement);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"description": "Basic line chart with formatted time dates on hAxis",
3+
"main": "index.tsx",
4+
"dependencies": {
5+
"react": "17.0.2",
6+
"react-dom": "17.0.2",
7+
"react-google-charts": "*",
8+
"react-scripts": "4.0.3"
9+
},
10+
"devDependencies": {
11+
"@types/react": "17.0.20",
12+
"@types/react-dom": "17.0.9",
13+
"typescript": "4.4.2"
14+
}
15+
}

0 commit comments

Comments
 (0)