exchange-client_2/src/components/LineChart.jsx

102 lines
2.3 KiB
React
Raw Normal View History

2023-03-01 12:36:58 +00:00
// Modules
import { Line } from 'react-chartjs-2';
import {
Chart as ChartJS,
LineElement,
CategoryScale,
LinearScale,
PointElement,
Tooltip,
} from 'chart.js';
import { Api } from '../helpers/api';
import { useState, useEffect } from 'react';
import { parseDate } from '../helpers/functions';
ChartJS.register(LineElement, PointElement, LinearScale, Tooltip, CategoryScale);
const LineChart = ({ activeRow, tabIndex }) => {
const [dataLineChart, setDataLineChart] = useState();
useEffect(() => {
// Table data fetch
if (tabIndex) {
const LineChartData = new Api(
`https://tmex.gov.tm:8765/api/categories/${tabIndex}/tradings`,
dataLineChart,
setDataLineChart,
).get();
}
}, [tabIndex]);
let delayed;
2023-04-26 06:41:11 +00:00
console.log(dataLineChart.data[0].all_prices.reverse());
2023-03-01 12:36:58 +00:00
const data = {
labels: dataLineChart
? dataLineChart.data[activeRow].all_prices.map((price) => parseDate(price.date)).reverse()
: [''],
datasets: [
{
data: dataLineChart
2023-04-26 06:41:11 +00:00
? dataLineChart.data[activeRow].all_prices
.map((price, index) => {
if (index <= 9) {
return price.price;
} else {
return '';
}
})
.reverse()
2023-03-01 12:36:58 +00:00
: [''],
borderColor: '#4b8dff',
pointBorderWidth: 2,
pointBackgroundColor: '#4b8dff',
tension: 0.1,
animation: {
onComplete: () => {
delayed = true;
},
delay: (context) => {
let delay = 0;
if (context.type === 'data' && context.mode === 'default' && !delayed) {
delay = context.dataIndex * 100 + context.datasetIndex * 100;
}
return delay;
},
},
},
],
};
const options = {
maintainAspectRatio: false,
radius: 5,
hitRadius: 30,
hoverRadius: 7,
scales: {
x: {
grid: {
display: false,
},
},
y: {
ticks: {
callback: (value) => value,
},
grid: {
borderDash: [10],
},
},
},
};
return (
<div className="line-chart">
<Line data={data} options={options} />
</div>
);
};
export default LineChart;