0

我想将数组值添加为 RGraph 的数据和标签部分。现在它是硬编码的,如下所示。我想将几个月的数组(ChartData1)作为“标签”传递给图形,其余部分作为“数据”部分

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

如何从“ChartData1”数组中提取标签和数据?

4

2 回答 2

0

您可以创建一个数组变量并推送月份列表并在图表的数据中替换该变量

 var months = [];
    months.push("January");
    months.push("February");

     new RGraph.Bar({
                        id: 'cvs',
                        data: [ [100, 200, 300], [500, 600, 700]],
                        labels: months,
 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 });
            }
于 2017-05-23T12:21:04.490 回答
0

这只是操作数组并创建 RGraph 可以使用的几个新数组的一个例子:

<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>
于 2017-05-23T15:00:41.740 回答