dynamic-loaded-chart.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. <title>Dynamic Loaded Column Chart</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. body {
  11. background: #fff;
  12. }
  13. #wrap {
  14. margin: 45px auto;
  15. max-width: 800px;
  16. position: relative;
  17. }
  18. .chart-box {
  19. padding-left: 0;
  20. }
  21. #chart-year,
  22. #chart-quarter {
  23. width: 96%;
  24. max-width: 48%;
  25. box-shadow: none;
  26. padding-left: 0;
  27. padding-top: 20px;
  28. background: #fff;
  29. border: 1px solid #ddd;
  30. }
  31. #chart-year {
  32. float: left;
  33. position: relative;
  34. transition: 1s ease transform;
  35. z-index: 3;
  36. }
  37. #chart-year.chart-quarter-activated {
  38. transform: translateX(0);
  39. transition: 1s ease transform;
  40. }
  41. #chart-quarter {
  42. float: left;
  43. position: relative;
  44. z-index: -2;
  45. transition: 1s ease transform;
  46. }
  47. #chart-quarter.active {
  48. transition: 1.1s ease-in-out transform;
  49. transform: translateX(0);
  50. z-index: 1;
  51. }
  52. @media screen and (min-width: 480px) {
  53. #chart-year {
  54. transform: translateX(50%);
  55. }
  56. #chart-quarter {
  57. transform: translateX(-50%);
  58. }
  59. }
  60. select#model {
  61. display: none;
  62. position: absolute;
  63. top: -40px;
  64. left: 0;
  65. z-index: 2;
  66. cursor: pointer;
  67. transform: scale(0.8);
  68. }
  69. </style>
  70. <script>
  71. window.Promise ||
  72. document.write(
  73. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  74. )
  75. window.Promise ||
  76. document.write(
  77. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  78. )
  79. window.Promise ||
  80. document.write(
  81. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  82. )
  83. </script>
  84. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  85. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  86. <script src="https://cdn.jsdelivr.net/npm/vue-apexcharts"></script>
  87. <script>
  88. Apex = {
  89. chart: {
  90. toolbar: {
  91. show: false
  92. }
  93. },
  94. tooltip: {
  95. shared: false
  96. },
  97. legend: {
  98. show: false
  99. }
  100. }
  101. var colors = ['#008FFB', '#00E396', '#FEB019', '#FF4560', '#775DD0', '#00D9E9', '#FF66C3'];
  102. /**
  103. * Randomize array element order in-place.
  104. * Using Durstenfeld shuffle algorithm.
  105. */
  106. function shuffleArray(array) {
  107. for (var i = array.length - 1; i > 0; i--) {
  108. var j = Math.floor(Math.random() * (i + 1));
  109. var temp = array[i];
  110. array[i] = array[j];
  111. array[j] = temp;
  112. }
  113. return array;
  114. }
  115. var arrayData = [{
  116. y: 400,
  117. quarters: [{
  118. x: 'Q1',
  119. y: 120
  120. }, {
  121. x: 'Q2',
  122. y: 90
  123. }, {
  124. x: 'Q3',
  125. y: 100
  126. }, {
  127. x: 'Q4',
  128. y: 90
  129. }]
  130. }, {
  131. y: 430,
  132. quarters: [{
  133. x: 'Q1',
  134. y: 120
  135. }, {
  136. x: 'Q2',
  137. y: 110
  138. }, {
  139. x: 'Q3',
  140. y: 90
  141. }, {
  142. x: 'Q4',
  143. y: 110
  144. }]
  145. }, {
  146. y: 448,
  147. quarters: [{
  148. x: 'Q1',
  149. y: 70
  150. }, {
  151. x: 'Q2',
  152. y: 100
  153. }, {
  154. x: 'Q3',
  155. y: 140
  156. }, {
  157. x: 'Q4',
  158. y: 138
  159. }]
  160. }, {
  161. y: 470,
  162. quarters: [{
  163. x: 'Q1',
  164. y: 150
  165. }, {
  166. x: 'Q2',
  167. y: 60
  168. }, {
  169. x: 'Q3',
  170. y: 190
  171. }, {
  172. x: 'Q4',
  173. y: 70
  174. }]
  175. }, {
  176. y: 540,
  177. quarters: [{
  178. x: 'Q1',
  179. y: 120
  180. }, {
  181. x: 'Q2',
  182. y: 120
  183. }, {
  184. x: 'Q3',
  185. y: 130
  186. }, {
  187. x: 'Q4',
  188. y: 170
  189. }]
  190. }, {
  191. y: 580,
  192. quarters: [{
  193. x: 'Q1',
  194. y: 170
  195. }, {
  196. x: 'Q2',
  197. y: 130
  198. }, {
  199. x: 'Q3',
  200. y: 120
  201. }, {
  202. x: 'Q4',
  203. y: 160
  204. }]
  205. }];
  206. function makeData() {
  207. var dataSet = shuffleArray(arrayData)
  208. var dataYearSeries = [{
  209. x: "2011",
  210. y: dataSet[0].y,
  211. color: colors[0],
  212. quarters: dataSet[0].quarters
  213. }, {
  214. x: "2012",
  215. y: dataSet[1].y,
  216. color: colors[1],
  217. quarters: dataSet[1].quarters
  218. }, {
  219. x: "2013",
  220. y: dataSet[2].y,
  221. color: colors[2],
  222. quarters: dataSet[2].quarters
  223. }, {
  224. x: "2014",
  225. y: dataSet[3].y,
  226. color: colors[3],
  227. quarters: dataSet[3].quarters
  228. }, {
  229. x: "2015",
  230. y: dataSet[4].y,
  231. color: colors[4],
  232. quarters: dataSet[4].quarters
  233. }, {
  234. x: "2016",
  235. y: dataSet[5].y,
  236. color: colors[5],
  237. quarters: dataSet[5].quarters
  238. }];
  239. return dataYearSeries
  240. }
  241. function updateQuarterChart(sourceChart, destChartIDToUpdate) {
  242. var series = [];
  243. var seriesIndex = 0;
  244. var colors = []
  245. if (sourceChart.w.globals.selectedDataPoints[0]) {
  246. var selectedPoints = sourceChart.w.globals.selectedDataPoints;
  247. for (var i = 0; i < selectedPoints[seriesIndex].length; i++) {
  248. var selectedIndex = selectedPoints[seriesIndex][i];
  249. var yearSeries = sourceChart.w.config.series[seriesIndex];
  250. series.push({
  251. name: yearSeries.data[selectedIndex].x,
  252. data: yearSeries.data[selectedIndex].quarters
  253. })
  254. colors.push(yearSeries.data[selectedIndex].color)
  255. }
  256. if (series.length === 0) series = [{
  257. data: []
  258. }]
  259. return ApexCharts.exec(destChartIDToUpdate, 'updateOptions', {
  260. series: series,
  261. colors: colors,
  262. fill: {
  263. colors: colors
  264. }
  265. })
  266. }
  267. }
  268. </script>
  269. </head>
  270. <body>
  271. <div id="app">
  272. <div id="wrap">
  273. <select id="model" class="flat-select"
  274. @change="changeData">
  275. <option value="iphone5">iPhone 5</option>
  276. <option value="iphone6">iPhone 6</option>
  277. <option value="iphone7">iPhone 7</option>
  278. </select>
  279. <div id="chart-year">
  280. <apexchart type="bar" height="400" ref="chart" :options="chartOptions" :series="series"></apexchart>
  281. </div>
  282. <div id="chart-quarter">
  283. <apexchart type="bar" height="400" ref="chartQuarter" :options="chartOptionsQuarter" :series="seriesQuarter"></apexchart>
  284. </div>
  285. </div>
  286. </div>
  287. <!-- Below element is just for displaying source code. it is not required. DO NOT USE -->
  288. <div id="html">
  289. &lt;div id=&quot;wrap&quot;&gt;
  290. &lt;select id=&quot;model&quot; class=&quot;flat-select&quot;
  291. @change=&quot;changeData&quot;&gt;
  292. &lt;option value=&quot;iphone5&quot;&gt;iPhone 5&lt;/option&gt;
  293. &lt;option value=&quot;iphone6&quot;&gt;iPhone 6&lt;/option&gt;
  294. &lt;option value=&quot;iphone7&quot;&gt;iPhone 7&lt;/option&gt;
  295. &lt;/select&gt;
  296. &lt;div id=&quot;chart-year&quot;&gt;
  297. &lt;apexchart type=&quot;bar&quot; height=&quot;400&quot; ref=&quot;chart&quot; :options=&quot;chartOptions&quot; :series=&quot;series&quot;&gt;&lt;/apexchart&gt;
  298. &lt;/div&gt;
  299. &lt;div id=&quot;chart-quarter&quot;&gt;
  300. &lt;apexchart type=&quot;bar&quot; height=&quot;400&quot; ref=&quot;chartQuarter&quot; :options=&quot;chartOptionsQuarter&quot; :series=&quot;seriesQuarter&quot;&gt;&lt;/apexchart&gt;
  301. &lt;/div&gt;
  302. &lt;/div&gt;
  303. </div>
  304. <script>
  305. new Vue({
  306. el: '#app',
  307. components: {
  308. apexchart: VueApexCharts,
  309. },
  310. data: {
  311. series: [{
  312. data: makeData()
  313. }],
  314. chartOptions: {
  315. chart: {
  316. id: 'barYear',
  317. height: 400,
  318. width: '100%',
  319. type: 'bar',
  320. events: {
  321. dataPointSelection: function (e, chart, opts) {
  322. var quarterChartEl = document.querySelector("#chart-quarter");
  323. var yearChartEl = document.querySelector("#chart-year");
  324. if (opts.selectedDataPoints[0].length === 1) {
  325. if (quarterChartEl.classList.contains("active")) {
  326. updateQuarterChart(chart, 'barQuarter')
  327. } else {
  328. yearChartEl.classList.add("chart-quarter-activated")
  329. quarterChartEl.classList.add("active");
  330. updateQuarterChart(chart, 'barQuarter')
  331. }
  332. } else {
  333. updateQuarterChart(chart, 'barQuarter')
  334. }
  335. if (opts.selectedDataPoints[0].length === 0) {
  336. yearChartEl.classList.remove("chart-quarter-activated")
  337. quarterChartEl.classList.remove("active");
  338. }
  339. },
  340. updated: function (chart) {
  341. updateQuarterChart(chart, 'barQuarter')
  342. }
  343. }
  344. },
  345. plotOptions: {
  346. bar: {
  347. distributed: true,
  348. horizontal: true,
  349. barHeight: '75%',
  350. dataLabels: {
  351. position: 'bottom'
  352. }
  353. }
  354. },
  355. dataLabels: {
  356. enabled: true,
  357. textAnchor: 'start',
  358. style: {
  359. colors: ['#fff']
  360. },
  361. formatter: function (val, opt) {
  362. return opt.w.globals.labels[opt.dataPointIndex]
  363. },
  364. offsetX: 0,
  365. dropShadow: {
  366. enabled: true
  367. }
  368. },
  369. colors: colors,
  370. states: {
  371. normal: {
  372. filter: {
  373. type: 'desaturate'
  374. }
  375. },
  376. active: {
  377. allowMultipleDataPointsSelection: true,
  378. filter: {
  379. type: 'darken',
  380. value: 1
  381. }
  382. }
  383. },
  384. tooltip: {
  385. x: {
  386. show: false
  387. },
  388. y: {
  389. title: {
  390. formatter: function (val, opts) {
  391. return opts.w.globals.labels[opts.dataPointIndex]
  392. }
  393. }
  394. }
  395. },
  396. title: {
  397. text: 'Yearly Results',
  398. offsetX: 15
  399. },
  400. subtitle: {
  401. text: '(Click on bar to see details)',
  402. offsetX: 15
  403. },
  404. yaxis: {
  405. labels: {
  406. show: false
  407. }
  408. }
  409. },
  410. seriesQuarter: [{
  411. data: []
  412. }],
  413. chartOptionsQuarter: {
  414. chart: {
  415. id: 'barQuarter',
  416. height: 400,
  417. width: '100%',
  418. type: 'bar',
  419. stacked: true
  420. },
  421. plotOptions: {
  422. bar: {
  423. columnWidth: '50%',
  424. horizontal: false
  425. }
  426. },
  427. legend: {
  428. show: false
  429. },
  430. grid: {
  431. yaxis: {
  432. lines: {
  433. show: false,
  434. }
  435. },
  436. xaxis: {
  437. lines: {
  438. show: true,
  439. }
  440. }
  441. },
  442. yaxis: {
  443. labels: {
  444. show: false
  445. }
  446. },
  447. title: {
  448. text: 'Quarterly Results',
  449. offsetX: 10
  450. },
  451. tooltip: {
  452. x: {
  453. formatter: function (val, opts) {
  454. return opts.w.globals.seriesNames[opts.seriesIndex]
  455. }
  456. },
  457. y: {
  458. title: {
  459. formatter: function (val, opts) {
  460. return opts.w.globals.labels[opts.dataPointIndex]
  461. }
  462. }
  463. }
  464. }
  465. },
  466. },
  467. methods: {
  468. changeData: function() {
  469. this.$refs.chart.updateSeries([{
  470. data: makeData()
  471. }])
  472. }
  473. }
  474. })
  475. </script>
  476. </body>
  477. </html>