<html> <head> <title>Using Event Markers Custom Attributes</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <!--- This sample uses jquery library to work with DOM --> <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> <style type="text/css"> html, body { background-color: white; } /* chart size */ #chartContainer { width: 800px; height: 500px; } /* other settings */ 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: #ffffff; font:normal 70% Verdana; padding-bottom:4px; padding-top:0; padding-left:4px; text-align:left; } /* Event table list settings */ /* firefox outline fix */ * { outline: none; } #info { width: 400px; height: 490px; overflow-y: scroll; padding-top: 10px; background-color: white; } .event { width: 354px; height: 50px; border: 1px solid #d2d2d2; padding: 10px; margin-bottom: 5px; cursor: hand; } #info td { background: none; padding: 0px; margin: 0px; } .event .icon { width: 37px; } .event .icon p { width: 27px; height: 26px; padding: 0; margin: 0; background-image: url(letters_strip.png); background-repeat: no-repeat; } .event .iconSelected { background-position: 0px 0px; } .event .iconNormal { background-position: 0px -29px; } .event .title a, .event .title a:visited { color: #1e399d; font: 8pt Verdana; } a { text-decoration: none; } .event .date a, .date .event a:visited { color: #595959; } .event .date a:hover, .event .title a:hover { text-decoration: underline; } .event .date { padding-top: 5px !important; font: 8pt Verdana; } </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"); // needConfig allows to use chart object model, which is needed to get information about event markers chart.needConfig = true; // Setting XML config file. chart.setXMLFile("config.xml"); //-------------------------------------------------------------------------------- // Auxiliary Date Time Functions //-------------------------------------------------------------------------------- var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; // Function to format date according to "%MMM %dd, %yyyy" mask. (e.g. 15 May, 2010) function formatDate(date) { return months[date.getUTCMonth()] + " "+date.getUTCDate()+", "+date.getUTCFullYear(); } //-------------------------------------------------------------------------------- // Obtain event markers from the chart object model and show them on the page //-------------------------------------------------------------------------------- // Listen to onChartDraw, get event markers from object model and show them on the page chart.onChartDraw = function() { // Get all event markers from zero indexed group var eventMarkers = chart.objectModel.eventMarkers.groups[0].events; // show them for (var i = eventMarkers.length-1;i>=0;i--) { // get link to event marker info var eventMarker = eventMarkers[i]; // create list element var htmlCode = "<div class=\"event\" id=\"e_"+eventMarker.id+"\">" // event markers id +" <table cellspacing=\"0\" cellpadding=\"0\">" +" <tr valign=\"top\">" +" <td class=\"icon\"><a href=\"#\"><p class=\"iconNormal\"> </p></a></td>" +" <td class=\"title\"><a href=\""+eventMarker.attributes.Link + "\" target=\"_blank\">"+eventMarker.attributes.Title+"</a></td>" // Title attribute (see config.xml) +" </tr>" +" <tr>" +" <td> </td>" +" <td class=\"date\"><a href=\"http://oracle.com\" target=\"_blank\">www.oracle.com</a> - "+formatDate(eventMarker.date)+"</td>" //Link attribute (see config.xml) and date +" </tr>" +" </table>" +" </div>"; // and add it into <div id="info"/> $("#info").append(htmlCode); // now we format this element and add event liseners to it // get link to this div (it contains event marker info) var e = $("#e_"+eventMarker.id); // store its vertical offset to offset_top (we need for scrolling) e.attr("offset_top",e.position().top); // store event marker in event_id attribute e.attr("event_id",eventMarker.id); // store event marker in event_id attribute of the link e.find(".icon a").attr("event_id",eventMarker.id); // set letter (A-Z) e.find(".icon p").css("backgroundPosition","-"+(30*(i%26))+"px -29px"); // set click handler e.click(function() { // in the handler we get event marker id from event_id attribute var eventId = $(this).attr("event_id"); // and select it on the chart chart.selectEventMarker("news", eventId); // and highlight its representation on the page selectEventMarker(eventId, true); }); } // reset the handler chart.onChartDraw = null; }; //-------------------------------------------------------------------------------- // Select/deselect event markers //-------------------------------------------------------------------------------- // listen to event marker select event chart.onEventMarkerSelect = function(eventMarkerInfo) { selectEventMarker(eventMarkerInfo.id, false); }; // id of the currently selected event marker var selectedMarker = null; // Deselect marker by its id function deselectEventMarker(id) { // find the div with information about this marker and find icon of the letter in it var e = $("#e_"+id+" .icon p"); var pos = e.css("backgroundPosition"); var posXY = pos.split(" "); posXY[1] = "-29px"; // set icon as unselected (all icons in one file - so we just set an offset) e.css("backgroundPosition", posXY.join(" ")); $("#e_"+id).css("background-color","White"); } // Select event marker by id function selectEventMarker(id, scrollChart) { // If any event marker is selected - deselect it if (selectedMarker != null) deselectEventMarker(selectedMarker); // store the currently selected event marker id selectedMarker = id; $("#e_"+id).css("background-color","#EFE9E4"); // find the div with information about this marker and set icon as selected var e = $("#e_"+id+" .icon p"); var pos = e.css("backgroundPosition"); var posXY = pos.split(" "); posXY[1] = "0px"; // set icon as selected (all icons in one file - so we just set an offset) e.css("backgroundPosition", posXY.join(" ")); // scroll list to make div visible var top = $("#e_"+id).attr("offset_top")-51; // (using jQuery.animate) $("#info").animate({scrollTop:top}, 500); // check if we can scrolll the chart? if (scrollChart) { // find event marker in objectModel by id var group = chart.getEventMarkersGroupById("news"); var info = null; for (var i = 0;i<group.events.length;i++) { if (group.events[i].id == id) { info = group.events[i]; break; } } // if nothing found - nothing to do if (!info) return; // get timestamps of visible range var firstVisibleDate = chart.getFirstVisibleDate().getTime(); var lastVisibleDate = chart.getLastVisibleDate().getTime(); // get timestamp of the event marker to be shown var date = info.date.getTime(); // get the width of the visible range in milliseconds var range = lastVisibleDate - firstVisibleDate; // if event marker isn't visible - scroll chart to make it visible if (date < firstVisibleDate) { scrollChartToDate(new Date(date - range/10)); }else if (date > lastVisibleDate) { scrollChartToDate(new Date(date - range/10)); } } } //-------------------------------------------------------------------------------- // Chart Scrolling //-------------------------------------------------------------------------------- // Function to scroll to the date animated function scrollChartToDate(date) { var start = chart.getFirstVisibleDate().getTime(); // current position var end = date.getTime(); // position to scroll to // distance in milliseconds var distance = end - start; // animation step var step = Math.round(distance/10); // animated scroll to new date var interval; var scrollingF = function() { // if we are scrolling to left and got to the end date - stop animation if (step < 0 && start <= end) { chart.scrollTo(new Date(end)); clearInterval(interval); }else if (step > 0 && start >= end) { // if we are scrolling to right and got to the end date - stop animation chart.scrollTo(new Date(end)); clearInterval(interval); }else { // in other case just change scroll to intermediate point start += step; chart.scrollTo(new Date(start)); } } // start animating interval = setInterval(scrollingF, 10); scrollingF(); } // Writing the flash object into the page DOM. chart.write("chartContainer"); </script> </head> <body> <table> <tr valign="top"> <td> <table class="settings"> <tr> <th>Historical Prices for Oracle Corporation: 2007-2010<br> Select event markers on the chart to see event details in the list.</th> </tr> <tr> <td id="chartContainer"><!-- Chart Container --></td> </tr> </table> </td> <td> <table class="settings"> <tr> <th>Key Developments<br> Click letter icons to see event on the chart timeline.</th> </tr> <tr> <td><div id="info"></div></td> </tr> </table> </td> </tr> </table> </body> </html>
Sample Description
How to use this sample?
Select event markers on the chart to see event details in the list - the list scrolls to the event description if needed. Also you can click on the letter icon in the list to scroll the chart and select event marker on it.
How it works
This sample
In this sample when onChartDraw event happens chart object model is used to get infomation about event markers from the chart.
When the information is gathered the list is created and onEventMarkerSelect event is listened to show list entries when needed, at the same time scrollTo and selectEventMarker methods are used to show and highlight event markers when user interacts with the list.
You can read more about event markers in Event Markers section and about object model in Updating Chart using Object Model article.
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. |
objectModel | Property (Object) | Gets chart configuration as an Object. |
getEventMarkersGroupById | Method | Returns the link to event marker group object. |
getFirstVisibleDate | Method | Gets the first visible date. |
getLastVisibleDate | Method | Gets the last visible date. |
scrollTo | Method | Scrolls the chart to a given date. |
selectEventMarker | Method | Selects an event marker by its id and its group 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. |
onEventMarkerSelect | Event | This event is dispatched when an event marker is selected. |
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.
Additional Files
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.