• JQuery+webpack+echarts构建可视化开发环境


      数据可视化就越来越受重视,而一般的可视化的需求可能仅仅只是一个大屏展示,这就意味着我们没必要为一个大屏展示的项目引入一整套前端框架,而是使用jq就够了,因为成本最低,开发上手最快,接下来就分享下最近研究的jq+webpack+echarts构建的可视化开发环境
      首先是我的项目结构
    转存失败重新上传取消
      语法使用es6编写,关于es6的配置就不多说了
      可视化用echarts还是比较方便,在这之前也看了一点d3.js,相比较而言,echarts还是简单很多,首先安装echarts

    npm install echarts --save

      这是入口文件app.js(非常简单的入口文件,把可视化展示的逻辑放在echarts.js里去写,这里的app.js只是给webpack提供一个依赖图创建的入口)

    1. import './scripts/echarts.js'
    2. import './style/main.less'

      接下来是webpack的配置文件

    1. const webpack = require('webpack')
    2. const echarts = require('echarts')
    3. const HtmlWebpackPlugin = require('html-webpack-plugin')
    4. const path = require('path')
    5. module.exports = {
    6. context: path.resolve(__dirname, './'),
    7. entry: {
    8. app: './src/app.js'
    9. },
    10. output: {
    11. path: path.resolve(__dirname, 'public'),
    12. filename: '[name].js'
    13. },
    14. devServer: {
    15. contentBase: "./public",
    16. historyApiFallback: true,
    17. inline: true,
    18. hot: true,
    19. },
    20. module: {
    21. loaders: [
    22. {
    23. test: /\.js$/,
    24. loader: 'babel-loader',
    25. exclude: /^node_modules$/,
    26. },
    27. {
    28. test: /\.less$/,
    29. loader: 'style-loader!css-loader!less-loader',
    30. exclude: /^node_modules$/,
    31. },
    32. {
    33. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    34. loader: 'url-loader',
    35. options: {
    36. limit: 10000
    37. }
    38. }
    39. ]
    40. },
    41. plugins: [
    42. new webpack.ProvidePlugin({
    43. $: 'jquery',
    44. _: 'lodash'
    45. }),
    46. new webpack.HotModuleReplacementPlugin(),
    47. new HtmlWebpackPlugin({
    48. template: "./src/index.html",
    49. inject: true,
    50. favicon: path.resolve(__dirname, './favicon.ico')
    51. })
    52. ]
    53. }

      项目使用的是jq,webpack.ProvidePlugin可直接引入依赖,但是记得一定要先安装jq(lodash也是)

    npm install jquery --save -dev

      模板文件就是index.html

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <link rel="shortcut icon" type="image/x-icon" href="./favicon.ico">
    8. <title>Echarts</title>
    9. </head>
    10. <body>
    11. <div id="echartsDom"></div>
    12. </body>
    13. </html>

      可以看到我们为echarts准备的id为echartsDom的DOM节点,webpack里面HtmlWebpackPlugin也有相应的配置,为了美观,浏览器title的图标也从阿里矢量图标库找了一个图片,等项目跑起来的时候可以看到,这些都是在HtmlWebpackPlugin里面去配置。接下来就是我们的echarts.js了

    1. const echarts = require('echarts')
    2. const echartsDom = $('#echartsDom')[0]
    3. const resizeWorldMapContainer = () => {
    4. echartsDom.style.width = window.innerWidth + 'px';
    5. echartsDom.style.height = window.innerHeight + 'px';
    6. };
    7. resizeWorldMapContainer();
    8. const myEcharts = echarts.init(echartsDom)
    9. myEcharts.setOption({
    10. title: {
    11. text: '监控大屏',
    12. textStyle: {
    13. color: 'rgba(255, 255, 255, 0.7)'
    14. },
    15. left: 'center',
    16. top: '10px'
    17. },
    18. tooltip: {
    19. },
    20. legend: {
    21. x: '25%',
    22. bottom: '2%',
    23. textStyle: {
    24. color: '#ffd285',
    25. },
    26. data: [
    27. {
    28. name: '合肥',
    29. textStyle: {
    30. color: 'red'
    31. }
    32. },
    33. {
    34. name: '芜湖',
    35. icon: 'circle',
    36. textStyle: {
    37. color: 'red'
    38. }
    39. },
    40. {
    41. name: '马鞍山',
    42. icon: 'circle',
    43. textStyle: {
    44. color: 'red'
    45. }
    46. }
    47. ]
    48. },
    49. grid: {
    50. left: '10%',
    51. right: '45%',
    52. bottom: '15%',
    53. containLabel: false
    54. },
    55. xAxis: {
    56. type: 'category',
    57. axisLine: {
    58. onZero: false,
    59. lineStyle: {
    60. color: '#fff'
    61. }
    62. },
    63. axisTick: {
    64. "show": true,
    65. },
    66. axisLabel: {
    67. textStyle: {
    68. color: '#ffd285'
    69. }
    70. },
    71. splitArea: {
    72. show: true
    73. },
    74. boundaryGap: true,
    75. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    76. },
    77. yAxis: {
    78. "axisLine": {
    79. lineStyle: {
    80. color: '#c0576d'
    81. }
    82. },
    83. splitLine: {
    84. show: true,
    85. lineStyle: {
    86. color: '#c0576d'
    87. }
    88. },
    89. "axisTick": {
    90. show: true
    91. },
    92. axisLabel: {
    93. textStyle: {
    94. color: '#ffd285'
    95. }
    96. },
    97. type: 'value'
    98. },
    99. series: [
    100. {
    101. name: '合肥',
    102. smooth: true,
    103. type: 'line',
    104. lineStyle: {
    105. normal: {
    106. color: '#ffff00',
    107. opacity: 10
    108. }
    109. },
    110. itemStyle: {
    111. normal: {
    112. color: '#ff6600'
    113. },
    114. emphasis: {
    115. color: '#ff6600'
    116. }
    117. },
    118. areaStyle: {
    119. normal: {
    120. color: '#ff6600'
    121. },
    122. },
    123. symbolSize: 8,
    124. data: [10, 50, 39, 50, 120, 82, 80],
    125. markPoint: {
    126. label: {
    127. normal: {
    128. show: true,
    129. color: '#ff0066'
    130. },
    131. emphasis: {
    132. show: true,
    133. color: '#ff9900'
    134. }
    135. },
    136. data: [{
    137. name: '最大值',
    138. type: 'min'
    139. }]
    140. }
    141. },
    142. {
    143. name: '芜湖',
    144. smooth: true,
    145. type: 'bar',
    146. symbolSize: 8,
    147. data: [70, 50, 50, 87, 90, 80, 70]
    148. },
    149. {
    150. name: '马鞍山',
    151. smooth: true,
    152. type: 'bar',
    153. symbolSize: 8,
    154. data: [290, 200, 20, 132, 15, 200, 90]
    155. },
    156. {
    157. type: 'pie',
    158. name: '饼图',
    159. center: ['83%', '20%'],
    160. radius: ['15%', '20%'],
    161. tooltip: {
    162. trigger: 'item',
    163. formatter: "{a}
      {b} : {c} ({d}%)"
    164. },
    165. label: {
    166. normal: {
    167. position: 'center'
    168. },
    169. emphasis: {
    170. show: true,
    171. color: '#ff6600',
    172. }
    173. },
    174. data: [
    175. {
    176. value: 335,
    177. name: '污染来源分析',
    178. itemStyle: {
    179. normal: {
    180. color: '#ffd285'
    181. }
    182. },
    183. tooltip: {
    184. trigger: 'item',
    185. formatter: "{a}
      {b} : {c} ({d}%)"
    186. },
    187. label: {
    188. normal: {
    189. formatter: '{d} %',
    190. textStyle: {
    191. color: '#ffd285',
    192. fontSize: 20
    193. }
    194. }
    195. }
    196. },
    197. {
    198. value: 180,
    199. name: '占位',
    200. tooltip: {
    201. show: false
    202. },
    203. itemStyle: {
    204. normal: {
    205. color: '#404A59'
    206. }
    207. },
    208. label: {
    209. normal: {
    210. textStyle: {
    211. color: '#ffd285',
    212. },
    213. formatter: '\n汽车尾气'
    214. }
    215. }
    216. }
    217. ]
    218. },
    219. {
    220. type: 'pie',
    221. center: ['83%', '50%'],
    222. radius: ['15%', '20%'],
    223. name: '饼图',
    224. tooltip: {
    225. trigger: 'item',
    226. formatter: "{a}
      {b} : {c} ({d}%)"
    227. },
    228. data: [
    229. {
    230. value: 335,
    231. name: '直接访问',
    232. itemStyle: {
    233. normal: {
    234. color: '#FF3300'
    235. }
    236. }
    237. },
    238. {
    239. value: 310,
    240. name: '邮件营销',
    241. itemStyle: {
    242. normal: {
    243. color: '#FFFF00'
    244. }
    245. }
    246. },
    247. {
    248. value: 234,
    249. name: '联盟广告',
    250. itemStyle: {
    251. normal: {
    252. color: '#00FF00'
    253. }
    254. }
    255. },
    256. {
    257. value: 135,
    258. name: '视频广告',
    259. itemStyle: {
    260. normal: {
    261. color: '#1E90FF'
    262. }
    263. }
    264. },
    265. {
    266. value: 1548,
    267. name: '搜索引擎',
    268. itemStyle: {
    269. normal: {
    270. color: '#ADFF2F'
    271. }
    272. }
    273. }
    274. ]
    275. }
    276. ]
    277. })
    278. window.onresize = () => {
    279. resizeWorldMapContainer();
    280. myEcharts.resize();
    281. };

      可以看到这个文件是非常长的,因为我把一个大屏里面所有图形的option都放在这里面去设置,这样其实是不便于调试的,比较好的方案还是在布局的时候根据可视化的设计图,将不同的图,比如折线图,饼图甚至是地图的分别分成不同的模块,不同的模块对应不同的dom节点,然后在index.html的文件里去排版好这些dom节点的布局,使得每一个图形都只有一个单独的js文件,方便调试,看起来也十分清爽,因为时间有限,我就只放在了这一个文件里,dom节点也只创建了一个,有兴趣的同学可以自行尝试做一下
      这是package.json

    1. {
    2. "name": "my_echarts",
    3. "version": "1.0.0",
    4. "description": "",
    5. "main": "app.js",
    6. "scripts": { "dev": "rm -rf ./public && webpack-dev-server --inline --progress --config webpack.config.js", "build": "webpack" },
    7. "author": "",
    8. "license": "ISC",
    9. "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-env": "^1.6.1", "babel-preset-es2015": "^6.24.1", "babel-preset-stage-0": "^6.24.1", "css-loader": "^0.28.7", "html-webpack-plugin": "^2.30.1", "jquery": "^3.2.1", "less": "^2.7.3", "less-loader": "^4.0.5", "raw-loader": "^0.5.1", "style-loader": "^0.19.1", "webpack": "^3.10.0", "webpack-dev-server": "^2.9.7" },
    10. "dependencies": { "echarts": "^3.8.5" } }

      启动项目npm run dev

  • 相关阅读:
    『力扣刷题本』:移除链表元素
    成电860考研专业课考前划重点-学长课程音频转文字-用科大讯飞花钱买的-三万五千字
    pat 1145
    【选型】JAVA生成PPT及选型
    【浅学Java】排序大全
    CTF简介
    MFC中字符串string类型和CString类型互转方法
    智谱API调用
    入职字节外包一个月,我离职了
    UVA-548 树 题解答案代码 算法竞赛入门经典第二版
  • 原文地址:https://blog.csdn.net/m0_72431373/article/details/127772140