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
- <script type="text/javascript">
- // Save references to the marks for easier access later
- var mLow = document.getElementById("markerLow");
- var mHigh = document.getElementById("markerHigh");
- // doAfterDraw will be attached to the chart's onAfterDraw event and
- // move the markers into the correct position
- function doAfterDraw() {
- // Determine the pixel coordinates of the left side of the chart at y value 15
- var x_axis = chart.axis_bottom.getZoom();
- var y_axis = chart.axis_left.getZoom();
- coordsLeft = {
- top: chart.axis_left.pointToPixel(15),
- left: chart.axis_bottom.pointToPixel(x_axis.min)
- };
- // Determine the pixel coordinates of the right side of the chart at y value 15
- coordsRight = {
- top: chart.axis_left.pointToPixel(15),
- left: chart.axis_bottom.pointToPixel(x_axis.max)
- };
- // If top (i.e. y value of 15) is not in view, it will be returned as undefined
- // if that's the case, hide the low marker
- if (coordsLeft.top == undefined) { mLow.style.display = "none"; }
- else { mLow.style.display = "block"; }
- var left = coordsLeft.left;
- var width = coordsRight.left - coordsLeft.left + 60;
- // Move the low marker into the correct position (accounting for its 14 pixel height)
- mLow.style.top = coordsLeft.top - 14 + "px";
- mLow.style.left = left + "px";
- mLow.style.width = width + "px";
- // Determine the pixel coordinates of y value 90
- coordsLeft = {
- top: chart.axis_left.pointToPixel(90),
- left: chart.axis_bottom.pointToPixel(chart.x_min)
- };;
- // If top (i.e. y value of 90) is not in view, it will be returned as undefined
- // if that's the case, hide the high marker
- if (coordsLeft.top == undefined) { mHigh.style.display = "none"; }
- else { mHigh.style.display = "block"; }
- // Move the high marker into the correct position
- mHigh.style.top = coordsLeft.top - 14 + "px";
- mHigh.style.left = left + "px";
- mHigh.style.width = width + "px";
- }
- window.onload = doAfterDraw;
- // doShowHint will be attached to the chart's onShowHint event and allow
- // us to show all hints at the top of the container bar (even when the mouse
- // is over or has clicked on a value bar) as well as customize the display
- // to show colored values based on the bars
- function doShowHint(point, series, chart, hint, sticky) {
- // Get the current Y value of the related bar in the barValueSeries
- var currentYValue = barValuesSeries.findClosestByPoint({ x: point.x, y: 0 }).y;
- var className;
- if (currentYValue < 15) { className = "yellow"; }
- else if (currentYValue < 90) { className = "green"; }
- else { className = "red"; }
- // Modify the hint window to display maximum value and current value
- return '[x]: <strong class="' + className + '">' + currentYValue + '</strong>';
- }
- function doBeforeSelectPoint(point, series, chart, hint, sticky) {
- // Find out if the point is in the container series or bar series
- if (series == barValuesSeries) {
- // hint is triggered from the value series, find the same bar in the
- // container series and select it instead
- chart.selectPoint(barContainersSeries.findClosestByPoint({ x: point.x, y: point.y }), (sticky != "hover"));
- // Cancel selection of this point
- return false;
- }
- }
- var chart = new EJSC.Chart("myChart1a",
- {
- show_titlebar: false,
- show_legend: false,
- axis_bottom: { caption: "", extremes_ticks: true },
- axis_left: { caption: "Level" },
- allow_zoom: true,
- onAfterDraw: doAfterDraw,
- onShowHint: doShowHint,
- onBeforeSelectPoint: doBeforeSelectPoint
- }
- );
- // Modify extremes for a little bit of padding on the top
- chart.axis_left.setExtremes(0,105);
- // Define colors for bar ranges
- var barRanges = [
- { min: 0, max: 14, color: "rgb(255,255,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 },
- { min: 15, max: 90, color: "rgb(0,216,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 },
- { min: 90, max: 100, color: "rgb(255,0,0)", opacity: 80, lineOpacity: 80, lineWidth: 0 }
- ];
- var barValuesSeries = new EJSC.BarSeries(
- new EJSC.ArrayDataHandler([
- ["Tank A",60],["Tank B",50],["Tank C",55],
- ["Tank D",95],["Tank E",56],["Tank F",10]
- ]),
- {
- // Turn off grouping so that the series overlay
- groupedBars: false,
- intervalOffset: 0.9,
- ranges: barRanges
- }
- );
- // barContainersSeries will define the empty bars which appear to be filled
- // with the bars defined by barValuesSeries
- var barContainersSeries = new EJSC.BarSeries(
- new EJSC.ArrayDataHandler([
- ["Tank A",100],["Tank B",100],["Tank C",100],
- ["Tank D",100],["Tank E",100],["Tank F",100]
- ]),
- {
- // Style bars with thicker borders and no fill
- lineWidth: 3,
- color: "rgb(0,0,0)",
- intervalOffset: 0.9,
- opacity: 10
- }
- );
- chart.addSeries(barValuesSeries);
- chart.addSeries(barContainersSeries);
- </script>