How to avoid duplicate dates in x axis in Victory Chart

966 Views Asked by At

I try to draw a graph with Victory Chart, and the main axis values are dates fetched from database. I define my main axis as below

<VictoryAxis
        // main axis styles
        scale={{x: 'time'}}
        style={{
            tickLabels: {
                fontSize: 12,
                fontFamily: 'Nunito',
                fontWeight: 'bold',
            },
        }}
        tickCount={6}
        // define the format of the main axis dates
        tickFormat={(x) => {
            const dateObj = new Date(x);
            const year = dateObj.getFullYear().toString().substr(-2);
            const month = dateObj.toLocaleString('fr-fr', { month: '2-digit' });
            return `${month}/${year}`;
        }}
       fixLabelOverlap={true}
    />

The data are defined as below :

<VictoryLine
        interpolation="monotoneX"
        labelComponent={(
        <VictoryTooltip
            style={{
                fontSize: 10,
                fontWeight: 'bold',
            }}
            flyoutStyle={{
                stroke: 'tomato',
                strokeWidth: 0,
                fill: '#f87268',
            }}
        />
        )}
        style={{
            data: { stroke: '#f87268' },
            parent: {
                border: '1px solid #ccc',
                fontFamily: 'Nunito',
            },
        }}
        domain={{
            y: [0, 10]
        }}
        data={petWeight.map((item) => {
            if (item.weightDate && item.weightValue) {
                return { x: new Date(item.weightDate), y: item.weightValue };
            }
        })}
        animate={{
            duration: 1500,
            onLoad: { duration: 1000 },
        }}
    />

enter image description here

I want to remove the dates duplicates in x-axis, I have read the documentation but couldn't find the solution. How can I do that ?

1

There are 1 best solutions below

4
On

I think that you should be responsible for handling duplicate date case, not the library itself. One sample approach is to merge values that have the same weightDate, calculate their avarage weightValue to display.