Skip to content
Advertisement

Cannot enter array values into graph

I am using Rgraph to general a line chart. I am trying to enter values into the chart with Javascript.

here is the routine..

        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()
    };

if I use the data as used all is fine. But if I use dta then I do get get a result.

Advertisement

Answer

After adding my previous comment I’ve been playing with this and you can also do this (which was already catered for):

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

This just splits the string into an array and the values are then converted to numbers by RGraph.

In the next version of RGraph (v5.27) you won’t need the call to split().

There’s a demo here which calls the split() function on the data:

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

The code of which is this:

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:['Monn(yuck!)','Tue','Wed','Thu','Frin(woo!)','Sat','Sun'],marginInner: 10}},
    {maxWidth:null,width:750,height:250,options: {textSize:14,xaxisLabels: ['Mondayn(yuck!)','Tuesday','Wednesday','Thursday','Fridayn(woo!)','Saturday','Sunday'],marginInner: 20}}
]);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement