1👍
UPDATE1 ==> I did it with the useEffect hook can’t able to do it with a built-in method update()
import React, { useEffect, useState } from 'react';
import { Line } from 'react-chartjs-2';
import { Chart as ChartJS} from 'chart.js/auto';
const Chart = ({following}) => {
const date = following.map(item => new Date(item.dt * 1000));
const dateLocal = date.map(item => item.toLocaleString('default', { weekday: 'long' }));
const temp = following.map(item => Math.floor(item.temp.day));
// if userData changes then the chart will re-render
useEffect(() => {
setUserData({
labels: dateLocal,
datasets: [
{
data: temp,
label: 'Temperature',
backgroundColor: [
'rgba(0, 0, 0,.5)',
'rgba(0, 0, 0,.5)',
'rgba(0, 0, 0,.5)',
],
color : ['rgba(0,0,0,1)'],
fill:true,
pointRadius: 1,
pointHitRadius:4,
pointHoverRadius:8,
pointBackgroundColor: 'mediumblue',
borderWidth: 2,
pointBorderColor: 'lightblue',
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
tooltips: {
mode: 'index',
intersect: true,
windowPadding: 10,
},
scales: {
xAxes: [{
gridLines: {
display: false,
},
ticks: {
fontColor: 'green'
}
}],
yAxes: [{
gridLines: {
display: false,
},
ticks: {
fontColor: 'green'
}
}]
}
}
]
});
},[following]);
const [userData, setUserData] = useState({
labels: dateLocal,
datasets: [
{
data: temp,
label: 'Temperature',
backgroundColor: [
'rgba(0, 0, 0,.5)',
'rgba(0, 0, 0,.5)',
'rgba(0, 0, 0,.5)',
],
color : ['rgba(0,0,0,1)'],
fill:true,
pointRadius: 1,
pointHitRadius:4,
pointHoverRadius:8,
pointBackgroundColor: 'mediumblue',
borderWidth: 2,
pointBorderColor: 'lightblue',
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
tooltips: {
mode: 'index',
intersect: true,
windowPadding: 10,
},
scales: {
xAxes: [{
gridLines: {
display: false,
},
ticks: {
fontColor: 'green'
}
}],
yAxes: [{
gridLines: {
display: false,
},
ticks: {
fontColor: 'green'
}
}]
}
}
]
});
return (
<div className="line" style={{ width:'100%'}}>
<Line className="actual-chart"
data={userData}
redraw={true}
/>
</div>
)
};
export default Chart;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
- Chartjs-Is it possible for chat.js "Stepped Line Chart" to render the stepped line without the next data point
- Chartjs-Cannot find name 'ChartOptions'
0👍
Chart.js has update method. Try save your chart in variable and then call foo.update(), when necessary
Source:stackexchange.com