I'm working with HighCharts ("highcharts-react-official": "^3.2.0")
.
I want to have the xAxis
to split into 6 ticks where each interval equals the time between the first data point and the last data point / 6.
I calculate the tickInterval
like this:
const tickInterval = useMemo(() => {
const { before, after} = multiChartData.reduce(
(result, series, index) => {
series.data.forEach((dataPoint) => {
if (index === 0) {
result.before = dataPoint.x;
result.after = dataPoint.x;
}
if (dataPoint.x < result.before) {
result.before = dataPoint.x;
}
if (dataPoint.x > result.after) {
result.after = dataPoint.x;
}
});
return result;
},
{ before: 0, after: 0 }
);
return (after - before) / 6;
}, [multiChartData]);
Then, I pass the xAxis
settings like this in props:
tickAmount: 6,
xAxis: {
type: 'datetime',
lineWidth: 0,
tickWidth: 0,
labels: {
y: 30,
format: '{value:%b %d, %Y}',
style: {
color: CHART_LABEL_COLOR,
fontSize: FONT_SIZE_13
}
},
tickPixelInterval: null,
tickInterval
}
Yet when my x axis time frame is 30 days the tick interval changes to 2 days even though it should be 5 (30 /6).
I'm working with HighCharts ("highcharts-react-official": "^3.2.0")
.
I want to have the xAxis
to split into 6 ticks where each interval equals the time between the first data point and the last data point / 6.
I calculate the tickInterval
like this:
const tickInterval = useMemo(() => {
const { before, after} = multiChartData.reduce(
(result, series, index) => {
series.data.forEach((dataPoint) => {
if (index === 0) {
result.before = dataPoint.x;
result.after = dataPoint.x;
}
if (dataPoint.x < result.before) {
result.before = dataPoint.x;
}
if (dataPoint.x > result.after) {
result.after = dataPoint.x;
}
});
return result;
},
{ before: 0, after: 0 }
);
return (after - before) / 6;
}, [multiChartData]);
Then, I pass the xAxis
settings like this in props:
tickAmount: 6,
xAxis: {
type: 'datetime',
lineWidth: 0,
tickWidth: 0,
labels: {
y: 30,
format: '{value:%b %d, %Y}',
style: {
color: CHART_LABEL_COLOR,
fontSize: FONT_SIZE_13
}
},
tickPixelInterval: null,
tickInterval
}
Yet when my x axis time frame is 30 days the tick interval changes to 2 days even though it should be 5 (30 /6).
Share Improve this question edited Jan 21 at 14:36 NateDhaliwal 1431 silver badge12 bronze badges asked Jan 21 at 11:46 J JJ J 12 bronze badges 1 |1 Answer
Reset to default 0Based on Magdalena's comment:
I solved this issue by calculating the tickPositions based on the available data.
export const calculateXAxisTickPositions = (
multiChartData: SeriesLineOptions[],
dataPointsAmount: number
): number[] => {
const { before, after } = multiChartData.reduce(
(result, series, index) => {
series.data.forEach((dataPoint, dataIndex) => {
if (index === 0 && dataIndex === 0) {
result.before = dataPoint.x;
result.after = dataPoint.x;
}
if (dataPoint.x < result.before) {
result.before = dataPoint.x;
}
if (dataPoint.x > result.after) {
result.after = dataPoint.x;
}
});
return result;
},
{ before: 0, after: 0 }
);
const range = after - before;
const amountOfTimeSections = dataPointsAmount < 6 ? dataPointsAmount : 6;
const interval = range > 0 ? range / amountOfTimeSections : 0;
return Array.from({ length: 7 }, (_, i) => before + i * interval);
};
The props passed to Highcharts were simplified to:
{
type: 'datetime',
tickPositions: dataPointsAmount > 1 && tickPositions
}
I don't pass the tickPositions in case of 1 data point on the chart as it would place it at the start of the chart making it less readable. So by not having any tick related prop passed in case of 1 point the default behaviour of placing it in the middle is applied which was my preferred view.
tickInterval
,tickPixelInterval
, andtickAmount
can lead to conflicts as these options may override one another. Highcharts might not prioritize the settings in the way you expect. You can also consider using thetickPositions
option, which allows you to define the tick positions. Example: jsfiddle/BlackLabel/u9xef1q3 – magdalena Commented Jan 21 at 17:26