When we reload the page again now and hover over one of the points we’ll see our nicely-formatted tooltip.
Now that we have a graph of our orders it’s easy to see trends in the data.
We’ll show you the inefficient way to do this first because it’s easier to write the code that way, then show you how to optimize it at the end of the episode.We’ll need the sum of the total for each order for each day and so we’ll write a class method in our on the result to convert it into something that Java Script can use.The system contains hundreds of orders across dozens of pages so to calculate any kind of statistical data about the orders manually would be rather tedious to say the least.We could see trends in the sales data far more easily if we had a chart at the top of the page that showed a summary of the orders over time and we’re going to do just that in this episode.With the files in place we’ll add the following line to the file.
Now that we have Highcharts set up we can begin to add a chart to our orders page.
As we’ll see Highcharts can generate beautiful looking graphs with only a few lines of Java Script.
Before considering Highcharts for any projects it’s worth bearing in mind that it is only free for non-commercial products.
If we reload the page again now we’ll see that the test data has been replaced with the actual data from the last three weeks, with a point for each day and the total revenue in dollars of that day’s orders. This function returns a string that will appear as the tooltip’s text.
The data values for the point can be formatted using formatters that Highcharts provides and we use two of them in the code above to format the date value for the x-axis and the numeric value for the y-axis.
In the code below we’ve created one data series with five values to test that our graph works before we add the real data.