Your search did not match any results.
Charts

SignalR Service

Documentation

This example demonstrates real-time data update in a financial chart bound to a SignalR server. Note that data used in this demo is for demonstration purposes only.

Backend API
Copy to CodeSandBox
Apply
Reset
import React from 'react'; import Chart, { ArgumentAxis, ValueAxis, Aggregation, Legend, Series, ScrollBar, ZoomAndPan, LoadingIndicator, Pane, Tooltip, Crosshair } from 'devextreme-react/chart'; import CustomStore from 'devextreme/data/custom_store'; import { HubConnectionBuilder, HttpTransportType } from '@aspnet/signalr'; import TooltipTemplate from './TooltipTemplate.js'; class App extends React.Component { constructor(props) { super(props); this.state = { dataSource: null }; const hubConnection = new HubConnectionBuilder() .withUrl('https://js.devexpress.com/Demos/NetCore/stockTickDataHub', { skipNegotiation: true, transport: HttpTransportType.WebSockets }) .build(); const store = new CustomStore({ load: () => hubConnection.invoke('getAllData'), key: 'date' }); hubConnection .start() .then(() => { hubConnection.on('updateStockPrice', (data) => { store.push([{ type: 'insert', key: data.date, data: data }]); }); this.setState({ dataSource: store }); }); this.customizePoint = this.customizePoint.bind(this); this.storeChartRef = (ref) => { this.chartRef = ref; }; } calculateCandle(e) { const prices = e.data.map(d => d.price); if (prices.length) { return { date: new Date((e.intervalStart.valueOf() + e.intervalEnd.valueOf()) / 2), open: prices[0], high: Math.max.apply(null, prices), low: Math.min.apply(null, prices), close: prices[prices.length - 1] }; } } render() { return ( <div> <Chart id="chart" ref={this.storeChartRef} dataSource={this.state.dataSource} margin={{ right: 30 }} title="Stock Price" customizePoint={this.customizePoint}> <Series pane="Price" argumentField="date" type="CandleStick"> <Aggregation enabled={true} method="custom" calculate={this.calculateCandle} /> </Series> <Series pane="Volume" name="Volume" argumentField="date" valueField="volume" color="red" type="bar"> <Aggregation enabled={true} method="sum" /> </Series> <Pane name="Price"></Pane> <Pane name="Volume" height={80}></Pane> <Legend visible={false} /> <ArgumentAxis argumentType="datetime" minVisualRangeLength={{ minutes: 10 }} defaultVisualRange={{ length: 'hour' }} /> <ValueAxis placeholderSize={50} /> <ZoomAndPan argumentAxis="both" /> <ScrollBar visible={true} /> <LoadingIndicator enabled={true} /> <Tooltip enabled={true} shared={true} argumentFormat="shortDateShortTime" contentRender={TooltipTemplate} /> <Crosshair enabled={true} horizontalLine={{ visible: false }} /> </Chart> </div> ); } customizePoint(arg) { if(arg.seriesName === 'Volume') { var point = this.chartRef.instance.getAllSeries()[0].getPointsByArg(arg.argument)[0].data; if(point && point.close >= point.open) { return { color: '#1db2f5' }; } } } } export default App;
import React from 'react'; const formatCurrency = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0 } ).format; const formatNumber = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0 }).format; export default function TooltipTemplate(pointInfo) { const volume = pointInfo.points.filter(point => point.seriesName === 'Volume')[0]; const prices = pointInfo.points.filter(point => point.seriesName !== 'Volume')[0]; return ( <div className="tooltip-template"> <div>{ pointInfo.argumentText }</div> <div><span>Open: </span> { formatCurrency(prices.openValue) } </div> <div><span>High: </span> { formatCurrency(prices.highValue) } </div> <div><span>Low: </span> { formatCurrency(prices.lowValue) } </div> <div><span>Close: </span> { formatCurrency(prices.closeValue) } </div> <div><span>Volume: </span> { formatNumber(volume.value) } </div> </div> ); }
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; ReactDOM.render( <App />, document.getElementById('app') );
<!DOCTYPE html> <html> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/21.1.5/css/dx.common.css" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/21.1.5/css/dx.light.css" /> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script> <script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script> <script type="text/javascript" src="config.js"></script> <script type="text/javascript"> System.import('./index.js'); </script> </head> <body class="dx-viewport"> <div class="demo-container"> <div id="app"></div> </div> </body> </html>
#chart { height: 440px; } .tooltip-template span { font-weight: 500; }
window.config = { transpiler: 'plugin-babel', meta: { 'devextreme/localization.js': { "esModule": true }, }, paths: { 'npm:': 'https://unpkg.com/' }, defaultExtension: 'js', map: { 'react': 'npm:react@17.0.2/umd/react.development.js', 'react-dom': 'npm:react-dom@17.0.2/umd/react-dom.development.js', 'prop-types': 'npm:prop-types@15.7.2/prop-types.js', '@aspnet/signalr': 'npm:@aspnet/signalr@1.0.0/dist/cjs', 'tslib': 'npm:tslib@2.3.0/tslib.js', 'rrule': 'npm:rrule@2.6.6/dist/es5/rrule.js', 'luxon': 'npm:luxon@1.28.0/build/global/luxon.min.js', 'es6-object-assign': 'npm:es6-object-assign@1.1.0', 'devextreme': 'npm:devextreme@21.1.5/cjs', 'devextreme-react': 'npm:devextreme-react@21.1.5', 'jszip': 'npm:jszip@3.7.0/dist/jszip.min.js', 'devextreme-quill': 'npm:devextreme-quill@1.3.1/dist/dx-quill.min.js', 'devexpress-diagram': 'npm:devexpress-diagram@2.1.25/dist/dx-diagram.js', 'devexpress-gantt': 'npm:devexpress-gantt@3.0.4/dist/dx-gantt.js', '@devextreme/vdom': 'npm:@devextreme/vdom@1.2.2', 'inferno': 'npm:inferno@7.4.8/dist/inferno.min.js', 'inferno-compat': 'npm:inferno-compat@7.4.8/dist/inferno-compat.min.js', 'inferno-create-element': 'npm:inferno-create-element@7.4.8/dist/inferno-create-element.min.js', 'inferno-dom': 'npm:inferno-dom@1.0.7/dist/inferno-dom.min.js', 'inferno-hydrate': 'npm:inferno-hydrate@7.4.8/dist/inferno-hydrate.min.js', 'inferno-clone-vnode': 'npm:inferno-clone-vnode@7.4.8/dist/inferno-clone-vnode.min.js', 'inferno-create-class': 'npm:inferno-create-class@7.4.8/dist/inferno-create-class.min.js', 'inferno-extras': 'npm:inferno-extras@7.4.8/dist/inferno-extras.min.js', // SystemJS plugins 'plugin-babel': 'npm:systemjs-plugin-babel@0.0.25/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js' }, packages: { 'devextreme': { defaultExtension: 'js' }, '@devextreme/vdom': { defaultExtension: 'js' }, 'devextreme-react': { main: 'index.js' }, 'devextreme/events/utils': { main: 'index' }, 'devextreme/events': { main: 'index' }, '@aspnet/signalr': { main: 'index.js', defaultExtension: 'js' }, 'es6-object-assign': { main: './index.js', defaultExtension: 'js' } }, packageConfigPaths: [ "npm:@devextreme/*/package.json", ], babelOptions: { sourceMaps: false, stage0: true, react: true } }; System.config(window.config);
using System.Collections.Generic; using DevExtreme.MVC.Demos.Models.SignalRTickData; using Microsoft.AspNet.SignalR; namespace DevExtreme.MVC.Demos.Hubs { public class StockTickDataHub : Hub { private readonly TickDataService _service; public StockTickDataHub() { _service = TickDataService.Instance; } public IEnumerable<TickItem> GetAllData() { return _service.GetAllData(); } } }
using DevExtreme.MVC.Demos.Hubs; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using System; using System.Collections.Generic; using System.Threading; namespace DevExtreme.MVC.Demos.Models.SignalRTickData { public class TickDataService { public readonly static TickDataService Instance = new TickDataService(GlobalHost.ConnectionManager.GetHubContext<StockTickDataHub>().Clients); private IHubConnectionContext<dynamic> Clients { get; set; } private readonly object updateStockPricesLock = new object(); private TickItem[] tickData; private int lastTickIndex; private Timer timer; private TickDataService(IHubConnectionContext<dynamic> clients) { Clients = clients; tickData = GenerateTestData(); lastTickIndex = tickData.Length - 1; timer = new Timer(Update, null, 1000, 1000); } public IEnumerable<TickItem> GetAllData() { var data = new List<TickItem>(); lock(updateStockPricesLock) { for(var i = lastTickIndex; data.Count < 10000; i--) { data.Add(tickData[i]); if(i == 0) { i = tickData.Length - 1; } } } return data; } private void Update(object state) { lock(updateStockPricesLock) { lastTickIndex = (lastTickIndex + 1) % tickData.Length; var tick = tickData[lastTickIndex]; tick.Date = DateTime.Now; BroadcastStockPrice(tick); } } private void BroadcastStockPrice(TickItem item) { Clients.All.updateStockPrice(item); } private TickItem[] GenerateTestData() { var lastPrice = 140m; var random = new Random(); var slowRandomValue = random.NextDouble(); var data = new TickItem[60 * 60 * 20]; var curTime = DateTime.Now; for(var i = 0; i < data.Length / 2; i++) { lastPrice = Math.Round(lastPrice * (decimal)(1 + 0.002 * (-1 + 2 * random.NextDouble())), 2); if(i % 50 == 0) { slowRandomValue = random.NextDouble(); if(slowRandomValue > 0.3 && slowRandomValue <= 0.5) slowRandomValue -= 0.2; if(slowRandomValue > 0.5 && slowRandomValue <= 0.7) slowRandomValue += 0.2; } var volume = (int)(100 + 1900 * random.NextDouble() * slowRandomValue); data[data.Length - 1 - i] = new TickItem(lastPrice, volume, curTime.AddSeconds(-1 * i)); data[i] = new TickItem(lastPrice, volume, curTime.AddSeconds(-1 * (data.Length - 1 - i))); } return data; } } }