简体   繁体   English

将Amcharts与NodeJS数据一起使用

[英]Using Amcharts with NodeJS data

I am trying to use Amcharts using data from Node JS. 我正在尝试使用来自Node JS的数据使用Amcharts。 I am trying to pass a json as a part of the response from Node server flie to the client side that I could use feed amcharts to create a pie chart. 我试图将json作为从Node服务器文件到客户端的响应的一部分传递,我可以使用提要amcharts创建饼图。 The code is as below - 代码如下-

app.js app.js

var express = require('express');
var path = require('path');
var app = express();
var port = process.env.PORT || 5000;

var delivCount  = require('./serv_delivered.js');
var serv_ontime = require('./serv_ontime.js');
var delayedCount = require('./serv_delayed.js');
var crtidelayedCount = require('./serv_crit_delayed.js');
var chartData = [{
        "country": "Czech",
        "litres": 301.9
    }, {
        "country": "Ireland",
        "litres": 201.1
    }, {
        "country": "Germany",
        "litres": 165.8
    }, {
        "country": "Pak",
        "litres": 139.9
    }, {
        "country": "Austria",
        "litres": 128.3
    }, {
        "country": "UK",
        "litres": 99
    }] ;

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/src/views');

var bookRouter= express.Router();
app.set('view engine','ejs');

app.get('/',function(req,res){
            delivCount(function(error, res_deliv){
                serv_ontime(function(error, res_ontime){
                    delayedCount(function(error, res_delayed){
                        crtidelayedCount(function(error, res_cdelayed){
                            res.render('index', {title: "Hellow From Render",
                                    res: [res_deliv,res_ontime,res_delayed,res_cdelayed,chartData]
                                });
                        });
                    });
                });
            });
    }); 
app.listen(port,function(err){
    console.log('running server on port'+ port);
}); 

amchart5.js amchart5.js

var chart = AmCharts.makeChart("pie1", {
    "type": "pie",
    "theme": "light",
    "dataProvider": [{
        "country": "Czech",
        "litres": 301.9
    }, {
        "country": "Ireland",
        "litres": 201.1
    }, {
        "country": "Germany",
        "litres": 165.8
    }, {
        "country": "Pak",
        "litres": 139.9
    }, {
        "country": "Austria",
        "litres": 128.3
    }, {
        "country": "UK",
        "litres": 99
    }],
    "valueField": "litres",
    "titleField": "country",
    "balloon": {
        "fixedPosition": true
    },
    "export": {
        "enabled": true
    }
});

Can anyone help me use the chartData that I am sending from app.js to create a pie chart? 谁能帮助我使用从app.js发送的chartData创建饼图?

Adding screenshot validating correct data - 添加屏幕截图以验证正确的数据-

数据

if your server is returning the correct values, and you're using a external js file, you could do something like this. 如果您的服务器返回正确的值,并且您使用的是外部js文件,则可以执行以下操作。

function createChart(chartData){
    var chart = AmCharts.makeChart("pie1", {
        "type": "pie",
        "theme": "light",
        "dataProvider": chartData,
        "valueField": "litres",
        "titleField": "country",
        "balloon": {
            "fixedPosition": true
        },
        "export": {
            "enabled": true
        }
    });
}

and then call the function from ejs file 然后从ejs文件中调用函数

<script>
    createChart(<%- res[4] %>);
</script>

as you declare your res as an array and your chart data is on the index 4, this should work. 当您将res声明为数组并且图表数据位于索引4上时,这应该可以工作。 ps: maybe you need to use JSON.stringify in your array and pay attention on the load order of scripts. ps:也许您需要在数组中使用JSON.stringify并注意脚本的加载顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM