<html> <head> <title>Adding/Removing Event Markers Dynamically</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> <!-- chart size settings --> <style type="text/css"> #chartContainer { width: 630px; height: 520px; 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"); // Create two auxiliary objects to store information about last // added A group event marker var groupALastInfo = { date: new Date(Date.UTC(1999,10,1)), index: 0 }; // And the same for B group var groupBLastInfo = { date: new Date(Date.UTC(1999,10,1)), index: 0 }; // Auxiliary date increment function function increaseDate(date) { date.setUTCMonth(date.getUTCMonth() + 3); } // Auxiliary date decrement function function decreaseDate(date) { date.setUTCMonth(date.getUTCMonth() - 3); } // Auxiliary function to enable/disable buttons function updateButtons() { if (groupALastInfo.index == 0) { document.getElementById("btnRemoveLastA").setAttribute("disabled", "disabled"); document.getElementById("btnRemoveAllA").setAttribute("disabled", "disabled"); }else { document.getElementById("btnRemoveLastA").removeAttribute("disabled"); document.getElementById("btnRemoveAllA").removeAttribute("disabled"); } if (groupBLastInfo.index == 0) { document.getElementById("btnRemoveLastB").setAttribute("disabled", "disabled"); document.getElementById("btnRemoveAllB").setAttribute("disabled", "disabled"); }else { document.getElementById("btnRemoveLastB").removeAttribute("disabled"); document.getElementById("btnRemoveAllB").removeAttribute("disabled"); } } //------------------------------------------------------------------ // Adding event markers //------------------------------------------------------------------ /* Function to add event marker to the group groupId {String} id of the group, in which event marker is added lastInfo {Object} link to the object with the last event marker added labelPrefix {String} prefix of the event markers caption (for example for "A0" prefix is "A") color: {String} color of the event markers text */ function addEventMarker(groupId, lastInfo, labelPrefix, color) { chart.addEventMarker(groupId, { date: lastInfo.date, // date is passed as string and formatted according to event markers input locale from config.xml format: labelPrefix + lastInfo.index.toString(), // format: prefix + index (for example "A0") it is shown in caption id: lastInfo.index.toString(), // unique (in group) id of event marker. If needed for later interaction with event marker settings: { font: { color: color, size: 10 } }, attributes: { markerCaption: labelPrefix + lastInfo.index.toString() // custom attribute, which is displayed in event markers tooltip } }); // apply the changes if (chart.commitEventMarkersChanges()) { // if successful... // update information about last marker lastInfo.index++; increaseDate(lastInfo.date); // update the buttons state updateButtons(); } } function addEventMarkerToGroupA() { // adding marker to A group. See addEventMarker reference for more. @see addEventMarker addEventMarker("idEventMarkersGroupA", groupALastInfo, "A", "#A80027"); } function addEventMarkerToGroupB() { // adding marker to B group. See addEventMarker reference for more. @see addEventMarker addEventMarker("idEventMarkersGroupB", groupBLastInfo, "B", "#0027A8"); } //------------------------------------------------------------------ // Removal of the last event marker //------------------------------------------------------------------ /* Function to remove event marker from the group groupId {String} id if the group lastInfo {Object} link to the object with information about the last event marker */ function removeLastEventMarker(groupId, lastInfo) { var lastId = lastInfo.index-1; if (lastId < 0) return; // Remove event marker by its id and group id. See removeEventMarker reference for more. @see removeEventMarker chart.removeEventMarker(groupId, lastId); // Apply changes if (chart.commitEventMarkersChanges()) { // if successful... // Update information about the last added event marker. Now it's next to last. lastInfo.index--; decreaseDate(lastInfo.date); // Update buttons state updateButtons(); } } function removeLastEventMarkerFromGroupA() { // Remove of the last event marker from A group. See removeLastEventMarker removeLastEventMarker("idEventMarkersGroupA", groupALastInfo); } function removeLastEventMarkerFromGroupB() { // Remove of the last event marker from B group. See removeLastEventMarker removeLastEventMarker("idEventMarkersGroupB", groupBLastInfo); } //------------------------------------------------------------------ // Removal of all event markers //------------------------------------------------------------------ /* Function to remove all event markers from the group groupId {String} id of the group lastInfo {Object} link to the object with information about last event marker */ function removeAllEvetMarkers(groupId, lastInfo) { /* We know id of the last event marker and this id is set using simple increment, so we just go by id from 0 to lastInfo.index */ for (var i = 0;i<lastInfo.index;i++) chart.removeEventMarker(groupId, i.toString()); // Apply changes if (chart.commitEventMarkersChanges()) {// if successful... // Reset info about last event marker, to start adding new from the beginning lastInfo.index = 0; lastInfo.date = new Date(Date.UTC(1999,10,1)); // Update buttons states updateButtons(); } } function removeAllEventMarkersFromGroupA() { // remove all markers from A group. See removeAllEventMarker removeAllEvetMarkers("idEventMarkersGroupA", groupALastInfo); } function removeAllEventMarkersFromGroupB() { // remove all markers from B group. See removeAllEventMarker removeAllEvetMarkers("idEventMarkersGroupB", groupBLastInfo); } </script> <!-- table and other page elements settings --> <style type="text/css"> table input { width: 120px; } 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; } </style> </head> <body> <div id="chartContainer"><!-- Chart Container --></div> <table class="settings"> <tr> <th>Group A</th> <th>Group B</th> </tr> <tr> <td><input type="button" value="Add New" onclick="addEventMarkerToGroupA()"/></td> <td><input type="button" value="Add New" onclick="addEventMarkerToGroupB()"/></td> </tr> <tr> <td><input id="btnRemoveLastA" type="button" disabled="disabled" value="Remove Last" onclick="removeLastEventMarkerFromGroupA()" /><br /></td> <td><input id="btnRemoveLastB" type="button" disabled="disabled" value="Remove Last" onclick="removeLastEventMarkerFromGroupB()" /><br /></td> </tr> <tr> <td><input id="btnRemoveAllA" type="button" disabled="disabled" value="Remove All" onclick="removeAllEventMarkersFromGroupA()" /><br /></td> <td><input id="btnRemoveAllB" type="button" disabled="disabled" value="Remove All" onclick="removeAllEventMarkersFromGroupB()" /><br /></td> </tr> </table> </body> </html>
Sample Description
How to use this sample?
In this sample you can add and remove markers to/from two groups clicking on "Add" and "Remove" buttons. Also, all added markers can be removed using "Remove all" buttons.
How it works
Event markers addings and removings are executed through addEventMarker and removeEventMarker methods. Changes in event markers are commited by commitEventMarkersChanges method call.
You can read more about event markers in Event Markers section.
AnyChartStock JavaScript API
This sample uses the following methods, properties and events from AnyChartStock JavaScript API:
Item | Type | Description |
---|---|---|
addEventMarker | Method | Adds new event marker to the specified group. |
commitEventMarkersChanges | Method | Commits all changes made to event markers by addEventMarker(), removeEventMarker(), addEventMarkerGroup(), removeEventMarkerGroup() method calls. |
removeEventMarker | Method | Removes event marker. |
setXMLFile | Method | Sets chart XML configuration file path. |
write | Method | Adds the chart to HTML DOM as a child of the specified container. |
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.
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.