<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Update Existing Data Set Values along with Streaming</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" language="javascript" src="./../js/AnyChartStock.js?v=1.9.0r9317"></script> <!-- Randomizer script. It is used to make random more smooth. --> <script type="text/javascript" language="javascript" src="./js/Randomizer.js"></script> <script type="text/javascript" src="js/jquery.min.js"></script> <!-- chart size settings --> <style type="text/css"> #chartContainer { width: 800px; height: 550px; float: left; } </style> <script type="text/javascript" language="javascript"> // Creating new chart object. var chart = new AnyChartStock("./../swf/AnyChartStock.swf?v=1.9.0r9317", "./../swf/Preloader.swf?v=1.9.0r9317"); // Setting XML config file. chart.setXMLFile("config.xml"); // needConfig set to true allows to work with objectModel (to change series type in this sample) chart.needConfig = true; // Writing the flash object into the page DOM. chart.write("chartContainer"); //-------------------------------------------------------------------------------- // Date time utils //-------------------------------------------------------------------------------- // Format dat time according to mask set in config.xml for dataset (id = "dataSet1", mask: %u) function formatDate(date) { return Math.round(date.getTime() / 1000); } // date time increment function nextDate(date) { date.setUTCMinutes(date.getUTCMinutes()+1); } //-------------------------------------------------------------------------------- // Series type //-------------------------------------------------------------------------------- function changeSeriesType() { chart.getSeriesById("idMainChart","idMainSeries").type = $("#seriesType").val(); chart.applySettingsChanges(); } //-------------------------------------------------------------------------------- // Randomizer //-------------------------------------------------------------------------------- // generate row in csv function generateRandomRow(date) { var open = lastClose; // open value - last close close = closeRandomizer.generateNextValue()+Math.random()*0.03; // random close value high = Math.max(high, close); // high - maximal value low = Math.min(low, close); // low - minimal value // Create string accroding to CSV settings for data set "dataSet1" return formatDate(date) + "," + open + "," + high + "," + low + "," + close; } //-------------------------------------------------------------------------------- // Streaming + Updating //-------------------------------------------------------------------------------- var high; // current high value var low; // current low value var close = 31.1350; // Last close value var lastClose = close; var timestamp = new Date(1272311940000); // Last Date in data set // Randomizer instance for close values var closeRandomizer = new Randomizer(lastClose, 30.99, 31.25, 0.01); // quantity of changes var changeCount = 0; // changes interval var lastChangeInterval; // start streaming function startStreaming() { // Set Start and Stop buttons states $("#btnStart").attr("disabled","disabled"); $("#btnStop").removeAttr("disabled"); addPoint(); } // function to add new point function addPoint() { nextDate(timestamp); // date time increment lastClose = close; // remember last close high = close; // new high is the last close low = close; // new low is the same changeCount = 0; // quantity of changes lastChangeInterval = setInterval(changeLast, 500, false); // change values every 500 ms changeLast(true); // add one point and remove one point } // function to change values of the last point function changeLast() { if (changeCount == 10) { // if changes has been made 11 times - go to next point clearInterval(lastChangeInterval); // clear changes interval addPoint(); // start adding next point }else { var removePoint = arguments.length > 0 ? arguments[0] : false; // remove first point or not? var csvData = generateRandomRow(timestamp); // generate random row chart.appendData("dataSet1", csvData, removePoint ? 1 : 0); // add it to csv chart.commitDataChanges(); // apply changes changeCount++; // changeCount increment } } // Function to stop streaming function stopStreaming() { // Set Start and Stop buttons states $("#btnStart").removeAttr("disabled"); $("#btnStop").attr("disabled","disabled"); clearInterval(lastChangeInterval); // stop changing } //--------------------------------------------------------------------------------------------------- // html events binding //--------------------------------------------------------------------------------------------------- $(function() { $("#seriesType").change(changeSeriesType); }); //--------------------------------------------------------------------------------------------------- // Enable html controls //--------------------------------------------------------------------------------------------------- chart.onChartDraw = function() { $("#seriesType").removeAttr("disabled"); $("#btnStart").removeAttr("disabled"); chart.onChartDraw = null; } </script> <!-- style settings --> <style type="text/css"> table.settings { border-style: solid; border-width: 1px; border-color: #D0CDC9; } table.settings tr th { font:normal 60% Verdana; background-color: #DCD9D5; font-weight:bold; padding-bottom:5px; padding-top:5px; padding-left:10px; text-align:left; } table.settings tr td { background-color: #F8F4F0; font:normal 70% Verdana; padding-bottom:2px; padding-top:2px; padding-left:10px; text-align:left; } table.settings input, table.settings select { width: 150px; } </style> </head> <body> <div id="chartContainer"><!-- Chart Container --></div> <table class="settings"> <tr> <th colspan="2">Streaming:</th> </tr> <tr> <td>Start: </td> <td><input id="btnStart" type="button" value="Start" disabled="disabled" onclick="startStreaming()" /></td> </tr> <tr> <td>Stop: </td> <td><input id="btnStop" type="button" value="Stop" disabled="disabled" onclick="stopStreaming()"/></td> </tr> <tr> <th colspan="2">Series type:</th> </tr> <tr> <td>Series type: </td> <td><select id="seriesType" disabled="disabled"> <option value="Line">Line</option> <option value="Candlestick" selected="true">Candlestick</option> <option value="OHLC">OHLC</option> <option value="Area">Area</option> </td> </tr> </select> </body> </html>
Sample Description
How to use this sample?
Use Start/Stop button to enable to control data streaming and drop down list to switch series type.
How it works
This sample shows how chart values can be updated in real-time using methods described in Real-Time Data Streaming and Data Manipulations, but, at the same time, it is almost ready to use application prototype, that shows live data feed with ability to see newcoming values oscillations.
Particularly, when you click buttons script generates random points and then starts updating one point, does this several time using appendData and then pushes it into the data set using the same appendData method, in both cases appending or updating should be followed by commitDataChanges to apply changes. Note, that when you series is updated not only series displayed on the chart changes, but also thumbnail series in scroller is updated too, because they are based on the same data set.
The special thing in this sample you should understand is the fact, that appendData is used both for adding data and updating values too - please see Logic Principles section in Real-Time Data Streaming and Data Manipulations.
AnyChartStock JavaScript API
This sample uses the following methods, properties and events from AnyChartStock JavaScript API:
Item | Type | Description |
---|---|---|
needConfig | Property (Boolean) | Defines whether the access to full chart configuration object model is required. |
appendData | Method | Appends data to the chart. |
applySettingsChanges | Method | Commits changes made to settings element of the objectModel. |
commitDataChanges | Method | Commits data appends and data points erasure made by appendData(), removeDataRow() and removeDataRange() method calls. |
getSeriesById | Method | Gets series object from the object model by its id. |
setXMLFile | Method | Sets chart XML configuration file path. |
write | Method | Adds the chart to HTML DOM as a child of the specified container. |
onChartDraw | Event | This event is dispatched when the AnyChart Stock is drawn. |
Prerequisites
This section lists all configuration, data and auxiliary files required for this sample.
Configuration file
CSV files
SWF files
- AnyChartStock.swf - AnyChart Stock component.
- Preloader.swf - AnyChart Stock helper component that loads the main component (AnyChartStock.swf) and displays loading progress.
JavaScript Libraries
- jquery.min.js - A JavaScript jQuery library. Learn more at jQuery official site.
- Randomizer.js - A sample JavaScript library that generates random values.
- AnyChartStock.js - A JavaScript library that is shipped with AnyChart Stock component. It is used to embed the component into HTML DOM and to comunicate with the Flash part.
The information contained in this website is for general information purposes only. All sample data provided on this site is for demonstration purposes only.
The logos and names of other companies and products mentioned on this site are copyright and/or trademarks of their respective owners.
The content on this site, including news, quotes, data and other information, is provided for your personal information only, and is intended for demonstration purposes only. Content on this site is not appropriate for the purposes of making a decision to carry out a transaction or trade. Nor does it provide any form of advice (investment, tax, legal) amounting to investment advice, or make any recommendations regarding particular financial instruments, investments or products.
In no event AnyChart will be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website.
This site may point to other Internet sites that may be of interest to you, however AnyChart does not endorse or take responsibility for the content on such other sites
Market data and News provided by and copyright RediNews, Incorporated.