chartist.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/charts/chartist", ["jquery", "Site"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(require("jquery"), require("Site"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(global.jQuery, global.Site);
  11. global.chartsChartist = mod.exports;
  12. }
  13. })(this, function (_jquery, _Site) {
  14. "use strict";
  15. _jquery = babelHelpers.interopRequireDefault(_jquery);
  16. (0, _jquery.default)(document).ready(function ($$$1) {
  17. (0, _Site.run)();
  18. }); // Example Chartist Css Animation
  19. // ------------------------------
  20. (function () {
  21. var cssAnimationData = {
  22. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  23. series: [[1, 2, 2.7, 0, 3, 5, 3, 4, 8, 10, 12, 7], [0, 1.2, 2, 7, 2.5, 9, 5, 8, 9, 11, 14, 4], [10, 9, 8, 6.5, 6.8, 6, 5.4, 5.3, 4.5, 4.4, 3, 2.8]]
  24. };
  25. var cssAnimationResponsiveOptions = [[// Foundation.media_queries.small,
  26. {
  27. axisX: {
  28. labelInterpolationFnc: function labelInterpolationFnc(value, index) {
  29. // Interpolation function causes only every 2nd label to be displayed
  30. if (index % 2 !== 0) {
  31. return false;
  32. } else {
  33. return value;
  34. }
  35. }
  36. }
  37. }]];
  38. new Chartist.Line('#exampleLineAnimation', cssAnimationData, null, cssAnimationResponsiveOptions);
  39. })(); // Example Chartist Simple Line
  40. // ----------------------------
  41. (function () {
  42. new Chartist.Line('#exampleSimpleLine', {
  43. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
  44. series: [[12, 9, 7, 8, 5], [2, 1, 3.5, 7, 3], [1, 3, 4, 5, 6]]
  45. }, {
  46. fullWidth: true,
  47. chartPadding: {
  48. right: 40
  49. }
  50. });
  51. })(); // Example Chartist Line Scatter
  52. // -----------------------------
  53. (function () {
  54. var ctScatterTimes = function ctScatterTimes(n) {
  55. return Array.apply(null, new Array(n));
  56. };
  57. var ctScatterData = ctScatterTimes(52).map(Math.random).reduce(function (data, rnd, index) {
  58. data.labels.push(index + 1);
  59. data.series.forEach(function (series) {
  60. series.push(Math.random() * 100);
  61. });
  62. return data;
  63. }, {
  64. labels: [],
  65. series: ctScatterTimes(4).map(function () {
  66. return [];
  67. })
  68. });
  69. var ctScatterOptions = {
  70. showLine: false,
  71. axisX: {
  72. labelInterpolationFnc: function labelInterpolationFnc(value, index) {
  73. return index % 13 === 0 ? 'W' + value : null;
  74. }
  75. }
  76. };
  77. var ctScatterResponsiveOptions = [['screen and (min-width: 640px)', {
  78. axisX: {
  79. labelInterpolationFnc: function labelInterpolationFnc(value, index) {
  80. return index % 4 === 0 ? 'W' + value : null;
  81. }
  82. }
  83. }]];
  84. new Chartist.Line('#exampleLineScatter', ctScatterData, ctScatterOptions, ctScatterResponsiveOptions);
  85. })(); // Example Chartist Line Chart With Tooltips
  86. // -----------------------------------------
  87. (function () {
  88. new Chartist.Line('#exampleTooltipsLine', {
  89. labels: ['1', '2', '3', '4', '5', '6'],
  90. series: [{
  91. name: 'Fibonacci sequence',
  92. data: [1, 2, 3, 5, 8, 13]
  93. }, {
  94. name: 'Golden section',
  95. data: [1, 1.618, 2.618, 4.236, 6.854, 11.09]
  96. }]
  97. }, {
  98. plugins: [Chartist.plugins.tooltip()]
  99. });
  100. var $ctTooltipsChart = (0, _jquery.default)('#exampleTooltipsLine');
  101. })(); // Example Chartist Line Chart With Area
  102. // -------------------------------------
  103. (function () {
  104. new Chartist.Line('#exampleAreaLine', {
  105. labels: [1, 2, 3, 4, 5, 6, 7, 8],
  106. series: [[5, 9, 7, 8, 5, 3, 5, 4]]
  107. }, {
  108. low: 0,
  109. showArea: true
  110. });
  111. })(); // Example Chartist Bi-Polar Line
  112. // ------------------------------
  113. (function () {
  114. new Chartist.Line('#exampleOnlyArea', {
  115. labels: [1, 2, 3, 4, 5, 6, 7, 8],
  116. series: [[1, 2, 3, 1, -2, 0, 1, 0], [-2, -1, -2, -1, -2.5, -1, -2, -1], [0, 0, 0, 1, 2, 2.5, 2, 1], [2.5, 2, 1, 0.5, 1, 0.5, -1, -2.5]]
  117. }, {
  118. high: 3,
  119. low: -3,
  120. showArea: true,
  121. showLine: false,
  122. showPoint: false,
  123. fullWidth: true,
  124. axisX: {
  125. showLabel: false,
  126. showGrid: false
  127. }
  128. });
  129. })(); // Example Chartist Advanced Smil Animations
  130. // -----------------------------------------
  131. (function () {
  132. var animationsChart = new Chartist.Line('#exampleLineAnimations', {
  133. labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  134. series: [[12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6], [4, 5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5], [5, 3, 4, 5, 6, 3, 3, 4, 5, 6, 3, 4], [3, 4, 5, 6, 7, 6, 4, 5, 6, 7, 6, 3]]
  135. }, {
  136. low: 0
  137. }); // // Let's put a sequence number aside so we can use it in the event callbacks
  138. var seq = 0,
  139. delays = 80,
  140. durations = 500; // Once the chart is fully created we reset the sequence
  141. animationsChart.on('created', function () {
  142. seq = 0;
  143. }); // // On each drawn element by Chartist we use the Chartist.Svg API to trigger SMIL animations
  144. animationsChart.on('draw', function (data) {
  145. seq++;
  146. if (data.type === 'line') {
  147. // If the drawn element is a line we do a simple opacity fade in. This could also be achieved using CSS3 animations.
  148. data.element.animate({
  149. opacity: {
  150. // The delay when we like to start the animation
  151. begin: seq * delays + 1000,
  152. // Duration of the animation
  153. dur: durations,
  154. // The value where the animation should start
  155. from: 0,
  156. // The value where it should end
  157. to: 1
  158. }
  159. });
  160. } else if (data.type === 'label' && data.axis === 'x') {
  161. data.element.animate({
  162. y: {
  163. begin: seq * delays,
  164. dur: durations,
  165. from: data.y + 100,
  166. to: data.y,
  167. // We can specify an easing function from Chartist.Svg.Easing
  168. easing: 'easeOutQuart'
  169. }
  170. });
  171. } else if (data.type === 'label' && data.axis === 'y') {
  172. data.element.animate({
  173. x: {
  174. begin: seq * delays,
  175. dur: durations,
  176. from: data.x - 100,
  177. to: data.x,
  178. easing: 'easeOutQuart'
  179. }
  180. });
  181. } else if (data.type === 'point') {
  182. data.element.animate({
  183. x1: {
  184. begin: seq * delays,
  185. dur: durations,
  186. from: data.x - 10,
  187. to: data.x,
  188. easing: 'easeOutQuart'
  189. },
  190. x2: {
  191. begin: seq * delays,
  192. dur: durations,
  193. from: data.x - 10,
  194. to: data.x,
  195. easing: 'easeOutQuart'
  196. },
  197. opacity: {
  198. begin: seq * delays,
  199. dur: durations,
  200. from: 0,
  201. to: 1,
  202. easing: 'easeOutQuart'
  203. }
  204. });
  205. } else if (data.type === 'grid') {
  206. // Using data.axis we get x or y which we can use to construct our animation definition objects
  207. var pos1Animation = {
  208. begin: seq * delays,
  209. dur: durations,
  210. from: data[data.axis.units.pos + '1'] - 30,
  211. to: data[data.axis.units.pos + '1'],
  212. easing: 'easeOutQuart'
  213. };
  214. var pos2Animation = {
  215. begin: seq * delays,
  216. dur: durations,
  217. from: data[data.axis.units.pos + '2'] - 100,
  218. to: data[data.axis.units.pos + '2'],
  219. easing: 'easeOutQuart'
  220. };
  221. var ctAnimations = {};
  222. ctAnimations[data.axis.units.pos + '1'] = pos1Animation;
  223. ctAnimations[data.axis.units.pos + '2'] = pos2Animation;
  224. ctAnimations.opacity = {
  225. begin: seq * delays,
  226. dur: durations,
  227. from: 0,
  228. to: 1,
  229. easing: 'easeOutQuart'
  230. };
  231. data.element.animate(ctAnimations);
  232. }
  233. }); // For the sake of the example we update the chart every time it's created with a delay of 10 seconds
  234. animationsChart.on('created', function () {
  235. if (window.__exampleAnimateTimeout) {
  236. clearTimeout(window.__exampleAnimateTimeout);
  237. window.__exampleAnimateTimeout = null;
  238. }
  239. window.__exampleAnimateTimeout = setTimeout(animationsChart.update.bind(animationsChart), 12000);
  240. });
  241. })(); // Example Chartist Svg Path Animation
  242. // -----------------------------------
  243. (function () {
  244. //ct-path-animation
  245. var pathAnimationChart = new Chartist.Line('#examplePathAnimation', {
  246. labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  247. series: [[1, 5, 2, 5, 4, 3], [2, 3, 4, 8, 1, 2], [5, 4, 3, 2, 1, 0.5]]
  248. }, {
  249. low: 0,
  250. showArea: true,
  251. showPoint: false,
  252. fullWidth: true
  253. });
  254. pathAnimationChart.on('draw', function (data) {
  255. if (data.type === 'line' || data.type === 'area') {
  256. data.element.animate({
  257. d: {
  258. begin: 2000 * data.index,
  259. dur: 2000,
  260. from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
  261. to: data.path.clone().stringify(),
  262. easing: Chartist.Svg.Easing.easeOutQuint
  263. }
  264. });
  265. }
  266. });
  267. })(); // Example Chartist Line Interpolation
  268. // -----------------------------------
  269. (function () {
  270. var smoothingChart = new Chartist.Line('#exampleSmoothingLine', {
  271. labels: [1, 2, 3, 4, 5],
  272. series: [[1, 5, 10, 0, 1], [10, 15, 0, 1, 2]]
  273. }, {
  274. // Remove this configuration to see that chart rendered with cardinal spline interpolation
  275. // Sometimes, on large jumps in data values, it's better to use simple smoothing.
  276. lineSmooth: Chartist.Interpolation.simple({
  277. divisor: 2
  278. }),
  279. fullWidth: true,
  280. chartPadding: {
  281. right: 20
  282. },
  283. low: 0
  284. });
  285. })(); // Example Chartist Bi-Polar Bar
  286. // -----------------------------
  287. (function () {
  288. var biPolarData = {
  289. labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
  290. series: [[1, 2, 4, 8, 6, -2, -1, -4, -6, -2]]
  291. };
  292. var biPolarOptions = {
  293. high: 10,
  294. low: -10,
  295. axisX: {
  296. labelInterpolationFnc: function labelInterpolationFnc(value, index) {
  297. return index % 2 === 0 ? value : null;
  298. }
  299. }
  300. };
  301. new Chartist.Bar('#exampleBiPolarBar', biPolarData, biPolarOptions);
  302. })(); // Example Chartist Overlapping Bars
  303. // ---------------------------------
  304. (function () {
  305. var overlappingData = {
  306. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  307. series: [[5, 4, 3, 7, 5, 10, 3, 4, 8, 10, 6, 8], [3, 2, 9, 5, 4, 6, 4, 6, 7, 8, 7, 4]]
  308. };
  309. var overlappingOptions = {
  310. seriesBarDistance: 10
  311. };
  312. var overlappingResponsiveOptions = [['screen and (max-width: 640px)', {
  313. seriesBarDistance: 5,
  314. axisX: {
  315. labelInterpolationFnc: function labelInterpolationFnc(value) {
  316. return value[0];
  317. }
  318. }
  319. }]];
  320. new Chartist.Bar('#exampleOverlappingBar', overlappingData, overlappingOptions, overlappingResponsiveOptions);
  321. })(); // Example Chartist Add Peak Circles
  322. // ---------------------------------
  323. (function () {
  324. // Create a simple bi-polar bar chart
  325. var peakCirclesChart = new Chartist.Bar('#examplePeakCirclesBar', {
  326. labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
  327. series: [[1, 2, 4, 8, 6, -2, -1, -4, -6, -2]]
  328. }, {
  329. high: 10,
  330. low: -10,
  331. axisX: {
  332. labelInterpolationFnc: function labelInterpolationFnc(value, index) {
  333. return index % 2 === 0 ? value : null;
  334. }
  335. }
  336. }); // Listen for draw events on the bar chart
  337. peakCirclesChart.on('draw', function (data) {
  338. // If this draw event is of type bar we can use the data to create additional content
  339. if (data.type === 'bar') {
  340. // We use the group element of the current series to append a simple circle with the bar peek coordinates and a circle radius that is depending on the value
  341. data.group.append(new Chartist.Svg('circle', {
  342. cx: data.x2,
  343. cy: data.y2,
  344. r: Math.abs(Chartist.getMultiValue(data.value)) * 2 + 5
  345. }, 'ct-slice-pie'));
  346. }
  347. });
  348. })(); // Example Chartist Multi-Line Labels
  349. // ----------------------------------
  350. (function () {
  351. new Chartist.Bar('#exampleMultiLabelsBar', {
  352. labels: ['First quarter of the year', 'Second quarter of the year', 'Third quarter of the year', 'Fourth quarter of the year'],
  353. series: [[60000, 40000, 80000, 70000], [40000, 30000, 70000, 65000], [8000, 3000, 10000, 6000]]
  354. }, {
  355. seriesBarDistance: 10,
  356. axisX: {
  357. offset: 60
  358. },
  359. axisY: {
  360. offset: 80,
  361. labelInterpolationFnc: function labelInterpolationFnc(value) {
  362. return value + ' CHF';
  363. },
  364. scaleMinSpace: 15
  365. }
  366. });
  367. })(); // Example Chartist Stacked Bar Chart
  368. // ----------------------------------
  369. (function () {
  370. new Chartist.Bar('#exampleStackedBar', {
  371. labels: ['Q1', 'Q2', 'Q3', 'Q4'],
  372. series: [[800000, 1200000, 1400000, 1300000], [200000, 400000, 500000, 300000], [100000, 200000, 400000, 600000]]
  373. }, {
  374. stackBars: true,
  375. axisY: {
  376. labelInterpolationFnc: function labelInterpolationFnc(value) {
  377. return value / 1000 + 'k';
  378. }
  379. }
  380. }).on('draw', function (data) {
  381. if (data.type === 'bar') {
  382. data.element.attr({
  383. style: 'stroke-width: 30px'
  384. });
  385. }
  386. });
  387. })(); // Example Chartist Horizontal Bar
  388. // -------------------------------
  389. (function () {
  390. new Chartist.Bar('#exampleHorizontalBar', {
  391. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
  392. series: [[5, 4, 3, 7, 5, 10, 3], [3, 2, 9, 5, 4, 6, 4]]
  393. }, {
  394. seriesBarDistance: 10,
  395. reverseData: true,
  396. horizontalBars: true,
  397. axisY: {
  398. offset: 70
  399. }
  400. });
  401. })(); // Example Chartist Extreme Responsive
  402. // -----------------------------------
  403. (function () {
  404. new Chartist.Bar('#exampleResponsiveBar', {
  405. labels: ['Quarter 1', 'Quarter 2', 'Quarter 3', 'Quarter 4'],
  406. series: [[5, 4, 3, 7], [3, 2, 9, 5], [1, 5, 8, 4], [2, 3, 4, 6], [4, 1, 2, 1]]
  407. }, {
  408. // Default mobile configuration
  409. stackBars: true,
  410. axisX: {
  411. labelInterpolationFnc: function labelInterpolationFnc(value) {
  412. return value.split(/\s+/).map(function (word) {
  413. return word[0];
  414. }).join('');
  415. }
  416. },
  417. axisY: {
  418. offset: 20
  419. }
  420. }, [// Options override for media > 400px
  421. ['screen and (min-width: 480px)', {
  422. reverseData: true,
  423. horizontalBars: true,
  424. axisX: {
  425. labelInterpolationFnc: Chartist.noop
  426. },
  427. axisY: {
  428. offset: 60
  429. }
  430. }], // Options override for media > 800px
  431. ['screen and (min-width: 992px)', {
  432. stackBars: false,
  433. seriesBarDistance: 10
  434. }], // Options override for media > 1000px
  435. ['screen and (min-width: 1200px)', {
  436. reverseData: false,
  437. horizontalBars: false,
  438. seriesBarDistance: 15
  439. }]]);
  440. })(); // Example Chartist Simple Pie
  441. // ---------------------------
  442. (function () {
  443. var simplePiedata = {
  444. series: [5, 3, 4]
  445. };
  446. var simplePieSum = function simplePieSum(a, b) {
  447. return a + b;
  448. };
  449. new Chartist.Pie('#exampleSimplePie', simplePiedata, {
  450. labelInterpolationFnc: function labelInterpolationFnc(value) {
  451. return Math.round(value / simplePiedata.series.reduce(simplePieSum) * 100) + '%';
  452. }
  453. });
  454. })(); // Example Chartist Pie Chart Labels
  455. // ---------------------------------
  456. (function () {
  457. var labelsPieData = {
  458. labels: ['Bananas', 'Apples', 'Grapes'],
  459. series: [20, 15, 40]
  460. };
  461. var labelsPieOptions = {
  462. labelInterpolationFnc: function labelInterpolationFnc(value) {
  463. return value[0];
  464. }
  465. };
  466. var labelsPieResponsiveOptions = [['screen and (min-width: 640px)', {
  467. chartPadding: 30,
  468. labelOffset: 100,
  469. labelDirection: 'explode',
  470. labelInterpolationFnc: function labelInterpolationFnc(value) {
  471. return value;
  472. }
  473. }], ['screen and (min-width: 1024px)', {
  474. labelOffset: 80,
  475. chartPadding: 20
  476. }]];
  477. new Chartist.Pie('#exampleLabelsPie', labelsPieData, labelsPieOptions, labelsPieResponsiveOptions);
  478. })(); // Example Chartist Gauge Pie
  479. // --------------------------
  480. (function () {
  481. new Chartist.Pie('#exampleGaugePie', {
  482. series: [20, 10, 30, 40]
  483. }, {
  484. donut: true,
  485. donutWidth: 60,
  486. startAngle: 270,
  487. total: 200,
  488. showLabel: false
  489. });
  490. })();
  491. });