0
I have several charts from Google Charts because I am doing a Dashboard, the problem is that I need to make the connection with the bank via Nodejs for performance issues and acquire knowledge in new areas, but I can’t bring the data I already have in the bank (SQL Server) for the Javascript file where the Google Maps are located.
I’ve tried to express
, body-parser
and a little bit of everything I’ve seen on Google and Youtube. My code is in the MVC pattern on my PHP page I bring by the tag script the js files from Google Chart.
Code Google Chart:
google.charts.load('current', {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['CAMPO1', 12],
['CAMPO2', 2],
['CAMPO3', 2],
['CAMPO4', 2],
['CAMPO5', 7],
['CAMPO6', 2],
]);
var options = {
title: '',
width: '100%', height: '100%',
chartArea:{left:10,top:50,width:'80%',height:'100%'},
colors: ['#00eb0f', '#18b500', '#01730d', '#ffaa00', '#008cff', '#fc0303', '#c70000'],
backgroundColor: { fill:'transparent' },
textStyle: {color: 'white'},
pieHole: 0.4,
titleTextStyle: {
color: 'white'
},
legend: {position: 'right',textStyle: {color: 'white',fontSize: 14}}
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}
Code Nodejs:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 3000; //porta padrão
const sql = require("mssql");
const connStr = "Server=MEUSERVE;Database=MINHABASE;User Id=MEUUSER;Password=MINHASENHA;";
//fazendo a conexão global
sql.connect(connStr)
.then(conn => global.conn = conn)
.catch(err => console.log(err));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//definindo as rotas
const router = express.Router();
router.get('/', (req, res) => res.json({ message: 'Funcionando!' }));
app.use('/', router);
//inicia o servidor
//BODY PARSER
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.listen(port);
console.log('API funcionando!');
function execSQLQuery(sqlQry, res){
global.conn.request()
.query(sqlQry)
.then(result => res.json(result.recordset))
.catch(err => res.json(err));
}
router.get('/clientes', (req, res) =>{
let a = execSQLQuery("SELECT * FROM MINHA_TABLE;", res);
})
One important detail to be put is that in the URL :3000/clientes
brings an array with all the information I need but don’t know how to pass the fields I want inside Google Chart.