Advertisement
Guest User

Untitled

a guest
Dec 1st, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. import React from "react";
  2. import FusionCharts from "fusioncharts";
  3. import TimeSeries from "fusioncharts/fusioncharts.timeseries";
  4. import ReactFC from "react-fusioncharts";
  5. import FusionTheme from "fusioncharts/themes/fusioncharts.theme.fusion";
  6. import GammelTheme from "fusioncharts/themes/fusioncharts.theme.gammel";
  7. import CandyTheme from "fusioncharts/themes/fusioncharts.theme.candy";
  8. import ZuneTheme from "fusioncharts/themes/fusioncharts.theme.zune";
  9. import OceanTheme from "fusioncharts/themes/fusioncharts.theme.ocean";
  10. import CarbonTheme from "fusioncharts/themes/fusioncharts.theme.carbon";
  11. import UmberTheme from "fusioncharts/themes/fusioncharts.theme.umber";
  12. import PowerCharts from "fusioncharts/fusioncharts.powercharts";
  13. import ExcelExport from "fusioncharts/fusioncharts.excelexport";
  14.  
  15. ReactFC.fcRoot(
  16. FusionCharts,
  17. TimeSeries,
  18. PowerCharts,
  19. ExcelExport,
  20. TimeSeries,
  21. FusionTheme,
  22. GammelTheme,
  23. CandyTheme,
  24. ZuneTheme,
  25. OceanTheme,
  26. CarbonTheme,
  27. UmberTheme,
  28. );
  29. // const fetch = require("node-fetch");
  30. // const jsonify = (res) => res.json();
  31. // const dataFetch = [
  32. // ["01/05/20 06:00", "Actual", 1115],
  33. // ["01/05/20 06:00", "Estimate", 3235],
  34. // ["01/05/20 06:00", "Prediction", 5478],
  35. // ["01/05/20 07:00", "Actual", 1248],
  36. // ["01/05/20 07:00", "Prediction", 5356],
  37. // ["01/05/20 07:00", "Estimate", 3138],
  38. // ["01/05/20 07:00", "Estimate", 3335],
  39. // ["01/05/20 08:00", "Actual", 1129],
  40. // ["01/05/20 08:00", "Estimate", 2464],
  41. // ["01/05/20 08:00", "Prediction", 5403],
  42. // ["01/05/20 09:00", "Actual", 1045],
  43. // ["01/05/20 09:00", "Estimate", 3015],
  44. // ["01/05/20 09:00", "Prediction", 5348],
  45. // ];
  46.  
  47. const formatNumber = (x) => JSON.stringify(x).slice();
  48.  
  49. const rows = [["Meninggal", "20200629", 10000.745, "10.000,745"]];
  50.  
  51. // format: "%d-%b-%y",
  52. // "%d/%m/%y %H:%M"
  53. const schemaFetch = [
  54. {
  55. name: "Type",
  56. type: "string",
  57. },
  58. {
  59. name: "Time",
  60. type: "date",
  61. format: "%Y%m%d",
  62. },
  63. {
  64. name: "Positive",
  65. type: "number",
  66. },
  67. {
  68. name: "PositiveD",
  69. type: "string",
  70. },
  71. ];
  72.  
  73. const dataSource = {
  74. chart: {
  75. theme: "candy",
  76. formatNumber: "1",
  77. formatNumberScale: "0",
  78. forceDecimals: "1",
  79. forceYAxisValueDecimals: "1",
  80. forceXAxisValueDecimals: "1",
  81. decimalSeparator: ",",
  82. thousandSeparator: ".",
  83. thousandSeparatorPosition: "2,3",
  84. paletteColors: "#52c41a,#1890ff,#fa8c16,#f5222d",
  85. exportEnabled: 1,
  86. exportFileName: "batchExport",
  87. exportFormat: "jpg",
  88. exportAtClientSide: 1,
  89. },
  90. caption: {
  91. text: "Covid 19 Indonesia",
  92. },
  93. subcaption: {
  94. text: "Data from https://data.kemkes.go.id/covid19/index.html",
  95. },
  96. series: "Type",
  97. yAxis: [
  98. {
  99. plot: "Positive",
  100. title: "Jiwa",
  101. format: {
  102. defaultFormat: 0,
  103. },
  104. },
  105. {
  106. plot: "PositiveD",
  107. title: "PositiveD",
  108. format: {
  109. // defaultFormat: 0,
  110. },
  111. },
  112. ],
  113. xAxis: {
  114. binning: {
  115. year: [],
  116. month: [],
  117. day: [],
  118. hour: [],
  119. minute: [],
  120. second: [],
  121. millisecond: [],
  122. },
  123. },
  124. tooltip: {
  125. enabled: true,
  126. toolText: `xxx : $series.0.dataValue <br> <b>$bin</b><br> Konfirmasi Baru: $series.0.dataValue<br>Sembuh: <b>$series.1.dataValue</b> <br> Meninggal: <b>$series.2.dataValue</b>`,
  127. },
  128. };
  129.  
  130. class TimseriesFushionChart extends React.Component {
  131. constructor(props) {
  132. super(props);
  133. this.onFetchData = this.onFetchData.bind(this);
  134. this.exportChart = this.exportChart.bind(this);
  135. this.state = {
  136. timeseriesDs: {
  137. id: "chart1", //ID chart must be exact same with function 'exportChart'
  138. type: "timeseries",
  139. renderAt: "container",
  140. width: "100%",
  141. height: "100%",
  142. dataSource,
  143. },
  144. timeseriesDs1: {
  145. id: "chart2", //ID chart must be exact same with function 'exportChart'
  146. type: "timeseries",
  147. renderAt: "container",
  148. width: "100%",
  149. height: "100%",
  150. dataSource,
  151. },
  152. };
  153. }
  154.  
  155. componentDidMount() {
  156. this.onFetchData();
  157. }
  158.  
  159. onFetchData() {
  160. Promise.all([rows, schemaFetch]).then((res) => {
  161. const data = res[0];
  162. const schema = res[1];
  163. console.log(JSON.stringify(data));
  164. console.log(JSON.stringify(schema));
  165. const fusionTable = new FusionCharts.DataStore().createDataTable(
  166. data,
  167. schema,
  168. );
  169. console.log(fusionTable);
  170. const timeseriesDs = Object.assign({}, this.state.timeseriesDs);
  171. timeseriesDs.dataSource.data = fusionTable;
  172. this.setState({
  173. timeseriesDs,
  174. });
  175.  
  176. console.log(this.state.timeseriesDs);
  177. });
  178. }
  179.  
  180. // Handler for export button.
  181. // Fires an export operation which exports all charts on the page as a PDF.
  182. // http://jsfiddle.net/fusioncharts/vga0prev/
  183. exportChart(e) {
  184. FusionCharts.batchExport({
  185. charts: [
  186. {
  187. id: "chart1",
  188. },
  189. {
  190. id: "chart2",
  191. },
  192. ],
  193. exportFileName: "batchExport",
  194. exportFormat: "pdf", //pdf,png,jpg
  195. exportAtClientSide: "1",
  196. exportMode: "auto",
  197. });
  198. }
  199.  
  200. render() {
  201. return (
  202. <div style={{ margin: "0.5em", height: "60vh" }}>
  203. {this.state.timeseriesDs.dataSource.data ? (
  204. <>
  205. <center>
  206. <button className='button' onClick={this.exportChart}>
  207. Export both charts as a single PDF
  208. </button>
  209. </center>
  210. <ReactFC {...this.state.timeseriesDs} />
  211. <ReactFC {...this.state.timeseriesDs1} />
  212. </>
  213. ) : (
  214. "loading"
  215. )}
  216. </div>
  217. );
  218. }
  219. }
  220.  
  221. export default TimseriesFushionChart;
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement