<html>
	<head>
		<title>Set Annotation List as JSON or XML</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>
		
		<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");
			// Writing the flash object into the page DOM.
			chart.write("chartContainer");

            // Here we will store annotations defined in the configuration file in JSON and as an XML string.
			var annotationsJSON = null;
			var annotationsXML = null;

			// Enabling disabled buttons when chart has rendered and save JSON and XML configuration of annotations
			chart.onChartDraw = function () {
			    annotationsJSON = chart.getAnnotationListAsJSON();
			    annotationsXML = chart.getAnnotationListAsXML();
			    document.getElementById("btnOnePointAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnTwoPointsAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnThreePointsAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnRemoveAll").removeAttribute("disabled");
			    document.getElementById("btnResetByJSON").removeAttribute("disabled");
			    document.getElementById("btnResetByXML").removeAttribute("disabled");
			}

			// Enable StopDrawingAnnotation button when any annotation drawing has started
			chart.onAnnotationDrawingStart = function (id) {
			    document.getElementById("btnStopDrawing").removeAttribute("disabled");
			}

			// Disable StopDrawingAnnotation button when annotation drawing successfully finished
			chart.onAnnotationDrawingFinish = function (id) {
			    document.getElementById("btnStopDrawing").setAttribute("disabled", "disabled");
			}

			// Enable btnRemoveSelectedAnnotation button when any annotation has been selected
			chart.onAnnotationSelect = function (id) {
			    document.getElementById("btnRemoveSelected").removeAttribute("disabled");
			}

			// Disable btnRemoveSelectedAnnotation button when no annotation is selected. 
			// This event also fires when the selected annotation has been removed, so we don't 
			// need to duplicate this disabling code to the removeSelectedAnnotation() function.
			chart.onAnnotationDeselect = function (id) {
			    document.getElementById("btnRemoveSelected").setAttribute("disabled", "disabled");
			}

			// All available one-anchor-point annotations
			var onePointAnnotations = [
                "VerticalLine",
                "HorizontalLine",
                "Arrow",
                "Label"
            ];

			// All available two-anchor-points annotations
			var twoPointAnnotations = [
                "Line",
                "FibonacciFan",
                "FibonacciArc",
                "FibonacciRetracement",
                "FibonacciTimezones",
                "InfiniteLine",
                "Ray",
                "Rectangle",
                "Ellipse"
            ];

			// All available three-anchor-points annotations
			var threePointAnnotations = [
                "Triangle",
                "TrendChannel",
                "AndrewsPitchfork"
            ];

			// Start drawing random one-anchor-point annotation
			function startOnePointAnnotation() {
			    chart.startDrawingAnnotation(onePointAnnotations[Math.floor(Math.random() * onePointAnnotations.length)]);
			}

			// Start drawing random two-anchor-points annotation
			function startTwoPointsAnnotation() {
			    chart.startDrawingAnnotation(twoPointAnnotations[Math.floor(Math.random() * twoPointAnnotations.length)]);
			}

			// Start drawing random three-anchor-points annotation
			function startThreePointsAnnotation() {
			    chart.startDrawingAnnotation(threePointAnnotations[Math.floor(Math.random() * threePointAnnotations.length)]);
			}

			// Stop drawing annotation
			function stopDrawing() {
			    chart.stopDrawingAnnotation();
			    document.getElementById("btnStopDrawing").setAttribute("disabled", "disabled");
			}

			// Removes selected annotation
			function removeSelected() {
			    var id = chart.getSelectedAnnotationId()
			    chart.removeAnnotation(id);
			}

			// Removes all annotations from the chart
			function removeAll() {
			    chart.removeAllAnnotations();
			}

			// Reset all changes made to annotations by replacing them with previously saved JSON
            function resetAnnotationsByJSON() {
			    chart.setAnnotationList(annotationsJSON);
			}

            // Reset all changes made to annotations by replacing them with previously saved XML
			function resetAnnotationsByXML() {
			    chart.setAnnotationList(annotationsXML);
			} 
		</script>
		
		<!-- table 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;
				padding-right:10px;
				text-align:left;
			}
			
			table.settings tr td {
				background-color: #F8F4F0;
				font:normal 70% Verdana;
				padding-bottom:2px;
				padding-top:2px;
				padding-left:10px;
				padding-right:10px;
				text-align:left;	
			}
			
			table.settings tr td input {
				width: 100%;
			}
			
			#chartContainer {
				width: 800px;
				height: 550px;
				float: left;
			}
			
			#log {
				width: 700px;
				height: 300;
				background-color: #FEFEFE;
				border-width: 1px;
				border-color: #C9C6C2;
				border-style: solid;
			}
		</style>
	</head>
	<body>
        <div id="chartContainer"><!-- Chart container --></div>
        <table class="settings">
            <tr>
				<th>Available Action</th>
			</tr>
			<tr>
				<td><input id="btnOnePointAnnotStart" type="button" value="Add one-anchor-point annotation" 
                        onclick="startOnePointAnnotation();" disabled="disabled" autocomplete="off"/></td>
			</tr>
			<tr>
				<td><input id="btnTwoPointsAnnotStart" type="button" value="Add two-anchor-points annotation" 
                        onclick="startTwoPointsAnnotation();" disabled="disabled" autocomplete="off"/></td>
			</tr>
			<tr>
				<td><input id="btnThreePointsAnnotStart" type="button" value="Add three-anchor-points annotation" 
                        onclick="startThreePointsAnnotation();" disabled="disabled" autocomplete="off"/></td>
			</tr>
            <tr>
				<td><input id="btnStopDrawing" type="button" value="Stop drawing annotations" 
                        onclick="stopDrawing();" disabled="disabled" autocomplete="off"/></td>
			</tr>
            <tr>
				<td><input id="btnRemoveSelected" type="button" value="Remove selected annotation" 
                        onclick="removeSelected();" disabled="disabled" autocomplete="off"/></td>
			</tr>
            <tr>
				<td><input id="btnRemoveAll" type="button" value="Remove all annotations" 
                        onclick="removeAll();" disabled="disabled" autocomplete="off"/></td>
			</tr>
            <tr>
				<td><input id="btnResetByJSON" type="button" value="Reset annotations using JSON" 
                        onclick="resetAnnotationsByJSON();" disabled="disabled" autocomplete="off"/></td>
			</tr>
            <tr>
				<td><input id="btnResetByXML" type="button" value="Reset annotations using XML" 
                        onclick="resetAnnotationsByXML();" disabled="disabled" autocomplete="off"/></td>
			</tr>
        </table>
	</body>
</html>

Sample Description

How to use this sample?

Click "Add one-anchor-point annotation", "Add two-anchor-points annotation" or "Add three-anchor-points annotation" to start drawing mode. To abort drawing, remove selected or all click "Stop drawing annotations", "Remove selected annotation" or "Remove all annotations" respectively.

Click "Reset annotations using JSON" or "Reset annotations using XML"button to replace the current set of annotations with initial, which are stored and set in JSON or XML.

to top

How it works

When page chart renders onChartDraw event is fired and at this point script saves annotations hard-coded in chart in JSON and XML (just for demo purposes, you don't have to do both in real application). Later, when "Reset annotations using JSON" or "Reset annotations using XML" buttons are pressed this saved data is used to set initial annotations again

When "Add one-anchor-point annotation", "Add two-anchor-points annotation" or "Add three-anchor-points annotation" button are pressed, startDrawingAnnotation() method makes chart enter drawing mode. removeAnnotation() and removeAllAnnotation() methods are used to remove annotations, stopDrawingAnnotation() to abort drawing.

When "Reset annotations using JSON" and "Reset annotations using XML"buttons are pressed, setAnnotationList() method puts saved annotations into the chart, this saved list replaces all annotations in the chart.

to top

AnyChartStock JavaScript API

This sample uses the following methods, properties and events from AnyChartStock JavaScript API:

Item Type Description
getAnnotationListAsJSON Method Returns an array with objects of annotation type, which contains all annotations in all charts.
getAnnotationListAsXML Method Returns an XML string with all annotations. XML of annotations is formatted as <annotation_list> node.
getSelectedAnnotationId Method Returns id of the selected annotation.
removeAllAnnotations Method Removes all annotations from all charts.
removeAnnotation Method Removes an annotation from the chart.
setAnnotationList Method Sets new annotation list to the chart, replacing all that are on the charts at the moment. Can be called only after onChartDraw event is fired.
setXMLFile Method Sets chart XML configuration file path.
startDrawingAnnotation Method Starts drawing annotation of a given type.
stopDrawingAnnotation Method Ends drawing mode started by startDrawingAnnotation() method.
write Method Adds the chart to HTML DOM as a child of the specified container.
onAnnotationDeselect Event This event is dispatched when an annotation looses focus.
onAnnotationDrawingFinish Event This event is dispatched when an annotation drawing initiated by startDrawingAnnotation() finishes.
onAnnotationDrawingStart Event This event is dispatched when startDrawingAnnotation() methods is used and drawing has successfully started.
onAnnotationSelect Event This event is dispatched when an annotation is selected.
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

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

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

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.