bar-with-negative.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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>Stacked Bar Chart with Negative Values</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. #chart {
  11. max-width: 650px;
  12. margin: 35px auto;
  13. }
  14. </style>
  15. <script>
  16. window.Promise ||
  17. document.write(
  18. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  19. )
  20. window.Promise ||
  21. document.write(
  22. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  23. )
  24. window.Promise ||
  25. document.write(
  26. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  27. )
  28. </script>
  29. <script src="https://cdn.jsdelivr.net/npm/react@16.12/umd/react.production.min.js"></script>
  30. <script src="https://cdn.jsdelivr.net/npm/react-dom@16.12/umd/react-dom.production.min.js"></script>
  31. <script src="https://cdn.jsdelivr.net/npm/prop-types@15.7.2/prop-types.min.js"></script>
  32. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
  33. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  34. <script src="https://cdn.jsdelivr.net/npm/react-apexcharts@1.3.6/dist/react-apexcharts.iife.min.js"></script>
  35. </head>
  36. <body>
  37. <div id="app"></div>
  38. <div id="html">
  39. &lt;div id=&quot;chart&quot;&gt;
  40. &lt;ReactApexChart options={this.state.options} series={this.state.series} type=&quot;bar&quot; height={440} /&gt;
  41. &lt;/div&gt;
  42. </div>
  43. <script type="text/babel">
  44. class ApexChart extends React.Component {
  45. constructor(props) {
  46. super(props);
  47. this.state = {
  48. series: [{
  49. name: 'Males',
  50. data: [0.4, 0.65, 0.76, 0.88, 1.5, 2.1, 2.9, 3.8, 3.9, 4.2, 4, 4.3, 4.1, 4.2, 4.5,
  51. 3.9, 3.5, 3
  52. ]
  53. },
  54. {
  55. name: 'Females',
  56. data: [-0.8, -1.05, -1.06, -1.18, -1.4, -2.2, -2.85, -3.7, -3.96, -4.22, -4.3, -4.4,
  57. -4.1, -4, -4.1, -3.4, -3.1, -2.8
  58. ]
  59. }
  60. ],
  61. options: {
  62. chart: {
  63. type: 'bar',
  64. height: 440,
  65. stacked: true
  66. },
  67. colors: ['#008FFB', '#FF4560'],
  68. plotOptions: {
  69. bar: {
  70. horizontal: true,
  71. barHeight: '80%',
  72. },
  73. },
  74. dataLabels: {
  75. enabled: false
  76. },
  77. stroke: {
  78. width: 1,
  79. colors: ["#fff"]
  80. },
  81. grid: {
  82. xaxis: {
  83. lines: {
  84. show: false
  85. }
  86. }
  87. },
  88. yaxis: {
  89. min: -5,
  90. max: 5,
  91. title: {
  92. // text: 'Age',
  93. },
  94. },
  95. tooltip: {
  96. shared: false,
  97. x: {
  98. formatter: function (val) {
  99. return val
  100. }
  101. },
  102. y: {
  103. formatter: function (val) {
  104. return Math.abs(val) + "%"
  105. }
  106. }
  107. },
  108. title: {
  109. text: 'Mauritius population pyramid 2011'
  110. },
  111. xaxis: {
  112. categories: ['85+', '80-84', '75-79', '70-74', '65-69', '60-64', '55-59', '50-54',
  113. '45-49', '40-44', '35-39', '30-34', '25-29', '20-24', '15-19', '10-14', '5-9',
  114. '0-4'
  115. ],
  116. title: {
  117. text: 'Percent'
  118. },
  119. labels: {
  120. formatter: function (val) {
  121. return Math.abs(Math.round(val)) + "%"
  122. }
  123. }
  124. },
  125. },
  126. };
  127. }
  128. render() {
  129. return (
  130. <div>
  131. <div id="chart">
  132. <ReactApexChart options={this.state.options} series={this.state.series} type="bar" height={440} />
  133. </div>
  134. <div id="html-dist"></div>
  135. </div>
  136. );
  137. }
  138. }
  139. const domContainer = document.querySelector('#app');
  140. ReactDOM.render(React.createElement(ApexChart), domContainer);
  141. </script>
  142. </body>
  143. </html>