Advertisement
MrBB

Highcharts.js

Oct 20th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.19 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.         <title>My first column chart by Highcharts</title>
  6.         <!-- 1. Add JQuery and Highcharts in the head of your page -->
  7.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  8.         <script src="http://code.highcharts.com/highcharts.js"></script>
  9.          
  10.         <!-- 2. You can add print and export feature by adding this line -->
  11.         <script src="http://code.highcharts.com/modules/exporting.js"></script>
  12.          
  13.          
  14.         <!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
  15.         <script type="text/javascript">
  16.         jQuery(document).ready(function() {
  17.  
  18.             var options = {
  19.                 chart: {
  20.                     renderTo: 'container',
  21.                     type: 'column'
  22.                 },
  23.                 title: {
  24.                     text: 'Tiny Tool Monthly Sales'                
  25.                 },
  26.                 subtitle: {
  27.                     text: '2014 Q1-Q2'
  28.                 },
  29.                 xAxis: {
  30.                     categories: []
  31.                 },
  32.                 yAxis: {
  33.                     title: {
  34.                         text: 'Sales'
  35.                     }
  36.                 },
  37.                 series: []
  38.             };
  39.             // JQuery function to process the csv data
  40.             $.get('column-data.csv', function(data) {
  41.                 // Split the lines
  42.                 var lines = data.split('\n');
  43.                 $.each(lines, function(lineNo, line) {
  44.                     var items = line.split(',');
  45.                      
  46.                     // header line contains names of categories
  47.                     if (lineNo == 0) {
  48.                         $.each(items, function(itemNo, item) {
  49.                             //skip first item of first line
  50.                             if (itemNo > 0) options.xAxis.categories.push(item);
  51.                         });
  52.                     }
  53.                      
  54.                     // the rest of the lines contain data with their name in the first position
  55.                     else {
  56.                         var series = {
  57.                             data: []
  58.                         };
  59.                         $.each(items, function(itemNo, item) {
  60.                             if (itemNo == 0) {
  61.                                 series.name = item;
  62.                             } else {
  63.                                 series.data.push(parseFloat(item));
  64.                             }
  65.                         });
  66.                          
  67.                         options.series.push(series);
  68.  
  69.                     }
  70.                      
  71.                 });
  72.                 //putting all together and create the chart
  73.                 var chart = new Highcharts.Chart(options);
  74.             });        
  75.              
  76.         });
  77.         </script>
  78.          
  79.     </head>
  80.     <body>
  81.          
  82.         <!-- 3. Add the container -->
  83.         <div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>    
  84.                  
  85.     </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement