Emprise Javascript Charts

Pie Piece Labels – Displaying hints for each piece

Description

This chart contains a PieSeries with a label centered over each piece.

Source Code

  1. <script type=”text/javascript”>
  2.   //
  3.   // Define the CSS in the header:
  4.   // <style type=”text/css” media=”screen,print”>
  5.   //     .PiePieceLabel { position: absolute; background-color: #fff; border: 1px solid #999; padding: 2px;
  6.   //    opacity: 0.8; filter: Alpha(opacity:80); }
  7.   // </style>
  8.   //
  9.   // Define variables to store references to the chart and series
  10.   var myChart, mySeries = undefined;
  11.   // After drawing is complete, create and/or reposition pie piece captions
  12.   function doAfterDraw(chart) {
  13.     // Verify series has been created
  14.     if (mySeries != undefined) {
  15.       var piePoints = mySeries.getPoints();
  16.       // Loop through the list of pie series points
  17.       for (var i = 0; i < piePoints.length; i++) {
  18.         // We’re saving the reference to the div object in the point,
  19.         // so check if it already exists
  20.         if (piePoints[i].hintDiv == undefined) {
  21.           // Create the div
  22.           piePoints[i].hintDiv = document.createElement(“DIV”);
  23.           piePoints[i].hintDiv.className = “PiePieceLabel”;
  24.           piePoints[i].hintDiv.innerHTML = “<strong>” + piePoints[i].label + “</strong><br/>” + piePoints[i].x + ” of ” + mySeries.getTotalValue() +
  25.             ” (” + (piePoints[i].x / mySeries.getTotalValue() * 100) + “%)”;
  26.           document.getElementsByTagName(“body”)[0].appendChild(piePoints[i].hintDiv);
  27.         }
  28.         // Find the center of the piece
  29.         var pointCoordinates = mySeries.findCenter(piePoints[i]);
  30.         // Adjust the location of the div based on the piece center
  31.         piePoints[i].hintDiv.style.left = pointCoordinates.x – Math.floor(piePoints[i].hintDiv.offsetWidth / 2) + “px”;
  32.         piePoints[i].hintDiv.style.top = pointCoordinates.y – Math.floor(piePoints[i].hintDiv.offsetHeight / 2) + “px”;
  33.       }
  34.     }
  35.   };
  36.   myChart = new EJSC.Chart(“myChart1a”,
  37.     {
  38.       show_legend: false,
  39.       allow_zoom: false,
  40.       show_mouse_position: false,
  41.       // Hide the standard hints
  42.       show_hints: false,
  43.       // Assign the onAfterDraw event
  44.       onAfterDraw: doAfterDraw
  45.     }
  46.   );
  47.   mySeries = new EJSC.PieSeries(
  48.     new EJSC.ArrayDataHandler(
  49.       [[10, “Piece 1”],[20, “Piece 2”],[45, “Piece 3″],[25,”Piece 4”]]
  50.     )
  51.   );
  52.   myChart.addSeries(mySeries);
  53. </script>
Spread the love