Multiple Axes

Properties Demonstrated

  • Series.x_axis: "top" | "bottom"
  • Series.y_axis: "left" | "right"

Desired Result

Multiple axes can be used to compare data with a common Y scale but different X scales such as page views vs. unique visotors over time. Implementing this functionality in Emprise JavaScript Charts is as simple as setting the x_axis or y_axis property within a series to the side on the chart the scale should be displayed.

Source Code

  1. <script type="text/javascript">
  2.   var chart = new EJSC.Chart("myChart1a", {
  3.     // Define the properties for the bottom (common) axis  
  4.     axis_bottom: {
  5.       caption: "Date",
  6.       formatter: new EJSC.DateFormatter({ format_string: "MM/DD" })
  7.     },
  8.     // Define the left axis, min/max_extreme are used to control the scale
  9.     // and make sure it matches up nicely with the left as far as grid lines
  10.     // and tick marks
  11.     axis_left: {
  12.       caption: "Pages",
  13.       min_extreme: 0,
  14.       max_extreme: 1000
  15.     },
  16.     // Define the right axis, again setting manual extremes so that the ticks
  17.     // align themselves nicely. This is not required for the axes to work but
  18.     // if these numbers are available it will make the chart look better
  19.     axis_right: {
  20.       caption: "Visitors",
  21.       min_extreme: 0,
  22.       max_extreme: 100,
  23.       // Since we've aligned the scales, there is no need to show two sets
  24.       // of grid lines, so hide this axis'
  25.       grid: {
  26.         show: false
  27.       }
  28.     }
  29.   });
  30.   // Create and add the first series. The default x and y axes used are bottom and left
  31.   // accordingly so they do not need to be set. If you want to add them for clarity
  32.   // it would be:
  33.   //   x_axis: "bottom",
  34.   //  y_axis: "left"
  35.   var hitsSeries = new EJSC.LineSeries(new EJSC.ArrayDataHandler(randomArray("days", 0, 1000)), {
  36.     title: "Average Page Views"
  37.   });
  38.   chart.addSeries(hitsSeries);
  39.   // Create and add the second series. The default x axis is already bottom so it does
  40.   // not need to be set. If you want to add it for clarity it would be:
  41.   //   x_axis: "bottom"
  42.   // We have to manually set the y_axis in order for it to be show up
  43.   var visitorsSeries = new EJSC.LineSeries(new EJSC.ArrayDataHandler(randomArray("days", 0, 100)), {
  44.     title: "Unique Visitors",
  45.     y_axis: "right"
  46.   });
  47.   chart.addSeries(visitorsSeries);
  48. </script>