Advertisement
Guest User

Untitled

a guest
Dec 1st, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 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 : <div id='fxd3'>$series.0.dataValue</div> <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. events: {
  144. dataPlotRollOver: function (e) {
  145. console.log(e.data.binValue);
  146. let x = e.data.binValue;
  147. var infoElem = document.getElementById("fxd3");
  148. if (infoElem) {
  149. infoElem.innerHTML = x.toLocaleString("id-ID", {
  150. minimumFractionDigits: 2,
  151. maximumFractionDigits: 2,
  152. });
  153.  
  154. console.log(infoElem.innerHTML);
  155. }
  156. },
  157. dataPlotRollOut: function (e) {
  158. console.log(e.data.binValue);
  159. let x = e.data.binValue;
  160. var infoElem = document.getElementById("fxd3");
  161. if (infoElem) {
  162. infoElem.innerHTML = x.toLocaleString("id-ID", {
  163. minimumFractionDigits: 2,
  164. maximumFractionDigits: 2,
  165. });
  166.  
  167. console.log(infoElem.innerHTML);
  168. }
  169. },
  170. },
  171. },
  172. timeseriesDs1: {
  173. id: "chart2", //ID chart must be exact same with function 'exportChart'
  174. type: "timeseries",
  175. renderAt: "container",
  176. width: "100%",
  177. height: "100%",
  178. dataSource,
  179. },
  180. };
  181. }
  182.  
  183. componentDidMount() {
  184. this.onFetchData();
  185. }
  186.  
  187. onFetchData() {
  188. Promise.all([rows, schemaFetch]).then((res) => {
  189. const data = res[0];
  190. const schema = res[1];
  191. console.log(JSON.stringify(data));
  192. console.log(JSON.stringify(schema));
  193. const fusionTable = new FusionCharts.DataStore().createDataTable(
  194. data,
  195. schema,
  196. );
  197. console.log(fusionTable);
  198. const timeseriesDs = Object.assign({}, this.state.timeseriesDs);
  199. timeseriesDs.dataSource.data = fusionTable;
  200. this.setState({
  201. timeseriesDs,
  202. });
  203.  
  204. console.log(this.state.timeseriesDs);
  205. });
  206. }
  207.  
  208. // Handler for export button.
  209. // Fires an export operation which exports all charts on the page as a PDF.
  210. // http://jsfiddle.net/fusioncharts/vga0prev/
  211. exportChart(e) {
  212. FusionCharts.batchExport({
  213. charts: [
  214. {
  215. id: "chart1",
  216. },
  217. {
  218. id: "chart2",
  219. },
  220. ],
  221. exportFileName: "batchExport",
  222. exportFormat: "pdf", //pdf,png,jpg
  223. exportAtClientSide: "1",
  224. exportMode: "auto",
  225. });
  226. }
  227.  
  228. render() {
  229. return (
  230. <div style={{ margin: "0.5em", height: "60vh" }}>
  231. {this.state.timeseriesDs.dataSource.data ? (
  232. <>
  233. <center>
  234. <button className='button' onClick={this.exportChart}>
  235. Export both charts as a single PDF
  236. </button>
  237. </center>
  238. <ReactFC {...this.state.timeseriesDs} />
  239. <ReactFC {...this.state.timeseriesDs1} />
  240. </>
  241. ) : (
  242. "loading"
  243. )}
  244. </div>
  245. );
  246. }
  247. }
  248.  
  249. export default TimseriesFushionChart;
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement