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 CodePen
Apply
Reset
var DemoApp = angular.module('DemoApp', ['dx']); DemoApp.controller('DemoController', function DemoController($scope) { var store = new DevExpress.data.CustomStore({ load: function() { return connection.invoke("getAllData"); }, key: "date" }); $scope.chartOptions = { dataSource: { store: store }, margin: { right: 30 }, loadingIndicator: { enabled: true }, title: "Stock Price", series: [{ pane: "Price", argumentField: "date", type: "candlestick", aggregation: { enabled: true, method: "custom", calculate: function(e) { var prices = e.data.map(function(i) { return i.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] }; } } } }, { pane: "Volume", name: "Volume", type: "bar", argumentField: "date", valueField: "volume", color: "red", aggregation: { enabled: true, method: "sum" } }], panes: [{ name: "Price" }, { name: "Volume", height: 80 }], customizePoint: function(arg) { if(arg.seriesName === "Volume") { var point = $("#chart").dxChart("getAllSeries")[0].getPointsByArg(arg.argument)[0].data; if(point.close >= point.open) { return { color: "#1db2f5" }; } } }, tooltip: { enabled: true, shared: true, argumentFormat: "shortDateShortTime", contentTemplate: function (pointInfo, element) { var volume = pointInfo.points.filter(function(point) { return point.seriesName === "Volume"; })[0]; var prices = pointInfo.points.filter(function(point) { return point.seriesName !== "Volume"; })[0]; return "<div class='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>"; } }, crosshair: { enabled: true, horizontalLine: { visible: false } }, zoomAndPan: { argumentAxis: "both" }, scrollBar: { visible: true }, legend: { visible: false }, argumentAxis: { argumentType: "datetime", minVisualRangeLength: { minutes: 10 }, visualRange: { length: "hour" } }, valueAxis: { placeholderSize: 50 } }; $scope.connectionStarted = false; var connection = new signalR.HubConnectionBuilder() .withUrl("https://js.devexpress.com/Demos/NetCore/stockTickDataHub") .configureLogging(signalR.LogLevel.Information) .build(); connection.start() .then(function () { connection.on("updateStockPrice", function (data) { store.push([{ type: "insert", key: data.date, data: data }]); }); $scope.connectionStarted = true; $scope.$apply(); }); var formatCurrency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", minimumFractionDigits: 2 }).format; var formatNumber = new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format; });
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <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.6/css/dx.common.css" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/21.1.6/css/dx.light.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script> <script>window.angular || document.write(decodeURIComponent('%3Cscript src="js/angular.min.js"%3E%3C/script%3E'))</script> <script src="https://cdn3.devexpress.com/jslib/21.1.6/js/dx.all.js"></script> <script src="js/signalr.js"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="index.js"></script> </head> <body class="dx-viewport"> <div class="demo-container" ng-app="DemoApp" ng-controller="DemoController"> <div ng-if="connectionStarted"> <div id="chart" dx-chart="chartOptions"></div> </div> </div> </body> </html>
#chart{ height: 440px; } .tooltip-template span { font-weight: 500; }
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; } } }