Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.     if ($('#dynamic-chart')[0]) {
  3.         var data = [],
  4.             totalPoints = 300;
  5.  
  6.         function getRandomData() {
  7.             if (data.length > 0)
  8.                 data = data.slice(1);
  9.  
  10.             while (data.length < totalPoints) {
  11.                 var prev = data.length > 0 ? data[data.length - 1] : 50,
  12.                     y = prev + Math.random() * 10 - 5;
  13.                 if (y < 0) {
  14.                     y = 0;
  15.                 } else if (y > 90) {
  16.                     y = 90;
  17.                 }
  18.  
  19.                 data.push(y);
  20.             }
  21.  
  22.             var res = [];
  23.             for (var i = 0; i < data.length; ++i) {
  24.                 res.push([i, data[i]])
  25.             }
  26.  
  27.             return res;
  28.         }
  29.  
  30.  
  31.         var updateInterval = 30;
  32.         var plot = $.plot("#dynamic-chart", [ getRandomData() ], {
  33.             series: {
  34.                 label: "Data",
  35.                 lines: {
  36.                     show: true,
  37.                     lineWidth: 1,
  38.                     fill: 0.25,
  39.                 },
  40.  
  41.                 color: 'rgba(255,255,255,0.2)',
  42.                 shadowSize: 0,
  43.             },
  44.             yaxis: {
  45.                 min: 0,
  46.                 max: 100,
  47.                 tickColor: 'rgba(255,255,255,0.15)',
  48.                 font :{
  49.                     lineHeight: 13,
  50.                     style: "normal",
  51.                     color: "rgba(255,255,255,0.8)",
  52.                 },
  53.                 shadowSize: 0,
  54.  
  55.             },
  56.             xaxis: {
  57.                 tickColor: 'rgba(255,255,255,0.15)',
  58.                 show: true,
  59.                 font :{
  60.                     lineHeight: 13,
  61.                     style: "normal",
  62.                     color: "rgba(255,255,255,0.8)",
  63.                 },
  64.                 shadowSize: 0,
  65.                 min: 0,
  66.                 max: 250
  67.             },
  68.             grid: {
  69.                 borderWidth: 1,
  70.                 borderColor: 'rgba(255,255,255,0.25)',
  71.                 labelMargin:10,
  72.                 hoverable: true,
  73.                 clickable: true,
  74.                 mouseActiveRadius:6,
  75.             },
  76.             legend: {
  77.                 show: false
  78.             }
  79.         });
  80.  
  81.         function update() {
  82.             plot.setData([getRandomData()]);
  83.             // Since the axes don't change, we don't need to call plot.setupGrid()
  84.  
  85.             plot.draw();
  86.             setTimeout(update, updateInterval);
  87.         }
  88.  
  89.         update();
  90.  
  91.         $("#dynamic-chart").bind("plothover", function (event, pos, item) {
  92.             if (item) {
  93.                 var x = item.datapoint[0].toFixed(2),
  94.                     y = item.datapoint[1].toFixed(2);
  95.                 $("#dynamic-chart-tooltip").html(item.series.label + " of " + x + " = " + y).css({top: item.pageY+5, left: item.pageX+5}).fadeIn(200);
  96.             }
  97.             else {
  98.                 $("#dynamic-chart-tooltip").hide();
  99.             }
  100.         });
  101.  
  102.         $("<div id='dynamic-chart-tooltip' class='chart-tooltip'></div>").appendTo("body");
  103.     }
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement