Reputation: 407
I am using React Vis to create a simple Graph. This is my code:
import React from 'react';
import '../../../node_modules/react-vis/dist/style.css';
import {XYPlot, VerticalBarSeries} from 'react-vis';
const myData = [
{x: 'A', y: 10},
{x: 'B', y: 5},
{x: 'C', y: 15}
]
export const Chart = () => {
return (
<XYPlot height={500} width={500}>
<VerticalBarSeries data={myData} />
</XYPlot>
)
};
export default Chart;
I receive this error message:
Received NaN for the `x` attribute. If this is expected, cast the value to a string."
When I change the x values in myData to ints, it works, but I need them as strings. The documentation clearly states that this should work:
Upvotes: 1
Views: 171
Reputation: 667
In my case, it's the other way around. I always had xType="ordinal" but I guess recently the data type changed to numbers and started getting this error. The fix is to convert the x-axis value from numerics to proper strings (which I could do by fixing the data itself, but this one is the smallest change possible to fix the chart)
export const Chart = () => {
return (
<XYPlot height={500} width={500} xType='ordinal' getX={d=>`${d[0]}`}>
<VerticalBarSeries data={myData} />
</XYPlot>
)
};
Upvotes: 0
Reputation: 1
For the x value to be a string you need to add xType='ordinal'
to the XYPlot
as following:
export const Chart = () => {
return (
<XYPlot height={500} width={500} xType='ordinal'>
<VerticalBarSeries data={myData} />
</XYPlot>
)
};
Now it should work for you.
Upvotes: 0