Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning jqPlot
  • Table Of Contents Toc
Learning jqPlot

Learning jqPlot

By : Gottreu
5 (1)
close
close
Learning jqPlot

Learning jqPlot

5 (1)
By: Gottreu

Overview of this book

If you are a developer with a good understanding of JavaScript and jQuery and have been burdened with the task of analyzing and presenting some data, this book will provide you with the start you need to create some very attractive data visualizations.
Table of Contents (14 chapters)
close
close
13
Index

Adding multiple data series

This is a nice start, but we both agree that the leadership is going to want more than just a couple of days of revenue on a chart. Our next chart will display the profit and revenue numbers for the last 12 months. We will only need to make a few adjustments to our previous chart:

  1. We include both arrays containing our revenue and profit figures:
    <script src="../js/jqplot.dateAxisRenderer.min.js"></script>
    
    <script>
    $(document).ready(function(){
    
      var revenue = [['2011-11-20', 800538], ['2011-12-20', 804879], ['2012-01-20', 847732], ['2012-02-20', 795758], ['2012-03-20', 835554], ['2012-04-20', 844379], ['2012-05-20', 828510], ['2012-06-20', 753984], ['2012-07-20', 807403], ['2012-08-20', 755840], ['2012-09-20', 775304], ['2012-10-20', 781322]];
    
      var profit = [['2011-11-20', 192049.56], ['2011-12-20', 188744.75], ['2012-01-20', 197352.54], ['2012-02-20', 190106.74], ['2012-03-20', 193453.07], ['2012-04-20', 197249.69], ['2012-05-20', 205480.18], ['2012-06-20', 177648.78], ['2012-07-20', 193793.82], ['2012-08-20', 183221.56], ['2012-09-20', 192797.31], ['2012-10-20', 182451.68]];
  2. We modify the variable name for our object and create a new ID attribute. Next, we combine both arrays into a container array and pass it into jqPlot. Within jqPlot, each array containing data is called a series and both series will appear on the same y axis:
      var rev_profit = $.jqplot ('revenueProfitChart', [revenue, profit],
      {
  3. We modify the title and the labels for our axes. We also update the ID of our div element:
        title:'Monthly Revenue & Profit',
        axes:{
          xaxis:{
            renderer:$.jqplot.DateAxisRenderer,
            label: 'Months'
          },
          yaxis:{
            label: 'Totals Dollars',
            tickOptions: { formatString: "$%'d" } 
          }
        }
      });
    });
    </script>
    <div id="revenueProfitChart" style="width:600px;"></div>

We load the page in the browser and smile. The changes to the chart get us closer to what the management is looking for. We can see the results of our efforts in the following figure.

Adding multiple data series

Adding multiple y axes

The two data series are really far apart in their values. It's hard to decipher the value of each point by just looking at the chart. We'll put each series on its own y axis. This will make it easier to see interactions between our revenue and profit. We revisit the code and begin to alter it to separate the y axes.

  1. We start by adding the series option. It is an array containing an object for each data series. For the first series, we can leave the default settings in place. So, we simply pass in an empty object. The second series will be on the second y axis, which means that we enter y2axis for the yaxis option. The ticks and label for this axis will appear on the right-hand side of our chart:
      var rev_profit = $.jqplot ('revenueProfitChart', [revenue, profit],
      {
        title:'Monthly Revenue & Profit',
        series:[
          {},
          {yaxis:'y2axis'}
        ],
    
  2. We change the label for yaxis to Revenue. We copy all these options for y2axis. We then change the axis option and the label:
        axes:{
          xaxis:{
            renderer:$.jqplot.DateAxisRenderer,
            label: 'Months'
          },
          yaxis:{
            label: 'Revenue',
            tickOptions: { formatString: "$%'d" } 
          },
          y2axis:{
            label: 'Profit',
            tickOptions: { formatString: "$%'d" } 
          }
        }
      });

We load this new chart in our browser. The fruit of our labors can be seen in the following figure:

Adding multiple y axes

We begin to study the chart. It appears that profit as a part of revenue is better in some months when compared to other months, but we can't draw any clear conclusions from this chart. However, this can be a springboard to try to track down some correlations.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning jqPlot
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon