Search code examples
javascriptarraysrgraph

How to add a javascript array to data part of RGraph


I want to add array values as data and label part of RGraph. Now it is hard coded as shown below. I want to pass months of array (ChartData1) as "Label" to graph and remaining part as it's "data" part

 <script type="text/javascript">

  var ChartData1 = new Array();
  var ChartData1ValueX = new Array();

  function ShowChart() 
  {
  ChartData1.Add("June,100,200,300");
  ChartData1.Add("July,500,600,700");
  var totalBars1 = ChartData1.length;
   for (var i = 0; i < totalBars1; i++) 
   {
    var arrVal = ChartData1[i].split(",");
    ChartData1ValueX[i] = arrVal[0];
    }
      new RGraph.Bar({
                    id: 'cvs',
                    data: [ [100, 200, 300], [500, 600, 700]],
                    labels: [
                       'June', '',
                       'July', ''],
                    options: {
                        key: ['aa ', 'bb ', 'cc '],
                        keyPosition: 'gutter',
                        keyPositionY: 325,
                        gutterBottom: 340,
                        keyTextColor: '#ffffff',
                        unitsPost: '.00',
                        colors: ['#009900', '#005ce6', '#ff4000'],                       
                        title: 'Month',
                        titleYaxis: 'Total'

                    }
                }).grow({ frames: 60 });
            }

How to extract both labels and data from 'ChartData1' array?


Solution

  • It's just a case of manipulating the array and creating a couple of new ones that RGraph can use:

    <script>
        // The source data
        var source = new Array();
        source.push("June,100,200,300");
        source.push("July,500,600,700");
        source.push("August,500,600,700");
        source.push("September,300,800,200");
        source.push("October,300,300,500");
        source.push("November,600,800,300");
    
        // Array that we'll add the data and labels to
        var labels = new Array();
        var data   = new Array();
    
        function showChart()
        {
            // Loop thru the source data
            for (var i=0; i<source.length; i++) {
    
                var values = source[i].split(",");
    
                // Use the first value of the string that has just been split
                // into an array as the label
                labels.push(values[0]);
    
                // Use the remaining values as the data (to a stacked/grouped
                // chart for example
                var arr = [
                    parseFloat(values[1]),
                    parseFloat(values[2]),
                    parseFloat(values[3])
                ];
    
                // Add the array to the data that we'll pass to the
                // chart.
                data.push(arr);
            }
    
            // TODO Create chart here using the data and labels arrays
            console.log(data);
            console.log(labels);
        }
    
    
        showChart();
    </script>