首页 > 解决方案 > 无法将数组值输入图形

问题描述

我正在使用 Rgraph 来绘制折线图。我正在尝试使用 Javascript 将值输入图表。

这是例程..


        function update_day_temp(newval){
        var newval = '6,5,7,3,7,9,10';
        var dta = [];
        dta = newval.split(',');
        console.log('data ' + dta);        

        day_temp = new RGraph.Line({
            id: 'day_temp',
//          data: dta,
            data: [6,5,7,3,7,9,10],
            options: {
            }
        }).draw()
    };

如果我使用所使用的数据,一切都很好。但是,如果我使用 dta,那么我确实会得到结果。

标签: javascriptrgraph

解决方案


添加我之前的评论后,我一直在玩这个,你也可以这样做(已经满足了):

data: '6,5,7,3,7,9,10'.split(','),

这只是将字符串拆分为一个数组,然后通过 RGraph 将值转换为数字。

在 RGraph 的下一个版本(v5.27)中,您将不需要调用 split()。

这里有一个演示,它对数据调用 split() 函数:

https://www.rgraph.net/demos/bar-basic.html

其中的代码是这样的:

new RGraph.Bar({
    id: 'cvs',
    data: '12,18,10,9,6,20,18'.split(','),
    options: {
        yaxisScaleUnitsPost: 'k',
        colors: ['red'],
        title: 'A basic Bar chart using accessible text',
        titleBold: true,
        xaxis: false,
        yaxis: false,
        marginLeft: 50,
        tooltips: '%{key}',
        tooltipsFormattedUnitsPost: '%',
        tooltipsCss: {
            fontSize: '26pt'
        },
        tooltipsFormattedKeyLabels: ['Dave','John'],
        tooltipsEvent: 'mousemove'
    }
}).draw().responsive([
    {maxWidth:900,width:400,height:150,options: {textSize:10,xaxisLabels:['Mon\n(yuck!)','Tue','Wed','Thu','Fri\n(woo!)','Sat','Sun'],marginInner: 10}},
    {maxWidth:null,width:750,height:250,options: {textSize:14,xaxisLabels: ['Monday\n(yuck!)','Tuesday','Wednesday','Thursday','Friday\n(woo!)','Saturday','Sunday'],marginInner: 20}}
]);

推荐阅读