Emprise Javascript Charts

Mark Point

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

  1. <script type=”text/javascript”>
  2.   // Save references to the ranges for easier access later
  3.   var markPoint = document.getElementById(“markPoint”);
  4.   var markPointImage = document.getElementById(“markPointImage”);
  5.   // This method will take the current zoom coordinates and position
  6.   // the given range
  7.   function positionMarker(bottom_zoom, left_zoom) {
  8.     // Determine if we have a selected point and hide or show the marker accordingly
  9.     if (markPoint.x == undefined || markPoint.y == undefined) {
  10.       markPoint.style.display = “none”;
  11.       return;
  12.     } else {
  13.       markPoint.style.display = “block”;
  14.     }
  15.     // Determine the pixel coordinates of the point to be marked
  16.     var left = chart.axis_bottom.pointToPixel(markPoint.x, true).point – 16;
  17.     var top = chart.axis_left.pointToPixel(markPoint.y, true).point – 16;
  18.     // Translate zoom coordinates into screen pixels
  19.     bottom_zoom = {
  20.       min: chart.axis_bottom.pointToPixel(bottom_zoom.min),
  21.       max: chart.axis_bottom.pointToPixel(bottom_zoom.max)
  22.     };
  23.     left_zoom = {
  24.       min: chart.axis_left.pointToPixel(left_zoom.min),
  25.       max: chart.axis_left.pointToPixel(left_zoom.max)
  26.     };
  27.     // Position the marker, adjusting styles and dimensions as necessary to make it
  28.     // partially visible when not fully within the bounds of the chart
  29.     if (((left + 32) < bottom_zoom.min) || (left > bottom_zoom.max) ||
  30.     ((top + 32) < left_zoom.max) || (top > left_zoom.min)) {
  31.       markPoint.style.display = “none”;
  32.     } else {
  33.       markPoint.style.display = “block”;
  34.       // If point is outside the visible range but the image should
  35.       // still be partially visible, adjust its width and image
  36.       // position so it appears to disappear beneath the axis /
  37.       // chart bounds
  38.       if (left < bottom_zoom.min) {
  39.         markPoint.style.left = bottom_zoom.min + “px”;
  40.         markPoint.style.width = 32 – (bottom_zoom.min – left) + “px”;
  41.         markPointImage.style.left = “-” + (bottom_zoom.min – left) + “px”;
  42.       } else if ((left + 32) > bottom_zoom.max) {
  43.         markPoint.style.left = left + “px”;
  44.         markPoint.style.width = (bottom_zoom.max – left) + “px”;
  45.         markPointImage.style.left = “0px”;
  46.       } else {
  47.         // Image is fully visible, position it and reset its width
  48.         markPoint.style.left = left + “px”;
  49.         markPoint.style.width = “32px”;
  50.         markPointImage.style.left = “0px”;
  51.       }
  52.       // If point is outside the visible range but the image should
  53.       // still be partially visible, adjust its width and image
  54.       // position so it appears to disappear beneath the axis /
  55.       // chart bounds
  56.       if (top < left_zoom.max) {
  57.         markPoint.style.top = left_zoom.max + “px”;
  58.         markPoint.style.height = 32 – (left_zoom.max – top) + “px”;
  59.         markPointImage.style.top = “-” + (left_zoom.max – top) + “px”;
  60.       } else if ((top + 32) > left_zoom.min) {
  61.         markPoint.style.top = top + “px”;
  62.         markPoint.style.height = (left_zoom.min – top) + “px”;
  63.         markPointImage.style.top = “0px”;
  64.       } else {
  65.         markPoint.style.top = top + “px”;
  66.         markPoint.style.height = “32px”;
  67.         markPointImage.style.top = “0px”;
  68.       }
  69.     }
  70.   };
  71.   // doDblClickPoint will be attached to the chart’s onDblClickPoint event and will
  72.   // register the clicked point so that it can be positioned as necessary
  73.   function doDblClickPoint(point, series, chart) {
  74.     // Record the point coordinates so that we can reposition if the chart is zoomed or moved
  75.     markPoint.x = point.x;
  76.     markPoint.y = point.y;
  77.     // Get the current coordinates of the chart area in axis-specific units
  78.     var bottom_zoom = chart.axis_bottom.getZoom();
  79.     var left_zoom = chart.axis_left.getZoom();
  80.     // Position the marker
  81.     positionMarker(bottom_zoom, left_zoom);
  82.     // Return false to cancel the zoom out (if applicable)
  83.     return false;
  84.   };
  85.   // doAfterDraw will be attached to the chart’s onAfterDraw event and
  86.   // move the markers into the correct position
  87.   function doAfterDraw() {
  88.     // Get the current coordinates of the chart area in axis-specific units
  89.     var bottom_zoom = chart.axis_bottom.getZoom();
  90.     var left_zoom = chart.axis_left.getZoom();
  91.     if (!isNaN(bottom_zoom.min) && !isNaN(bottom_zoom.max) &&
  92.     !isNaN(left_zoom.min) && !isNaN(left_zoom.max)) {
  93.       positionMarker(bottom_zoom, left_zoom);
  94.     }
  95.   }
  96.   var chart = new EJSC.Chart(“myChart1a”,
  97.     {
  98.       show_titlebar: false,
  99.       show_legend: false,
  100.       axis_bottom: {
  101.         size: 30,
  102.         caption: “Dates”,
  103.         extremes_ticks: true,
  104.         formatter: new EJSC.DateFormatter({format_string: “MM/DD<br/>HH:NN”})
  105.       },
  106.       axis_left: {
  107.         caption: “Level”,
  108.         min_extreme: -50,
  109.         max_extreme: 150,
  110.         formatter: new EJSC.NumberFormatter({forced_decimals: 1})
  111.       },
  112.       allow_zoom: true,
  113.       onAfterDraw: doAfterDraw,
  114.       onDblClickPoint: doDblClickPoint
  115.     }
  116.   );
  117.   var series1 = new EJSC.LineSeries(
  118.     new EJSC.ArrayDataHandler(randomArray(12, undefined, undefined, undefined, undefined, (1000*60*60*24), (new Date(“07/28/2008 00:00”)).getTime())),
  119.     {
  120.       drawPoints: true,
  121.       lineWidth: 2
  122.     }
  123.   );
  124.   chart.addSeries(series1);
  125. </script>
Spread the love