官术网_书友最值得收藏!

Building bars of data

While at lunch, Calvin took several phone calls. One near the end of lunch was from Sara, VP of Inventory. Her department is facing issues with higher than normal returns. She has a team investigating the problem, but she wants help. She wants a chart showing how much money is lost to returns and the reasons for the returns. She'd like the charts by the end of the meeting on Friday so that she can present the problem to the whole team. She needs their buy-in and help to work toward a solution.

Calvin relays this information to us as we walk back to the office. "She said she was going to e-mail a spreadsheet with the past six months' data in it. Can you do your thing and make Sara look like a rockstar? Her department is the most ignored, but it's the backbone of the company." We tell him we'll do our best.

By the time we get back to our desks, we have an e-mail waiting for us. We open the spreadsheet and see the following data:

After looking over the data, we decide that our best choice is a bar chart. Bar charts are best used when we have categorical data to compare. We can compare various data points in one series or multiple data series. With this chart, we will be able to compare the dollar amounts in each category listed previously, so we will have just one data series. It is also possible to have multiple data series in each category, such as a different bar for each product category, and these data series grouped by regions.

  1. We start by including the categoryAxisRenderer and barRenderer plugins. These extend jqPlot to allow us to group our x axis by categories and create a bar chart:
    <script src="../js/jqplot.categoryAxisRenderer.min.js"></script>
    <script src="../js/jqplot.barRenderer.min.js"></script>
    <script>
    
    $(document).ready(function(){
  2. We include our array containing the data points. Since we are using categoryAxisRenderer, we pass a string as the x value instead of a number or date:
      var returns = [['Damaged Item', 15876.98], ['Defective Item', 26078.41], ['Gift', 6397.06], ['Not Correct Item', 12876.60]];
    
      var product_returns = $.jqplot ('product_returns', [returns],
      {
        title:'Total Cost of Product Returns over 6 months',
  3. We assign BarRenderer to the renderer option for our one data series. If we were to include multiple data series, we could include this in seriesDefaults so we'd only have to include it once:
    series: [ { renderer:$.jqplot.BarRenderer } ],
  4. Since we are using categories for the x axis, we need to enable CategoryAxisRenderer as the renderer for the x axis:
        axes:{
          xaxis:{ renderer: $.jqplot.CategoryAxisRenderer },
          yaxis: {
            label: 'Totals Dollars',
            tickOptions: { formatString: "$%'d" }
          }
        }
      });
    });
    </script>
    
    <div id="product_returns" style="width:600px;"></div>

    Tip

    We can mix and match data renderers. If we had multiple data series, we could render a bar chart for the first series and use a line chart for the second.

We load the page in our browser and see the following result:

主站蜘蛛池模板: 峨眉山市| 从江县| 吐鲁番市| 日土县| 鹤庆县| 弥渡县| 蒲城县| 淳安县| 博爱县| 长顺县| 鄂州市| 广宗县| 平武县| 乳山市| 屏东县| 沽源县| 宣恩县| 贵阳市| 习水县| 彭水| 隆化县| 乌兰察布市| 千阳县| 思茅市| 新兴县| 疏勒县| 清流县| 拉萨市| 兴安盟| 建瓯市| 滨海县| 泰和县| 迭部县| 温宿县| 敦煌市| 陇川县| 丰城市| 溧阳市| 子洲县| 永昌县| 临湘市|