charts.mdx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. ---
  2. title: Charts
  3. libs: apexcharts
  4. summary: Tabler uses ApexCharts - a free and open-source modern charting library that helps developers to create beautiful and interactive visualizations for web pages.
  5. description: Interactive data visualizations with ApexCharts.
  6. ---
  7. To be able to use the charts in your application you will need to install the apexcharts dependency with `npm install apexcharts`.
  8. See also the [ApexCharts](https://apexcharts.com/) documentation.
  9. ## Line Chart
  10. Line charts are an essential tool for visualizing data trends over time. They are particularly useful for representing continuous data, such as stock prices, website traffic, or user activity. Below is an example of a line chart showcasing session duration, page views, and total visits:
  11. ```html example centered columns={2} height="25rem" libs="apexcharts"
  12. <div class="card">
  13. <div class="card-body">
  14. <div id="chart-demo-line" class="chart-lg"></div>
  15. </div>
  16. </div>
  17. <script>
  18. document.addEventListener("DOMContentLoaded", function() {
  19. window.ApexCharts && (new ApexCharts(document.getElementById('chart-demo-line'), {
  20. chart: {
  21. type: "line",
  22. fontFamily: 'inherit',
  23. height: 240,
  24. parentHeightOffset: 0,
  25. toolbar: {
  26. show: false,
  27. },
  28. animations: {
  29. enabled: false
  30. },
  31. },
  32. fill: {
  33. opacity: 1,
  34. },
  35. stroke: {
  36. width: 2,
  37. lineCap: "round",
  38. curve: "straight",
  39. },
  40. series: [{
  41. name: "Session Duration",
  42. data: [117, 92, 94, 98, 75, 110, 69, 80, 109, 113, 115, 95]
  43. }, {
  44. name: "Page Views",
  45. data: [59, 80, 61, 66, 70, 84, 87, 64, 94, 56, 55, 67]
  46. }, {
  47. name: "Total Visits",
  48. data: [53, 51, 52, 41, 46, 60, 45, 43, 30, 50, 58, 59]
  49. }],
  50. tooltip: {
  51. theme: 'dark'
  52. },
  53. grid: {
  54. padding: {
  55. top: -20,
  56. right: 0,
  57. left: -4,
  58. bottom: -4
  59. },
  60. strokeDashArray: 4,
  61. },
  62. xaxis: {
  63. labels: {
  64. padding: 0,
  65. },
  66. tooltip: {
  67. enabled: false
  68. },
  69. type: 'datetime',
  70. },
  71. yaxis: {
  72. labels: {
  73. padding: 4
  74. },
  75. },
  76. labels: [
  77. '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02'
  78. ],
  79. colors: [tabler.getColor("yellow"), tabler.getColor("green"), tabler.getColor("primary")],
  80. legend: {
  81. show: true,
  82. position: 'bottom',
  83. offsetY: 12,
  84. markers: {
  85. width: 10,
  86. height: 10,
  87. radius: 100,
  88. },
  89. itemMargin: {
  90. horizontal: 8,
  91. vertical: 8
  92. },
  93. },
  94. })).render();
  95. });
  96. </script>
  97. ```
  98. ## Area Chart
  99. Area charts are ideal for representing cumulative data over time. They add visual emphasis to trends by filling the space under the line, making it easier to compare values. Here's an example of an area chart with smooth transitions and data from two series:
  100. ```html example centered columns={2} height="25rem" libs="apexcharts"
  101. <div class="card">
  102. <div class="card-body">
  103. <div id="chart-demo-area" class="chart-lg"></div>
  104. </div>
  105. </div>
  106. <script>
  107. document.addEventListener("DOMContentLoaded", function() {
  108. window.ApexCharts && (new ApexCharts(document.getElementById('chart-demo-area'), {
  109. chart: {
  110. type: "area",
  111. fontFamily: 'inherit',
  112. height: 240,
  113. parentHeightOffset: 0,
  114. toolbar: {
  115. show: false,
  116. },
  117. animations: {
  118. enabled: false
  119. },
  120. },
  121. dataLabels: {
  122. enabled: false,
  123. },
  124. fill: {
  125. opacity: .16,
  126. type: 'solid'
  127. },
  128. stroke: {
  129. width: 2,
  130. lineCap: "round",
  131. curve: "smooth",
  132. },
  133. series: [{
  134. name: "series1",
  135. data: [56, 40, 39, 47, 34, 48, 44]
  136. }, {
  137. name: "series2",
  138. data: [45, 43, 30, 23, 38, 39, 54]
  139. }],
  140. tooltip: {
  141. theme: 'dark'
  142. },
  143. grid: {
  144. padding: {
  145. top: -20,
  146. right: 0,
  147. left: -4,
  148. bottom: -4
  149. },
  150. strokeDashArray: 4,
  151. },
  152. xaxis: {
  153. labels: {
  154. padding: 0,
  155. },
  156. tooltip: {
  157. enabled: false
  158. },
  159. axisBorder: {
  160. show: false,
  161. },
  162. type: 'datetime',
  163. },
  164. yaxis: {
  165. labels: {
  166. padding: 4
  167. },
  168. },
  169. labels: [
  170. '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27'
  171. ],
  172. colors: [tabler.getColor("primary"), tabler.getColor("purple")],
  173. legend: {
  174. show: true,
  175. position: 'bottom',
  176. offsetY: 12,
  177. markers: {
  178. width: 10,
  179. height: 10,
  180. radius: 100,
  181. },
  182. itemMargin: {
  183. horizontal: 8,
  184. vertical: 8
  185. },
  186. },
  187. })).render();
  188. });
  189. </script>
  190. ```
  191. ## Bar Chart
  192. Bar charts are highly effective for comparing data across different categories. They provide a clear and concise way to visualize differences in values, making them perfect for analyzing categorical data. Here's an example of a bar chart with stacked bars for enhanced readability:
  193. ```html example centered columns={2} height="25rem" libs="apexcharts"
  194. <div class="card">
  195. <div class="card-body">
  196. <div id="chart-demo-bar" class="chart-lg"></div>
  197. </div>
  198. </div>
  199. <script>
  200. document.addEventListener("DOMContentLoaded", function() {
  201. window.ApexCharts && (new ApexCharts(document.getElementById('chart-demo-bar'), {
  202. chart: {
  203. type: "bar",
  204. fontFamily: 'inherit',
  205. height: 240,
  206. parentHeightOffset: 0,
  207. toolbar: {
  208. show: false,
  209. },
  210. animations: {
  211. enabled: false
  212. },
  213. stacked: true,
  214. },
  215. plotOptions: {
  216. bar: {
  217. barHeight: '50%',
  218. horizontal: true,
  219. }
  220. },
  221. dataLabels: {
  222. enabled: false,
  223. },
  224. fill: {
  225. opacity: 1,
  226. },
  227. series: [{
  228. name: "Container for a Fanta",
  229. data: [44, 55, 41, 37, 22, 43, 21]
  230. }, {
  231. name: "Strange sunglasses",
  232. data: [53, 32, 33, 52, 13, 43, 32]
  233. }, {
  234. name: "Pen Pineapple Apple Pen",
  235. data: [12, 17, 11, 9, 15, 11, 20]
  236. }, {
  237. name: "Binoculars",
  238. data: [9, 7, 5, 8, 6, 9, 4]
  239. }, {
  240. name: "Magical notebook",
  241. data: [25, 12, 19, 32, 25, 24, 10]
  242. }],
  243. tooltip: {
  244. theme: 'dark'
  245. },
  246. grid: {
  247. padding: {
  248. top: -20,
  249. right: 0,
  250. left: -4,
  251. bottom: -4
  252. },
  253. strokeDashArray: 4,
  254. },
  255. xaxis: {
  256. labels: {
  257. padding: 0,
  258. formatter: function(val) {
  259. return val + "K"
  260. },
  261. },
  262. tooltip: {
  263. enabled: false
  264. },
  265. axisBorder: {
  266. show: false,
  267. },
  268. categories: ['2008', '2009', '2010', '2011', '2012', '2013', '2014'],
  269. },
  270. yaxis: {
  271. labels: {
  272. padding: 4
  273. },
  274. },
  275. colors: [tabler.getColor("purple"), tabler.getColor("green"), tabler.getColor("yellow"), tabler.getColor("red"), tabler.getColor("primary")],
  276. legend: {
  277. show: true,
  278. position: 'bottom',
  279. offsetY: 12,
  280. markers: {
  281. width: 10,
  282. height: 10,
  283. radius: 100,
  284. },
  285. itemMargin: {
  286. horizontal: 8,
  287. vertical: 8
  288. },
  289. },
  290. })).render();
  291. });
  292. </script>
  293. ```
  294. ## Pie Chart
  295. Pie charts are a simple and effective way to visualize proportions and ratios. They are commonly used to represent data as percentages of a whole, making them ideal for displaying parts of a dataset. Below is an example of a pie chart showcasing distribution across categories:
  296. ```html example centered columns={2} height="25rem" libs="apexcharts"
  297. <div class="card">
  298. <div class="card-body">
  299. <div id="chart-demo-pie" class="chart-lg"></div>
  300. </div>
  301. </div>
  302. <script>
  303. document.addEventListener("DOMContentLoaded", function() {
  304. window.ApexCharts && (new ApexCharts(document.getElementById('chart-demo-pie'), {
  305. chart: {
  306. type: "donut",
  307. fontFamily: 'inherit',
  308. height: 240,
  309. sparkline: {
  310. enabled: true
  311. },
  312. animations: {
  313. enabled: false
  314. },
  315. },
  316. fill: {
  317. opacity: 1,
  318. },
  319. series: [44, 55, 12, 2],
  320. labels: ["Direct", "Affilliate", "E-mail", "Other"],
  321. tooltip: {
  322. theme: 'dark'
  323. },
  324. grid: {
  325. strokeDashArray: 4,
  326. },
  327. colors: [tabler.getColor("primary"), tabler.getColor("primary", 0.8), tabler.getColor("primary", 0.6), tabler.getColor("gray-300")],
  328. legend: {
  329. show: true,
  330. position: 'bottom',
  331. offsetY: 12,
  332. markers: {
  333. width: 10,
  334. height: 10,
  335. radius: 100,
  336. },
  337. itemMargin: {
  338. horizontal: 8,
  339. vertical: 8
  340. },
  341. },
  342. tooltip: {
  343. fillSeriesColor: false
  344. },
  345. })).render();
  346. });
  347. </script>
  348. ```
  349. ## Heatmap Chart
  350. Heatmaps provide a graphical representation of data where individual values are represented by color intensity. They are particularly useful for identifying patterns or anomalies within large datasets. Here's an example of a heatmap chart to visualize data distributions:
  351. ```html example centered columns={2} height="25rem" libs="apexcharts"
  352. <div class="card">
  353. <div class="card-body">
  354. <div id="chart-demo-pie" class="chart-lg"></div>
  355. </div>
  356. </div>
  357. <script>
  358. document.addEventListener("DOMContentLoaded", function() {
  359. window.ApexCharts && (new ApexCharts(document.getElementById('chart-demo-pie'), {
  360. chart: {
  361. type: "donut",
  362. fontFamily: 'inherit',
  363. height: 240,
  364. sparkline: {
  365. enabled: true
  366. },
  367. animations: {
  368. enabled: false
  369. },
  370. },
  371. fill: {
  372. opacity: 1,
  373. },
  374. series: [44, 55, 12, 2],
  375. labels: ["Direct", "Affilliate", "E-mail", "Other"],
  376. tooltip: {
  377. theme: 'dark'
  378. },
  379. grid: {
  380. strokeDashArray: 4,
  381. },
  382. colors: [tabler.getColor("primary"), tabler.getColor("primary", 0.8), tabler.getColor("primary", 0.6), tabler.getColor("gray-300")],
  383. legend: {
  384. show: true,
  385. position: 'bottom',
  386. offsetY: 12,
  387. markers: {
  388. width: 10,
  389. height: 10,
  390. radius: 100,
  391. },
  392. itemMargin: {
  393. horizontal: 8,
  394. vertical: 8
  395. },
  396. },
  397. tooltip: {
  398. fillSeriesColor: false
  399. },
  400. })).render();
  401. });
  402. </script>
  403. ```
  404. ## Advanced example
  405. For more complex data visualizations, you can create advanced charts with multiple series and custom configurations. Below is an example of a social media referrals chart combining data from Facebook, Twitter, and Dribbble:
  406. ```html example centered columns={2} height="25rem" libs="apexcharts"
  407. <div class="card">
  408. <div class="card-body">
  409. <div id="chart-social-referrals" class="chart-lg"></div>
  410. </div>
  411. </div>
  412. <script>
  413. document.addEventListener("DOMContentLoaded", function() {
  414. window.ApexCharts && (new ApexCharts(document.getElementById('chart-social-referrals'), {
  415. chart: {
  416. type: "line",
  417. fontFamily: 'inherit',
  418. height: 240,
  419. parentHeightOffset: 0,
  420. toolbar: {
  421. show: false,
  422. },
  423. animations: {
  424. enabled: false
  425. },
  426. },
  427. fill: {
  428. opacity: 1,
  429. },
  430. stroke: {
  431. width: 2,
  432. lineCap: "round",
  433. curve: "smooth",
  434. },
  435. series: [{
  436. name: "Facebook",
  437. data: [13281, 8521, 15038, 9983, 15417, 8888, 7052, 14270, 5214, 9587, 5950, 16852, 17836, 12217, 17406, 12262, 9147, 14961, 18292, 15230, 13435, 10649, 5140, 13680, 4508, 13271, 13413, 5543, 18727, 18238, 18175, 6246, 5864, 17847, 9170, 6445, 12945, 8142, 8980, 10422, 15535, 11569, 10114, 17621, 16138, 13046, 6652, 9906, 14100, 16495, 6749]
  438. }, {
  439. name: "Twitter",
  440. data: [3680, 1862, 3070, 2252, 5348, 3091, 3000, 3984, 5176, 5325, 2420, 5474, 3098, 1893, 3748, 2879, 4197, 5186, 4213, 4334, 2807, 1594, 4863, 2030, 3752, 4856, 5341, 3954, 3461, 3097, 3404, 4949, 2283, 3227, 3630, 2360, 3477, 4675, 1901, 2252, 3347, 2954, 5029, 2079, 2830, 3292, 4578, 3401, 4104, 3749, 4457, 3734]
  441. }, {
  442. name: "Dribbble",
  443. data: [722, 1866, 961, 1108, 1110, 561, 1753, 1815, 1985, 776, 859, 547, 1488, 766, 702, 621, 1599, 1372, 1620, 963, 759, 764, 739, 789, 1696, 1454, 1842, 734, 551, 1689, 1924, 1875, 908, 1675, 1541, 1953, 534, 502, 1524, 1867, 719, 1472, 1608, 1025, 889, 1150, 654, 1695, 1662, 1285, 1787]
  444. }],
  445. tooltip: {
  446. theme: 'dark'
  447. },
  448. grid: {
  449. padding: {
  450. top: -20,
  451. right: 0,
  452. left: -4,
  453. bottom: -4
  454. },
  455. strokeDashArray: 4,
  456. xaxis: {
  457. lines: {
  458. show: true
  459. }
  460. },
  461. },
  462. xaxis: {
  463. labels: {
  464. padding: 0,
  465. },
  466. tooltip: {
  467. enabled: false
  468. },
  469. type: 'datetime',
  470. },
  471. yaxis: {
  472. labels: {
  473. padding: 4
  474. },
  475. },
  476. labels: [
  477. '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08', '2020-08-09', '2020-08-10'
  478. ],
  479. colors: [tabler.getColor("facebook"), tabler.getColor("twitter"), tabler.getColor("dribbble")],
  480. legend: {
  481. show: true,
  482. position: 'bottom',
  483. offsetY: 12,
  484. markers: {
  485. width: 10,
  486. height: 10,
  487. radius: 100,
  488. },
  489. itemMargin: {
  490. horizontal: 8,
  491. vertical: 8
  492. },
  493. },
  494. })).render();
  495. });
  496. </script>
  497. ```