Skip to content

Commit 85ff97a

Browse files
committed
Add reverse axis option example
1 parent f9c156d commit 85ff97a

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

examples/reverse/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Chart with reversed axis example</title>
6+
</head>
7+
<body>
8+
<theme-button></theme-button>
9+
<div id="chart-container" class="chart-container"></div>
10+
<div id="chart-container-time" class="chart-container"></div>
11+
<script type="module" src="/examples/reverse/reverse.ts"></script>
12+
</body>
13+
</html>

examples/reverse/reverse.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.chart-container {
2+
width: 100%;
3+
height: 400px;
4+
}

examples/reverse/reverse.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import '@lib/scss/wb-charts.scss'
2+
import '@shared/shared.css'
3+
import './reverse.css'
4+
5+
import '@shared/theme-button'
6+
7+
import { SvgPropertiesHyphen } from 'csstype'
8+
9+
import { AxisIndex, AxisType, CartesianAxes, CartesianAxesOptions, ChartLine } from '@lib'
10+
11+
const exampleData = [
12+
{ valueX: 0, valueY: 0 },
13+
{ valueX: 1, valueY: 1 },
14+
{ valueX: 2, valueY: 4 },
15+
{ valueX: 3, valueY: 9 },
16+
{ valueX: 4, valueY: 16 },
17+
]
18+
19+
const axesOption: CartesianAxesOptions = {
20+
x: [
21+
{
22+
type: AxisType.value,
23+
label: 'x',
24+
showGrid: true,
25+
},
26+
],
27+
y: [
28+
{
29+
type: AxisType.value,
30+
label: 'y',
31+
unit: '[m]',
32+
showGrid: true,
33+
domain: [0, 20],
34+
reverse: true,
35+
},
36+
],
37+
automargin: true,
38+
}
39+
const container = document.getElementById('chart-container')
40+
const axes = new CartesianAxes(container, null, null, axesOption)
41+
42+
const line = new ChartLine(exampleData, {})
43+
const axisIndex: AxisIndex = {
44+
x: { key: 'valueX', axisIndex: 0 },
45+
y: { key: 'valueY', axisIndex: 0 },
46+
}
47+
const chartId = 'example'
48+
const chartStyle: SvgPropertiesHyphen = {
49+
fill: 'none',
50+
stroke: 'skyblue',
51+
'stroke-width': '2',
52+
}
53+
line.addTo(axes, axisIndex, chartId, chartStyle)
54+
55+
axes.redraw({ x: { autoScale: true }, y: { autoScale: true } })
56+
57+
const exampleDataTime = [
58+
{ date: new Date('2025-01-01T12:00Z'), value: -1 },
59+
{ date: new Date('2025-01-02T12:00Z'), value: 1 },
60+
{ date: new Date('2025-01-03T12:00Z'), value: 0 },
61+
{ date: new Date('2025-01-04T12:00Z'), value: 2 },
62+
{ date: new Date('2025-01-05T12:00Z'), value: 1 },
63+
]
64+
65+
const axesOptionsTime: CartesianAxesOptions = {
66+
x: [
67+
{
68+
type: AxisType.time,
69+
label: 'Date',
70+
labelAngle: 45,
71+
position: 'atzero'
72+
},
73+
],
74+
y: [
75+
{
76+
type: AxisType.value,
77+
defaultDomain: [-1.5, 1.5],
78+
showGrid: true,
79+
reverse: true,
80+
},
81+
],
82+
automargin: true,
83+
}
84+
const containerTime = document.getElementById('chart-container-time')
85+
const axesTime = new CartesianAxes(containerTime, null, null, axesOptionsTime)
86+
87+
const lineTime = new ChartLine(exampleDataTime, {})
88+
lineTime.addTo(
89+
axesTime,
90+
{
91+
x: { key: 'date', axisIndex: 0 },
92+
y: { key: 'value', axisIndex: 0 },
93+
},
94+
'example-time',
95+
{
96+
fill: 'none',
97+
stroke: 'red',
98+
'stroke-width': '4',
99+
'stroke-dasharray': '5,5',
100+
},
101+
)
102+
103+
axesTime.redraw({ x: { autoScale: true }, y: { autoScale: true } })

0 commit comments

Comments
 (0)