demo.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script src="code/highcharts.js"></script>
  8. <script src="code/modules/exporting.js"></script>
  9. <script src="code/plugins/highcharts-zh_CN.js"></script>
  10. <style>
  11. #content div {
  12. float: left;
  13. }
  14. </style>
  15. <script>
  16. var time = new Date();
  17. window.onload = function () {
  18. getData();
  19. loopGetData(1000);
  20. }
  21. function getData() {
  22. var x = time.getTime(), y = Math.random() * 100; // x 时间 y 随机数据
  23. cpuChart.series[0].addPoint([x, y], true, true, true);
  24. y = Math.random() * 100; // 随机数据
  25. memChart.series[0].addPoint([x, y], true, true, true);
  26. }
  27. function loopGetData(loopTime) {
  28. setInterval(function () {
  29. time = new Date(time.getTime() + 1000);
  30. getData();
  31. }, loopTime);
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="content">
  37. <div id="container" style="width: 400px; height:200px;"></div>
  38. <div id="container_mem" style="width: 400px; height:200px;"></div>
  39. </div>
  40. <script>
  41. function getConfig(name, count) {
  42. return {
  43. chart: {
  44. // type: 'spline',
  45. marginRight: 10
  46. },
  47. exporting: {
  48. buttons: {
  49. contextButton: {
  50. menuItems: null,
  51. onclick: function () {
  52. this.fullscreen.toggle();
  53. }
  54. }
  55. }
  56. },
  57. title: {
  58. text: name
  59. },
  60. xAxis: {
  61. type: 'datetime',
  62. tickPixelInterval: 150
  63. },
  64. credits: {
  65. enabled: false
  66. },
  67. yAxis: {
  68. title: {
  69. text: null
  70. }
  71. },
  72. tooltip: {
  73. formatter: function () {
  74. return '<b>' + this.series.name + '</b><br/>' +
  75. Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
  76. Highcharts.numberFormat(this.y, 2);
  77. }
  78. },
  79. legend: {
  80. enabled: false
  81. },
  82. plotOptions: {
  83. series: {
  84. marker: {
  85. enabled: false
  86. }
  87. }
  88. },
  89. series: [{
  90. name: name,
  91. data: (function () {
  92. var data = [], i;
  93. for (i = count * -1; i <= 0; i += 1) {
  94. data.push({
  95. x: null,
  96. y: null
  97. });
  98. }
  99. return data;
  100. }())
  101. }]
  102. };
  103. }
  104. Highcharts.setOptions({
  105. global: {
  106. useUTC: false
  107. }
  108. });
  109. function activeLastPointToolip(chart) {
  110. var points = chart.series[0].points;
  111. chart.tooltip.refresh(points[points.length - 1]);
  112. }
  113. // var time = (new Date()).getTime();
  114. var cpuChart = Highcharts.chart('container', getConfig('CPU', 20));
  115. var memChart = Highcharts.chart('container_mem', getConfig('Memory', 20));
  116. </script>
  117. </body>
  118. </html>