Filled Bars, Ranges and Custom Markers

Description

This chart displays two overlayed bar series. The first is formatted to appear as a max value and will be filled at each point by the second series. Two DIV elements have been added to mark the normal range of the chart and are positioned over the chart when it is drawn. Custom hints have been added to allow display of a single hint per X axis point which displays only the 'data' value for each point.

Too Low
Too High

Source Code

  1. <script type="text/javascript">
  2.   // Save references to the marks for easier access later
  3.   var mLow = document.getElementById("markerLow");
  4.   var mHigh = document.getElementById("markerHigh");
  5.   // doAfterDraw will be attached to the chart's onAfterDraw event and
  6.   // move the markers into the correct position
  7.   function doAfterDraw() {
  8.     // Determine the pixel coordinates of the left side of the chart at y value 15
  9.     var x_axis = chart.axis_bottom.getZoom();
  10.     var y_axis = chart.axis_left.getZoom();
  11.     coordsLeft = {
  12.       top: chart.axis_left.pointToPixel(15),
  13.       left: chart.axis_bottom.pointToPixel(x_axis.min)
  14.     };
  15.     // Determine the pixel coordinates of the right side of the chart at y value 15
  16.     coordsRight = {
  17.       top: chart.axis_left.pointToPixel(15),
  18.       left: chart.axis_bottom.pointToPixel(x_axis.max)
  19.     };
  20.     // If top (i.e. y value of 15) is not in view, it will be returned as undefined
  21.     // if that's the case, hide the low marker
  22.     if (coordsLeft.top == undefined) { mLow.style.display = "none"; }
  23.     else { mLow.style.display = "block"; }
  24.     var left = coordsLeft.left;
  25.     var width = coordsRight.left - coordsLeft.left + 60;
  26.     // Move the low marker into the correct position (accounting for its 14 pixel height)
  27.     mLow.style.top = coordsLeft.top - 14 + "px";
  28.     mLow.style.left = left + "px";
  29.     mLow.style.width = width + "px";
  30.     // Determine the pixel coordinates of y value 90
  31.     coordsLeft = {
  32.       top: chart.axis_left.pointToPixel(90),
  33.       left: chart.axis_bottom.pointToPixel(chart.x_min)
  34.     };;
  35.     // If top (i.e. y value of 90) is not in view, it will be returned as undefined
  36.     // if that's the case, hide the high marker
  37.     if (coordsLeft.top == undefined) { mHigh.style.display = "none"; }
  38.     else { mHigh.style.display = "block"; }
  39.     // Move the high marker into the correct position
  40.     mHigh.style.top = coordsLeft.top - 14 + "px";
  41.     mHigh.style.left = left + "px";
  42.     mHigh.style.width = width + "px";    
  43.   }
  44.   window.onload = doAfterDraw;
  45.   // doShowHint will be attached to the chart's onShowHint event and allow
  46.   // us to show all hints at the top of the container bar (even when the mouse
  47.   // is over or has clicked on a value bar) as well as customize the display
  48.   // to show colored values based on the bars
  49.   function doShowHint(point, series, chart, hint, sticky) {
  50.     // Get the current Y value of the related bar in the barValueSeries
  51.     var currentYValue = barValuesSeries.findClosestByPoint({ x: point.x, y: 0 }).y;
  52.     var className;
  53.     if (currentYValue < 15) { className = "yellow"; }
  54.     else if (currentYValue < 90) { className = "green"; }
  55.     else { className = "red"; }
  56.     // Modify the hint window to display maximum value and current value
  57.     return '[x]: <strong class="' + className + '">' + currentYValue + '</strong>';
  58.   }
  59.   function doBeforeSelectPoint(point, series, chart, hint, sticky) {
  60.     // Find out if the point is in the container series or bar series
  61.     if (series == barValuesSeries) {
  62.       // hint is triggered from the value series, find the same bar in the
  63.       // container series and select it instead
  64.       chart.selectPoint(barContainersSeries.findClosestByPoint({ x: point.x, y: point.y }), (sticky != "hover"));
  65.       // Cancel selection of this point
  66.       return false;
  67.     }
  68.   }
  69.   var chart = new EJSC.Chart("myChart1a",
  70.     {
  71.       show_titlebar: false,
  72.       show_legend: false,
  73.       axis_bottom: { caption: "", extremes_ticks: true },
  74.       axis_left: { caption: "Level" },
  75.       allow_zoom: true,
  76.       onAfterDraw: doAfterDraw,
  77.       onShowHint: doShowHint,
  78.       onBeforeSelectPoint: doBeforeSelectPoint
  79.     }
  80.   );
  81.   // Modify extremes for a little bit of padding on the top
  82.   chart.axis_left.setExtremes(0,105);
  83.   // Define colors for bar ranges
  84.   var barRanges = [
  85.     { min: 0, max: 14, color: "rgb(255,255,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 },  
  86.     { min: 15, max: 90, color: "rgb(0,216,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 },  
  87.     { min: 90, max: 100, color: "rgb(255,0,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 }
  88.   ];  
  89.   var barValuesSeries = new EJSC.BarSeries(
  90.     new EJSC.ArrayDataHandler([
  91.       ["Tank A",60],["Tank B",50],["Tank C",55],
  92.       ["Tank D",95],["Tank E",56],["Tank F",10]
  93.     ]),
  94.     {
  95.       // Turn off grouping so that the series overlay
  96.       groupedBars: false,
  97.       intervalOffset: 0.9,
  98.       ranges: barRanges
  99.     }
  100.   );
  101.   // barContainersSeries will define the empty bars which appear to be filled
  102.   // with the bars defined by barValuesSeries
  103.   var barContainersSeries = new EJSC.BarSeries(
  104.     new EJSC.ArrayDataHandler([
  105.       ["Tank A",100],["Tank B",100],["Tank C",100],
  106.       ["Tank D",100],["Tank E",100],["Tank F",100]
  107.     ]),
  108.     {
  109.       // Style bars with thicker borders and no fill
  110.       lineWidth: 3,
  111.       color: "rgb(0,0,0)",
  112.       intervalOffset: 0.9,
  113.       opacity: 10
  114.     }
  115.   );
  116.   chart.addSeries(barValuesSeries);
  117.   chart.addSeries(barContainersSeries);
  118. </script>