<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Ajax Data Loading</title>
	
	<script type="text/javascript" src="./../js/AnyChartStock.js?v=1.9.0r9317"></script>
	<script type="text/javascript" src="./js/jquery.min.js"></script>
	<script type="text/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");
		// Writing the flash object into the page DOM.
		chart.write("chartContainer");
		// NOTE: needConfig flag set to true, it provides an ability to work with chart objectModel
		chart.needConfig = true;
		// Settings wMode to prevent Flash movie to overlay HTML div
		chart.wMode = 'opaque';
		
		// Function is called when DOMDocumentContentLoaded event happens
		$(function(){
			// Overlay which is shown when new data is loading
			var overlay = $('<div></div>').text('No data to display').css({
				display: 'none',
				position: 'absolute',
				top:0, left:0, right: 0, bottom: 0,
				backgroundColor: '#fff',
				backgroundRepeat: 'no-repeat',
				backgroundPosition: '330px 260px',
				border: '1px solid #ddd',
				padding: '235px 330px'
			});
			
			// event is fired each time flash object is redrawn
			chart.onChartDraw = function() {
				// "this" refers to chart object here
				// set handler to null - make sure event is fired only once
				this.onChartDraw = null;
				
				overlay.appendTo('#chartContainer').show()
			}
			
			// Code is executed 'onchange' event of #dataSet node
			$('#dataSet').change(function(){
				var val = $(this).val();
				if (val != '_none_') {
					$.ajax({ // then make an AJAX-request
						async: true,
						cache: false,
						url: './data/' + val + '_daily.csv',
						dataType: 'text', // csv data is  text
						beforeSend: function() {
							// Removes old data, note that "from" and "to" dates are used 
							chart.removeDataRange('ds1', chart.getFirstDate(), chart.getLastDate());
							// Show overlay with "loading data" message
							overlay
								.text('Loading data...')
								.css({
									backgroundImage: 'url(./preloader.gif)',
									opacity: 0.8
								})
								.show();
						},
						success: function(resp) { // "resp" variable is a response to AJAX-request
							// Append new data into the 'ds1' data set
							chart.appendData('ds1', resp);
							// getSeriesById method returns 's1' series 'main' chart,
							// you can also use full path to the series through the objectModel, but this way is shorter
							chart.getSeriesById('main', 's1').name = val;
							// Apply dataset changes
							chart.commitDataChanges();
							// Apply settings changes (new series name)
							chart.applySettingsChanges();
							// Hide overlay:
							overlay.hide()
						}
					});
				} else { // "_none_" is a special value that means, that nothing should be loaded
					overlay
						.text('No data to display')
						.css({
							backgroundImage: 'none',
							opacity: 1
						})
						.show();
				}
			});
		});
	</script>
	<style type="text/css">
		#chartContainer{width: 830px;height: 520px;float: left;margin-right:20px;position:relative;}
		.settings{border:1px solid #D0CDC9;width:150px;}
		.settings th{font:bold 60% Verdana;background: #DCD9D5;padding:5px 0 5px 10px;text-align:left;}
		.settings td{background:#F8F4F0;font:normal 70% Verdana;padding:2px 10px;text-align:left;}
		#dataSet{width:100%;}
	</style>
</head>
<body>
	<div id="chartContainer"></div>
	<table class="settings">
		<tr><th>Select data set to load:</th></tr>
		<tr>
			<td>
				<select id="dataSet" autocomplete="off">
					<option value="_none_">None</option>
					<option value="msft">MSFT</option>
					<option value="orcl">ORCL</option>
					<option value="csco">CSCO</option>
					<option value="gspc">GSPC</option>
				</select>
			</td>
		</tr>
	</table>
</body>
</html>

Sample Description

How to use this sample?

Use dropdown selector to change chart data sets.

to top

How it works

AnyChart Stock Component allows real-time data loading. This sample shows how to load CSV data from the server using AJAX and add this data directly into the data set.

Initial XML configuration file (config.xml) contains only one data set with source_mode set to "InternalData", but with empty <csv_data> node. Such empty data set generates empty chart, which is overlaid with special <div>.

When user changes the data set, CSV file is loaded using AJAX, old data is removed using removeDataRange() method, and new data is pasted using appendData method. You can learn more about this technique in Real-Time Data Streaming and Data Manipulations article.

Note, that when data is changed we also change series name using object model, learn more about object model in Updating Chart using Object Model article. Don't forget, that working with object model is possible only when needConfig property is set to true.

Loading data with AJAX takes some time, so we place an overlay <div> with "Loading" message when loading starts, when loading ends - we hide it, note, that wMode should be set to "opaque" to allow showing anything over the Flash object.

to top

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.
wMode Property (String) Sets the Window Mode property of the Flash movie for transparency, layering, and positioning in the browser.
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.
getFirstDate Method Gets the first date of all used by visible series data sources.
getLastDate Method Gets the last date from all mapped and used by visible series data sources.
getSeriesById Method Gets series object from the object model by its id.
removeDataRange Method Removes data range from a data set by the range of dates.
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.

to top

Prerequisites

This section lists all configuration, data and auxiliary files required for this sample.

Configuration file

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.
  • 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.

Additional Files

to top

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.