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
- <script type="text/javascript">
- var chart = new EJSC.Chart("myChart1a", {
- // Define the properties for the bottom (common) axis
- axis_bottom: {
- caption: "Date",
- formatter: new EJSC.DateFormatter({ format_string: "MM/DD" })
- },
- // Define the left axis, min/max_extreme are used to control the scale
- // and make sure it matches up nicely with the left as far as grid lines
- // and tick marks
- axis_left: {
- caption: "Pages",
- min_extreme: 0,
- max_extreme: 1000
- },
- // Define the right axis, again setting manual extremes so that the ticks
- // align themselves nicely. This is not required for the axes to work but
- // if these numbers are available it will make the chart look better
- axis_right: {
- caption: "Visitors",
- min_extreme: 0,
- max_extreme: 100,
- // Since we've aligned the scales, there is no need to show two sets
- // of grid lines, so hide this axis'
- grid: {
- show: false
- }
- }
- });
- // Create and add the first series. The default x and y axes used are bottom and left
- // accordingly so they do not need to be set. If you want to add them for clarity
- // it would be:
- // x_axis: "bottom",
- // y_axis: "left"
- var hitsSeries = new EJSC.LineSeries(new EJSC.ArrayDataHandler(randomArray("days", 0, 1000)), {
- title: "Average Page Views"
- });
- chart.addSeries(hitsSeries);
- // Create and add the second series. The default x axis is already bottom so it does
- // not need to be set. If you want to add it for clarity it would be:
- // x_axis: "bottom"
- // We have to manually set the y_axis in order for it to be show up
- var visitorsSeries = new EJSC.LineSeries(new EJSC.ArrayDataHandler(randomArray("days", 0, 100)), {
- title: "Unique Visitors",
- y_axis: "right"
- });
- chart.addSeries(visitorsSeries);
- </script>