Guest User

Untitled

a guest
Mar 22nd, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { ChartJSNodeCanvas } = require('chartjs-node-canvas');
  2. const fs = require("fs");
  3. const width = 1080; //px
  4. const height = 540; //px
  5. const for_chart_labels = ['01.02.2021 Пн/Mon', '02.02.2021 Вт/Tue', '03.02.2021 Ср/Wed', '04.02.2021 Чт/Thu', '05.02.2021 Пт/Fri', '06.02.2021 Сб/Sat', '07.02.2021 Вс/Sun'];
  6. const for_chart_data = [8706, 2725, 6821, 7602, 8452, 0, 8205];
  7. const chartJSNodeCanvas = new ChartJSNodeCanvas({ width, height });
  8.  
  9. (async () => {
  10.     const configuration = {
  11.         type: "bar",
  12.         data: {
  13.             labels: for_chart_labels,
  14.             datasets: [{
  15.                 label: "",
  16.                 borderWidth: 2,
  17.                 borderColor: [
  18.                     "rgb(255, 99, 132)",
  19.                     "rgb(54, 162, 235)",
  20.                     "rgb(255, 206, 86)",
  21.                     "rgb(75, 192, 192)",
  22.                     "rgb(153, 102, 255)",
  23.                     "rgb(255, 159, 64)",
  24.                     "rgb(255, 99, 132)"
  25.                 ],
  26.                 data: for_chart_data,
  27.                 backgroundColor: [
  28.                 "rgba(255, 99, 132, 0.2)",
  29.                 "rgba(54, 162, 235, 0.2)",
  30.                 "rgba(255, 206, 86, 0.2)",
  31.                 "rgba(75, 192, 192, 0.2)",
  32.                 "rgba(153, 102, 255, 0.2)",
  33.                 "rgba(255, 159, 64, 0.2)",
  34.                 "rgba(255, 99, 132, 0.2)"
  35.             ]
  36.             }]
  37.         },
  38.         options: {
  39.             legend: {
  40.                 display: false
  41.             },
  42.             scales: {
  43.                 xAxes: [{
  44.                     gridLines: {
  45.                         display: false,
  46.                     },
  47.                     ticks: {
  48.                         display: false
  49.                     }
  50.                 }],
  51.                 yAxes: [{
  52.                     gridLines: {
  53.                         display: false,
  54.                     },
  55.                     ticks: {
  56.                         display: false
  57.                     }
  58.                 }]
  59.             }
  60.     }};
  61.     const image = await chartJSNodeCanvas.renderToBuffer(configuration);
  62.    
  63.     fs.writeFile("./hello.png", image, function(error){
  64.         if(error) throw error; // если возникла ошибка
  65.  
  66.     });
  67. })();
  68.  
Advertisement
Add Comment
Please, Sign In to add comment