StatsCharts.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import Area from './Area';
  5. import Pie from './Pie';
  6. import Bar from './Bar';
  7. const ChartsWrapper = styled.div`
  8. display: flex;
  9. flex-direction: column;
  10. align-items: stretch;
  11. padding: 32px;
  12. @media only screen and (max-width: 768px) {
  13. padding: 16px 16px 32px 16px;
  14. }
  15. `;
  16. const Row = styled.div`
  17. display: flex;
  18. border-bottom: 1px dotted #aaa;
  19. padding: 0 0 40px 0;
  20. margin: 0 0 32px 0;
  21. :last-child {
  22. border: none;
  23. margin: 0;
  24. }
  25. @media only screen and (max-width: 768px) {
  26. flex-direction: column;
  27. padding-bottom: 0;
  28. margin-bottom: 0;
  29. border-bottom: none;
  30. > *:not(:last-child) {
  31. padding-bottom: 24px;
  32. margin-bottom: 16px;
  33. border-bottom: 1px dotted #aaa;
  34. }
  35. }
  36. `;
  37. const StatsCharts = ({ stats, period }) => {
  38. const periodText = period.includes('last')
  39. ? `the last ${period.replace('last', '').toLocaleLowerCase()}`
  40. : 'all time';
  41. const hasView = stats.views.filter(view => view > 0);
  42. return (
  43. <ChartsWrapper>
  44. <Row>
  45. <Area data={stats.views} period={period} periodText={periodText} />
  46. </Row>
  47. {hasView.length
  48. ? [
  49. <Row key="second-row">
  50. <Pie data={stats.stats.referrer} title="Referrals" />
  51. <Bar data={stats.stats.browser} title="Browsers" />
  52. </Row>,
  53. <Row key="third-row">
  54. <Pie data={stats.stats.country} title="Country" />
  55. <Bar data={stats.stats.os} title="OS" />
  56. </Row>,
  57. ]
  58. : null}
  59. </ChartsWrapper>
  60. );
  61. };
  62. StatsCharts.propTypes = {
  63. period: PropTypes.string.isRequired,
  64. stats: PropTypes.shape({
  65. stats: PropTypes.object.isRequired,
  66. views: PropTypes.array.isRequired,
  67. }).isRequired,
  68. };
  69. export default StatsCharts;