经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
使用laravel和ECharts实现折线图效果的例子
来源:jb51  时间:2019/10/10 8:35:57  对本文有异议

1、首先引入echart.js

  1. <script type="text/javascript" src="{{ asset('/public/js/echarts.js') }}"></script>

2、html页面,要有一个布局容器,用来显示图像,一定要设置宽和高

  1. <div class="contain" style="width: 84%;" id="contain"></div>

3、echarts折线图的使用

  1. var myChart = echarts.init(document.getElementById("contain"));
  2. option = {
  3. title : {
  4. text: '时间变化图' // 标题
  5. },
  6. tooltip : {
  7. trigger: 'axis' // 折线图
  8. },
  9. legend: {
  10. data:['时间'] // 图例,就是折线图上方的符号
  11. },
  12. toolbox: { // 工具箱,在折线图右上方的工具条,可以变成别的图像
  13. show : true,
  14. feature : {
  15. mark : {show: true},
  16. dataView : {show: true, readOnly: false},
  17. magicType : {show: true, type: ['line', 'bar']},
  18. restore : {show: true},
  19. saveAsImage : {show: true}
  20. }
  21. },
  22. calculable : true, // 是否启动拖拽重计算属性,默认false
  23. xAxis : [ // x坐标轴
  24. {
  25. axisLine: { // x坐标轴颜色
  26. lineStyle: { color: '#333' }
  27. },
  28. axisLabel: { // x轴的数据会旋转30度
  29. rotate: 30,
  30. interval: 0
  31. },
  32. type : 'category',
  33. boundaryGap : false, // x轴从0开始
  34. data : [] // x轴数据
  35. }
  36. ],
  37. yAxis : [ // y轴
  38. {
  39. type : 'value',
  40. axisLabel : {
  41. formatter: '{value} 秒' // y轴的值都加上秒的单位
  42. },
  43. axisLine: {
  44. lineStyle: { color: '#333' }
  45. }
  46. }
  47. ],
  48. series : [ // 设置图标数据用
  49. {
  50. name:'时间',
  51. type:'line',
  52. smooth: 0.3, // 线有弧度
  53. data: [] // y轴数据
  54. }
  55. ]
  56. };
  57. // 使用刚指定的配置项和数据显示图表。
  58. myChart.setOption(option);

4、实现功能

(1)路由

  1. Route::get('/', 'UserController@index');
  2. Route::post('/axis', 'UserController@axis');

(2)方法

  1. public function index()
  2. {
  3. return view('user.index');
  4. }
  5. // 是ajax所用的的方法,得到要显示的数据,返回数组
  6. public function axis()
  7. {
  8. $key = Key::all('name', 'ttl', 'created_time');
  9. return $key;
  10. }

(3)当访问/首页时,到index.blade.php

(4)index.blade.php的内容

  1. <div class="contain" style="width: 84%;" id="contain"></div>
  2. <script type="text/javascript">
  3. var names = []; // 设置两个变量用来存变量
  4. var ttls = [];
  5. var time = Date.parse(new Date()).toString().substr(0, 10); // 获取当前时间,精确到秒,但因为是毫秒级的,会多3个0,变成字符串后去掉
  6. time = parseInt(time);
  7. function getData()
  8. {
  9. $.post("{{ url('/axis') }}", {
  10. "_token": "{{ csrf_token() }}"
  11. }, function(data) {
  12. $.each(data, function(i, item) {
  13. names.push(item.name);
  14. if((ttl = (parseInt(item.ttl) + parseInt(item.created_time) - time)) > 0) { // 小于0就==0,
  15. ttls.push(ttl);
  16. } else {
  17. ttls.push(0);
  18. }
  19. });
  20. });
  21. }
  22. getData(); // 一定不能忘了,调用
  23. // 实现画图的功能
  24. function chart() {
  25. var myChart = echarts.init(document.getElementById("contain"));
  26. option = {
  27. title : {
  28. text: '键名过期时间变化图'
  29. },
  30. tooltip : {
  31. trigger: 'axis'
  32. },
  33. legend: {
  34. data:['过期剩余时间']
  35. },
  36. toolbox: {
  37. show : true,
  38. feature : {
  39. mark : {show: true},
  40. dataView : {show: true, readOnly: false},
  41. magicType : {show: true, type: ['line', 'bar']},
  42. restore : {show: true},
  43. saveAsImage : {show: true}
  44. }
  45. },
  46. calculable : true,
  47. xAxis : [
  48. {
  49. axisLine: {
  50. lineStyle: { color: '#333' }
  51. },
  52. axisLabel: {
  53. rotate: 30,
  54. interval: 0
  55. },
  56. type : 'category',
  57. boundaryGap : false,
  58. data : names // x的数据,为上个方法中得到的names
  59. }
  60. ],
  61. yAxis : [
  62. {
  63. type : 'value',
  64. axisLabel : {
  65. formatter: '{value} 秒'
  66. },
  67. axisLine: {
  68. lineStyle: { color: '#333' }
  69. }
  70. }
  71. ],
  72. series : [
  73. {
  74. name:'过期剩余时间',
  75. type:'line',
  76. smooth: 0.3,
  77. data: ttls // y轴的数据,由上个方法中得到的ttls
  78. }
  79. ]
  80. };
  81. // 使用刚指定的配置项和数据显示图表。
  82. myChart.setOption(option);
  83. }
  84. setTimeout('chart()', 1000); // 为什么加定时器?因为上面是一起执行的,可能还未取得数据,便已经将图画好了,图上就没有数据,所以这里我延迟了1s,
  85. </script>

(5)大功告成!!

以上这篇使用laravel和ECharts实现折线图效果的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号