histogram_custom.html 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <script src="../lib/esl.js"></script>
  7. <script src="../lib/config.js"></script>
  8. <script src="../lib/jquery.min.js"></script>
  9. </head>
  10. <body>
  11. <style>
  12. html, body, #main {
  13. width: 100%;
  14. height: 100%;
  15. margin: 0;
  16. padding: 0;
  17. }
  18. </style>
  19. <div id="main"></div>
  20. <script>
  21. require([
  22. 'echarts',
  23. 'ecStat'
  24. ], function (echarts, ecStat) {
  25. var chart = echarts.init(document.getElementById('main'));
  26. window.onresize = function () {
  27. chart.resize();
  28. };
  29. var height = [70, 65, 63, 72, 81, 83, 66, 75, 80, 75, 79, 76, 76, 69, 75, 74, 85, 86, 71, 64, 78, 80, 74, 72, 77, 81, 82, 80, 80, 80, 87];
  30. var girth = [8.3,8.6, 8.8, 10.5, 10.7, 10.8, 11.0, 11.0, 11.1, 11.2, 11.3, 11.4, 11.4, 11.7, 12.0, 12.9, 12.9, 13.3, 13.7, 13.8, 14.0, 14.2, 14.5, 16.0, 16.3, 17.3, 17.5, 17.9, 18.0, 18.0, 20.6];
  31. var testdata = [0.87659, 0.68975, 0.78613];
  32. var bins = ecStat.histogram(girth);
  33. var option = {
  34. title: {
  35. text: 'Girths of Black Cherry Trees',
  36. left: 'center',
  37. top: 20
  38. },
  39. tooltip: {},
  40. xAxis: [{
  41. scale: true, //这个一定要设,不然barWidth和bins对应不上
  42. }],
  43. yAxis: {
  44. },
  45. series: [{
  46. type: 'custom',
  47. renderItem: function (params, api) {
  48. var yValue = api.value(2);
  49. var start = api.coord([api.value(0), yValue]);
  50. var size = api.size([api.value(1) - api.value(0), yValue]);
  51. return {
  52. type: 'rect',
  53. shape: {
  54. x: start[0],
  55. y: start[1],
  56. width: size[0] * 0.99,
  57. height: size[1]
  58. },
  59. style: api.style()
  60. };
  61. },
  62. itemStyle: {
  63. color: 'rgb(25, 183, 207)'
  64. },
  65. label: {
  66. normal: {
  67. show: true,
  68. position: 'insideTop'
  69. }
  70. },
  71. dimensions: ['x0', 'x1', 'sampleNum'],
  72. encode: {
  73. x: [0, 1],
  74. y: 2,
  75. tooltip: [0, 1, 2]
  76. },
  77. data: bins.customData
  78. }]
  79. };
  80. chart.setOption(option);
  81. });
  82. </script>
  83. </body>
  84. </html>