Skip to content

Commit

Permalink
use band scale instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas committed Mar 4, 2025
1 parent 87a6b94 commit f8b984a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
28 changes: 13 additions & 15 deletions docs/data/charts/axis/OverlappingAxes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as React from 'react';
import { LineChart } from '@mui/x-charts/LineChart';
import { BarChart } from '@mui/x-charts/BarChart';

export default function OverlappingAxes() {
return (
<LineChart
<BarChart
xAxis={[
{
scaleType: 'time',
scaleType: 'band',
data: time,
valueFormatter: formatShortMonth,
height: 0,
tickLabelPlacement: 'middle',
},
{
scaleType: 'time',
scaleType: 'band',
data: time,
tickInterval: time.filter((_, index) => index % 3 === 0),
valueFormatter: formatQuarterYear,
Expand Down Expand Up @@ -57,7 +57,6 @@ const time = [
new Date(2015, 9, 1),
new Date(2015, 10, 1),
new Date(2015, 11, 1),
new Date(2016, 0, 1),
];

const a = [
Expand All @@ -76,19 +75,18 @@ const chartConfig = {
series: [
{
data: getPercents(a),
type: 'line',
label: 'a',
area: true,
stack: 'total',
showMark: false,
label: 'Income',
valueFormatter: (value) => `${(value ?? 0).toFixed(0)}%`,
},
{
data: getPercents(b),
type: 'line',
label: 'b',
area: true,
stack: 'total',
showMark: false,
label: 'Expenses',
valueFormatter: (value) => `${(value ?? 0).toFixed(0)}%`,
},
],
yAxis: [
{
valueFormatter: (value) => `${(value ?? 0).toFixed(0)}%`,
},
],
};
28 changes: 13 additions & 15 deletions docs/data/charts/axis/OverlappingAxes.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as React from 'react';
import { LineChart } from '@mui/x-charts/LineChart';
import { BarChart } from '@mui/x-charts/BarChart';
import { AxisValueFormatterContext } from '@mui/x-charts/models';

export default function OverlappingAxes() {
return (
<LineChart
<BarChart
xAxis={[
{
scaleType: 'time',
scaleType: 'band',
data: time,
valueFormatter: formatShortMonth,
height: 0,
tickLabelPlacement: 'middle',
},
{
scaleType: 'time',
scaleType: 'band',
data: time,
tickInterval: time.filter((_, index) => index % 3 === 0),
valueFormatter: formatQuarterYear,
Expand Down Expand Up @@ -58,7 +58,6 @@ const time = [
new Date(2015, 9, 1),
new Date(2015, 10, 1),
new Date(2015, 11, 1),
new Date(2016, 0, 1),
];
const a = [
4000, 3000, 2000, 2780, 1890, 2390, 3490, 2400, 1398, 9800, 3908, 4800, 2400,
Expand All @@ -75,19 +74,18 @@ const chartConfig = {
series: [
{
data: getPercents(a),
type: 'line',
label: 'a',
area: true,
stack: 'total',
showMark: false,
label: 'Income',
valueFormatter: (value: number | null) => `${(value ?? 0).toFixed(0)}%`,
},
{
data: getPercents(b),
type: 'line',
label: 'b',
area: true,
stack: 'total',
showMark: false,
label: 'Expenses',
valueFormatter: (value: number | null) => `${(value ?? 0).toFixed(0)}%`,
},
],
yAxis: [
{
valueFormatter: (value: number | null) => `${(value ?? 0).toFixed(0)}%`,
},
],
} as const;
5 changes: 4 additions & 1 deletion packages/x-charts/src/hooks/useTicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export function useTicks(
labelOffset:
tickLabelPlacement === 'tick'
? 0
: scale.step() * (offsetRatio[tickLabelPlacement] - offsetRatio[tickPlacement]),
: scale.step() *
(offsetRatio[tickLabelPlacement] - offsetRatio[tickPlacement]) *
// Only really works if ticks are evenly spaced
(domain.length !== filteredDomain.length ? filteredDomain.length - 1 : 1),
})),

...(tickPlacement === 'extremities'
Expand Down

0 comments on commit f8b984a

Please sign in to comment.