LanguagesJavaScriptCharting Amazon Sales Ranks with the Grafico

Charting Amazon Sales Ranks with the Grafico

Writing a book is a long and difficult process — one which is so laborious that most authors wonder where in the world they will find the energy to actually promote it after the writing is over! But these days it’s more important than ever not only to promote new books actively, but also to gauge the success of promotional activities. Never before has the old saying “If you are not measuring it, you are not managing it” rung more true.

For many authors, one useful measuring stick is a book’s Amazon sales rank (see Figure 1). A sales rank is associated with every book sold on Amazon.com; the lower the sales rank, the more popular (in terms of unit sales) the book at that given point in time. I specify “given point in time” because sales ranks are volatile, often changing daily and even hourly in accordance with sales volume fluctuations. While Amazon doesn’t publish specifics regarding any information regarding the correlation between sales ranks and unit sales, publishers and authors alike have put a great deal of work into deciphering the algorithm. Many analyses are available online; however, one of my favorites is published here.

Chart Amazon Sales Ranks with the Grafico Library

Being a lazy programmer, I’ve never been interested in navigating to my books’ various Amazon pages on a daily basis to examine the ranks. Further, charting the rank fluctuations over a long period of time can be useful for determining the efficacy of book reviews, advertising and other promotional activities. In this article, I’ll focus on how I implemented the latter feature, showing you how I used the Grafico charting library to produce visual representations of sales ranks.

Incidentally, before jumping into the Grafico introduction, I just wanted to explain quickly how I’m automating the retrieval of sales ranks. I use the Amazon-ECS-PHP-Library in conjunction with Cron to execute a script every six hours (12am, 6am, 12pm, 6pm). The line in my crontab file looks like this:

 0 0,6,12,18 * * * wget http://127.0.0.1/developer/2011/amazonpas/update.php -O /dev/null

The sales ranks are saved to a simple MySQL table containing each book’s ASIN, sales rank, and a timestamp.

Introducing Grafico

Chart Amazon Sales Ranks with the Grafico Library

As you can see in Figure 2, Grafico will, by default, associate each chart value with its array index offset. Presuming the chart values were inserted into the array in accordance with the day in which they were recorded, this default feature conveniently serves nicely as a chronological representation of recorded sales ranks. But there is much more we can do to improve this simple graph, starting with adding labels and adjusting the colors.

Improving the Presentation

Options are available for tweaking the chart line’s stroke width and color, background color and more. You can also enable a hover feature which identifies the exact value associated with a point, useful since it’s otherwise only possible to approximate a point’s value. For example, check out the revised graph presented in Figure 3.

Chart Amazon Sales Ranks with the Grafico Library

This graph overrides four default options, including stroke_width (the line’s width), colors (the line’s color), grid_color (the grid’s color), and markers (whether the value should appear when the user mouses over a point).

<script type="text/javascript" charset="utf-8">
  window.onload = function () {
    var linegraph = new Grafico.LineGraph($('chart'), 
      { ranks: [32136, 24865, 19498, 42597, 16233, 26997, 30535, 18287] },
      { 
        stroke_width : 3,
        colors : {ranks: '#383638'},
        grid_color: '#abc52f',
        markers : "value"
      }
    );
  };
</script>

Charting Multiple Books

Of course, it would be interesting to simultaneously view the historical rankings of multiple books (see Figure 4). This too is easily accomplished using Grafico; just pass in multiple data sets!

Chart Amazon Sales Ranks with the Grafico Library

In addition to charting multiple data sets, I’ve added labels which appear when the user hovers over a chart line, and I’ve also spruced up the data points by adding small circle icons. The code used to create this graph is presented next:

<script type="text/javascript" charset="utf-8">
  window.onload = function () {
    var linegraph = new Grafico.LineGraph($('chart'), 
      { 
        epwzf: [32136, 24865, 19498, 42597, 16233, 26997, 30535, 18287],
        bpm4e: [63256, 54925, 45980, 33762, 45901, 52091, 40013, 33129],
      },
      { 
        stroke_width : 3,
        colors : {epwzf: '#383638', bpm4e: '#F68B1F'},
        grid_color: '#abc52f',
        markers : "circle",
        draw_hovers: true,
        datalabels: {
          epwzf: "Easy PHP Websites with the Zend Framework",
          bpm4e: "Beginning PHP and MySQL, 4E"
        }
      }
    );
  };
</script>

Conclusion

I’ve tried many graphing solutions over the past several years, and Grafico has easily proved to be the most user-friendly. It natively supports the ability to perform all of the most commonplace tweaks, and setup is incredibly simple. Are you using a different solution? Tell us about it in the comments!

About the Author

Jason Gilmore — Contributing Editor, PHP — is the founder of EasyPHPWebsites.com, and author of the popular book, “Easy PHP Websites with the Zend Framework”. Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer’s conference, and was a member of the 2008 MySQL Conference speaker selection board.

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories