hello there,
I am working on a project in which i am using this IgrFinancialChart chart.
so basically I want to make a real-time candlestick chart that update according to the values that i am receiving through a websoket.
basically I have some old data (historic data) like from tomorrow mid-night to current time. and from current time i have real-time data that is coming from websockets.
so I want to merge that both data.
Some how i managed to show old data but when i push new data (from websokets ) its not getting displayed by chart. i.e. chart is not updating.
I am attaching a short video for better understanding. you can see in the video that chart is not updating but data is coming (you can see in console. ).
need help.
thanks in advance. and let me know if you don't understand the problem i will explain more briefly.
Hello Sagar,
From the video you have provided, I am under the impression that the data already in the chart is the “old data (historic data)” and that the real-time data that you are trying to add is the same schema/structure as that data. Please correct me if this impression is wrong, as the following is based on it.
Since the chart appears to be receiving the new data, but not displaying it, I believe the issue could be that the corresponding “notify” method is not being called to notify the chart that it needs to re-paint. The method that needs to be called is the notifyInsertItem method on the chart. This method takes three parameters:
- source: the data source where the new item was added. - index: the integer index in the data source where the new item was added. - newItem: the item that was added.
Calling this method after you add the item should cause the chart to repaint and display the new item.
Please let me know if you have any other questions or concerns on this matter.
Hello Andrew,
first of all thanks for the answering.
As you said when new item insert I have to call notifyInsertItem. I already calling that method when I insert the data but it's not re-painting the chart again. In fact In that video I already called the same method you mentioned.
here is code for ref :
import React, { useRef, useState } from 'react' import { useEffect } from 'react'; import { IgrFinancialChart } from 'igniteui-react-charts'; import { IgrFinancialChartModule } from 'igniteui-react-charts'; // import { getdata } from './getdata'; IgrFinancialChartModule.register(); const Test = () => { const [chartData, setChartData] = useState([]); // const [realtimedata, setRealtime] = useState([]); // const [historicalData, sethistoricalData] = useState([]); const chartRef = useRef(0); const today = new Date(); // const endOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1).getTime(); const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate()); console.log(today.getTime()); console.log(todayMidnight.getTime()); useEffect(() => { fetch(`https://api.binance.com/api/v3/uiKlines?symbol=BTCUSDT&interval=1m&startTime=${todayMidnight.getTime()}&endTime=${today.getTime()}&limit=1000`) .then(res => res.json()) .then(data => { let newdata = []; data.forEach(item => { newdata = [...newdata, { date: new Date(item[0]), open: parseFloat(item[1]), high: parseFloat(item[2]), low: parseFloat(item[3]), close: parseFloat(item[4]), volume: parseFloat(item[5]) }] }); // console.log("Historical Data is : ", newdata); setChartData((chartData) => [...chartData, newdata]) }); const ws = new WebSocket(`wss://stream.binance.com:9443/ws/btcusdt@kline_1s`); ws.onmessage = (e) => { const dataJson = JSON.parse(e.data); // console.log("DataJson is : ", dataJson); const newdata = { date: new Date(dataJson.k.t), open: parseFloat(dataJson.k.o), high: parseFloat(dataJson.k.h), low: parseFloat(dataJson.k.l), close: parseFloat(dataJson.k.c), volume: parseFloat(dataJson.k.v) } // console.log(newdata); console.log(chartRef.current.dataSource); chartRef.current.dataSource.push(newdata); chartRef.current.notifyInsertItem(chartRef.current.dataSource, chartRef.current.dataSource.length - 1, newdata); } return () => { if (ws) { ws.close(); } }; }, []) function customTooltip(context) { const item = context.item; return ( <div> <div>Open: ${item.open}</div> <div>High: ${item.high}</div> <div>Low: ${item.low}</div> <div>Close: ${item.close}</div> </div> ); } return ( <div className="container sample"> <div className="container" style={{ height: "100vh" }}> <IgrFinancialChart ref={chartRef} tooltipTemplate={customTooltip} xAxisAutoScale={false} width="100%" height="100%" isToolbarVisible={true} chartType="Candle" chartTitle="Crypto Trades" titleAlignment="Left" titleLeftMargin="25" titleTopMargin="10" titleBottomMargin="10" subtitle="Binance - CryptoCoin Price, Currency in USD" subtitleAlignment="Left" subtitleLeftMargin="25" subtitleTopMargin="5" subtitleBottomMargin="10" yAxisMode="Numeric" yAxisVisibility={false} zoomSliderType="None" dataSource={chartData} /> </div> </div > ) } export default Test
let me know if you don't get my question.
and again thanks for replying quickly. :)