Custom Point Markers
Description (this example requires v2.0.1)
This chart displays a line series and implements the onDblClickPoint event to trigger the marking of the clicked point. When a user double clicks a point, or single clicks a point and presses the enter key, the event handler marks/un-marks the point by positioning a custom image which stays in place as the chart is zoomed and moved and point selection changes.
Double click a point below to mark it:
Source Code
- <script type=”text/javascript”>
- // Save references to the ranges for easier access later
- var markPoint = document.getElementById(“markPoint”);
- var markPointImage = document.getElementById(“markPointImage”);
- // This method will take the current zoom coordinates and position
- // the given range
- function positionMarker(bottom_zoom, left_zoom) {
- // Determine if we have a selected point and hide or show the marker accordingly
- if (markPoint.x == undefined || markPoint.y == undefined) {
- markPoint.style.display = “none”;
- return;
- } else {
- markPoint.style.display = “block”;
- }
- // Determine the pixel coordinates of the point to be marked
- var left = chart.axis_bottom.pointToPixel(markPoint.x, true).point – 16;
- var top = chart.axis_left.pointToPixel(markPoint.y, true).point – 16;
- // Translate zoom coordinates into screen pixels
- bottom_zoom = {
- min: chart.axis_bottom.pointToPixel(bottom_zoom.min),
- max: chart.axis_bottom.pointToPixel(bottom_zoom.max)
- };
- left_zoom = {
- min: chart.axis_left.pointToPixel(left_zoom.min),
- max: chart.axis_left.pointToPixel(left_zoom.max)
- };
- // Position the marker, adjusting styles and dimensions as necessary to make it
- // partially visible when not fully within the bounds of the chart
- if (((left + 32) < bottom_zoom.min) || (left > bottom_zoom.max) ||
- ((top + 32) < left_zoom.max) || (top > left_zoom.min)) {
- markPoint.style.display = “none”;
- } else {
- markPoint.style.display = “block”;
- // If point is outside the visible range but the image should
- // still be partially visible, adjust its width and image
- // position so it appears to disappear beneath the axis /
- // chart bounds
- if (left < bottom_zoom.min) {
- markPoint.style.left = bottom_zoom.min + “px”;
- markPoint.style.width = 32 – (bottom_zoom.min – left) + “px”;
- markPointImage.style.left = “-” + (bottom_zoom.min – left) + “px”;
- } else if ((left + 32) > bottom_zoom.max) {
- markPoint.style.left = left + “px”;
- markPoint.style.width = (bottom_zoom.max – left) + “px”;
- markPointImage.style.left = “0px”;
- } else {
- // Image is fully visible, position it and reset its width
- markPoint.style.left = left + “px”;
- markPoint.style.width = “32px”;
- markPointImage.style.left = “0px”;
- }
- // If point is outside the visible range but the image should
- // still be partially visible, adjust its width and image
- // position so it appears to disappear beneath the axis /
- // chart bounds
- if (top < left_zoom.max) {
- markPoint.style.top = left_zoom.max + “px”;
- markPoint.style.height = 32 – (left_zoom.max – top) + “px”;
- markPointImage.style.top = “-” + (left_zoom.max – top) + “px”;
- } else if ((top + 32) > left_zoom.min) {
- markPoint.style.top = top + “px”;
- markPoint.style.height = (left_zoom.min – top) + “px”;
- markPointImage.style.top = “0px”;
- } else {
- markPoint.style.top = top + “px”;
- markPoint.style.height = “32px”;
- markPointImage.style.top = “0px”;
- }
- }
- };
- // doDblClickPoint will be attached to the chart’s onDblClickPoint event and will
- // register the clicked point so that it can be positioned as necessary
- function doDblClickPoint(point, series, chart) {
- // Record the point coordinates so that we can reposition if the chart is zoomed or moved
- markPoint.x = point.x;
- markPoint.y = point.y;
- // Get the current coordinates of the chart area in axis-specific units
- var bottom_zoom = chart.axis_bottom.getZoom();
- var left_zoom = chart.axis_left.getZoom();
- // Position the marker
- positionMarker(bottom_zoom, left_zoom);
- // Return false to cancel the zoom out (if applicable)
- return false;
- };
- // doAfterDraw will be attached to the chart’s onAfterDraw event and
- // move the markers into the correct position
- function doAfterDraw() {
- // Get the current coordinates of the chart area in axis-specific units
- var bottom_zoom = chart.axis_bottom.getZoom();
- var left_zoom = chart.axis_left.getZoom();
- if (!isNaN(bottom_zoom.min) && !isNaN(bottom_zoom.max) &&
- !isNaN(left_zoom.min) && !isNaN(left_zoom.max)) {
- positionMarker(bottom_zoom, left_zoom);
- }
- }
- var chart = new EJSC.Chart(“myChart1a”,
- {
- show_titlebar: false,
- show_legend: false,
- axis_bottom: {
- size: 30,
- caption: “Dates”,
- extremes_ticks: true,
- formatter: new EJSC.DateFormatter({format_string: “MM/DD<br/>HH:NN”})
- },
- axis_left: {
- caption: “Level”,
- min_extreme: -50,
- max_extreme: 150,
- formatter: new EJSC.NumberFormatter({forced_decimals: 1})
- },
- allow_zoom: true,
- onAfterDraw: doAfterDraw,
- onDblClickPoint: doDblClickPoint
- }
- );
- var series1 = new EJSC.LineSeries(
- new EJSC.ArrayDataHandler(randomArray(12, undefined, undefined, undefined, undefined, (1000*60*60*24), (new Date(“07/28/2008 00:00”)).getTime())),
- {
- drawPoints: true,
- lineWidth: 2
- }
- );
- chart.addSeries(series1);
- </script>