<html>
	<head>
		<title>Annotations Events Handling</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>	
        <script type="text/javascript" language="javascript" src="./js/jquery.min.js"></script>
		<script type="text/javascript" language="javascript" src="./../js/AnyChartStock.js?v=1.9.0r9317"></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");
			// Writing the flash object into the page DOM.
			chart.write("chartContainer");

			// Enabling disabled buttons when chart has been rendered
			chart.onChartDraw = function () {
			    document.getElementById("btnOnePointAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnTwoPointsAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnThreePointsAnnotStart").removeAttribute("disabled");
			    document.getElementById("btnRemoveAll").removeAttribute("disabled");
			}

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

			// Just log an event
			chart.onAnnotationDrawingProgress = function (id, mouseX, mouseY) {
			    logEvent("onAnnotationDrawingProgress", id, mouseX, mouseY);
			}

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

			// Just log an event
			chart.onAnnotationEditingStart = function (id) {
			    logEvent("onAnnotationEditingStart", id);
			}

			// Just log an event
			chart.onAnnotationEditingProgress = function (id) {
			    logEvent("onAnnotationEditingProgress", id);
			}

			// Just log an event
			chart.onAnnotationEditingFinish = function (id) {
			    logEvent("onAnnotationEditingFinish", id);
			}

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

			// Disable btnRemoveSelectedAnnotation button when no annotation is selected and log an event. 
			// 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");
			    logEvent("onAnnotationDeselect", id);
			}

            // Log an event.
			chart.onAnnotationRemove = function (id) {
			    logEvent("onAnnotationRemove", id);
			}

			// Events Log
			var events = [];

            // Log an event
			function logEvent(type, annotationId, opt_mouseX, opt_mouseY) {
                var mouseCoords;
                if (arguments.length == 2)
                    mouseCoords = null;
                else
                    mouseCoords = "[" + opt_mouseX + "; " + opt_mouseY + "]";
                // Show only ten last events
			    events.push({ type: type, id: annotationId, mouse: mouseCoords });
			    if (events.length > 10)
			        events = events.splice(1);

			    // Events are shown in log table
			    for (var i = 0; i < events.length; i++) {
			        var e = events[events.length - 1 - i];
			        var rowIndex = (i + 2).toString();

			        var row = $("table.log tr:eq(" + rowIndex + ")");

			        row.find("td:eq(0)").html(e.type);
			        row.find("td:eq(1)").html(e.id);
                    if (e.mouse)
                        row.find("td:eq(2)").html(e.mouse);
                    else
                        row.find("td:eq(2)").html("");
			    }
			}

			// 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();
			}
		</script>
		
		<!-- table settings -->
		<style type="text/css">
			table.settings  {
				border-style: solid;
				border-width: 1px;
				border-color: #D0CDC9;
			}

			table.settings tr th {
				font:normal 7pt 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 8pt Verdana;
				padding-bottom:2px;
				padding-top:2px;
				padding-left:10px;
				text-align:left;	
			}
			
			table.log td {
				height: 18px;
			}
			
			table.settings tr td.offset {
				text-align: center;
				vertical-align: middle;
				font-weight: bold;
				border: 0;
			}
			
			input {
			    width: 100%
			}
		</style>
	</head>
	<body>
		<div id="chartContainer"><!-- Chart Container --></div>
		<table>
            <tr>
                <td width="530">
				    <!-- events log -->
				    <table class="settings log" width="100%">
					    <tr><th colspan="3">Events Log:</th></tr>
					    <tr><th width="170">Annotation event</th><th width="200">Annotation id</th><th>Mouse coordinates</th></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
					    <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
				    </table>
				</td>
            </tr>
			<tr><td>
                <table class="settings">
                    <tr>
				        <th>AvaliableAction</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>
                </table>
            </td></tr>
		</table>
	</body>
</html>

Sample Description

How to use this sample?

Sample shows how to handle events dispatched by annotations.

Click"Add one-anchor-point annotation", "Add two-anchor-points annotation" or "Add three-anchor-points annotation" button do draw a random annotation to the chart. To abort drawing click "Stop drawing annotations", to remove selected or all click "Remove selected annotation" or "Remove all annotations".

When you click buttons, select, deselect, remove annotations, engine dispatches different events which are shown in log table.

to top

How it works

When "Add one-anchor-point annotation", "Add two-anchor-points annotation" or "Add three-anchor-points annotation" buttons are clicked startDrawingAnnotation starts drawing mode. When latter happens component dispatches onAnnotationDrawingStart event. When user finishes drawing onAnnotationDrawingFinish is fired.

Selection and deselection fires onAnnotationSelect and onAnnotationDeselect. Removal using Delete key fires onAnnotationRemove. You can learn more about events handling on Event handling article.

to top

AnyChartStock JavaScript API

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

Item Type Description
getSelectedAnnotationId Method Returns id of the selected annotation.
removeAllAnnotations Method Removes all annotations from all charts.
removeAnnotation Method Removes an annotation from the chart.
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.
onAnnotationDrawingProgress Event This event is fired repeatedly when annotation drawing started with startAnnotationDrawing() call progresses. Mouse coordinates, passed to event handler may become NaN, when the cursor leaves plot area.
onAnnotationDrawingStart Event This event is dispatched when startDrawingAnnotation() methods is used and drawing has successfully started.
onAnnotationEditingFinish Event This event is dispatched when a user has finished editing an annotation.
onAnnotationEditingProgress Event This event is fired repeatedly when annotation editing progresses.
onAnnotationEditingStart Event This event is dispatched when a user has started editing an annotation.
onAnnotationRemove Event This event is dispatched when an annotation is removed using "Delete" key on a Windows system or "Fn + Backspace" on Mac.
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

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

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.