Google图表:将系列动态添加到图表

时间:2019-05-25 22:54:33

标签: charts google-visualization

有一个similar thread here,但答案不明确,也没有明显的例子可言。

我需要将系列动态添加到Google图表图形中。假设每次连续单击按钮应从我的数组中添加一个新系列。现在,它只是替换它。我该怎么办?

// Init
google.charts.load('current', {'packages':['corechart']});

var currentclick = 0;

// My Data
var titles = ['Year1','Year2','Year3','Year4'];
var nums = [ [44,12,33,22], [33,11,7,8], [2,1,65,44] ];

$('#addSeries').click(function() {

   if (currentclick < 3) {
       draw(nums, currentclick);
   }
   currentclick++;
});

function draw(arr, seriesnum) {
 	var chartData = new google.visualization.DataTable();
	chartData.addColumn('string', 'Year');
	chartData.addColumn('number', 'Value');  
  
  var chartRowArray = $.makeArray();
	for (var i = 0; i < 4; i++) {
		chartRowArray.push( [ titles[i], arr[seriesnum][i] ] );
	}	
	chartData.addRows(chartRowArray);
  var chart = new google.visualization.LineChart(document.getElementById('chartarea'));
  chart.draw(chartData, null);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="addSeries">Add Next Series</button>

<div id="chartarea">
</div>

2 个答案:

答案 0 :(得分:1)

  • 您想在每次单击“添加下一个系列”按钮时从nums的数据中添加一个折线图。

如果我的理解是正确的,那么该修改如何?我认为您的情况有几个答案。因此,请考虑这只是几个答案之一。

修改点:

  • 在此修改中,chartRowArray被声明为全局变量。单击该按钮后,数据将添加到chartRowArray。这样,将行字符添加到现有图表中。

修改后的脚本:

// Init
google.charts.load('current', {'packages':['corechart']});
var currentclick = 0;

// My Data
var titles = ['Year1','Year2','Year3','Year4'];
var nums = [ [44,12,33,22], [33,11,7,8], [2,1,65,44] ];

$('#addSeries').click(function() {
  if (currentclick < 3) {
    draw(nums, currentclick);
  }
  currentclick++;
});

// Below script was modified.
var chartRowArray = $.makeArray(); // Added

function draw(arr, seriesnum) {
  var chartData = new google.visualization.DataTable();
  chartData.addColumn('string', 'Year');
  for (var i = 0; i < seriesnum + 1; i++) { // Added
    chartData.addColumn('number', 'Value');
  }
  if (seriesnum === 0) { // Added
    for (var i = 0; i < 4; i++) {
      chartRowArray.push( [ titles[i], arr[seriesnum][i] ] );
    }	
  } else { // Added
    for (var i = 0; i < 4; i++) {
      chartRowArray[i].push(arr[seriesnum][i]);
    }	
  }
  chartData.addRows(chartRowArray);
  var chart = new google.visualization.LineChart(document.getElementById('chartarea'));
  chart.draw(chartData, null);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="addSeries">Add Next Series</button>
<div id="chartarea"></div>

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

答案 1 :(得分:1)

关键是数据的最终结构。

列数组应为

0: Year
1: Value
2: Value
etc.

将Column数组以chartData.addColumn(type, name)的形式加1。

数据的行数组应为

0:  
   0: Year1
   1: 44
   2: 33
1:
   0: Year2
   1: 12
   2: 11
etc.

行数组以chartData.addRows(rowArray)的形式添加。

知道这一点后,我制作了 RowArray / ColArray 全局变量,这些全局变量可以随时修改(感谢 Tainake 的想法)以及数组处于初始状态时的初始条件。空,我是第一次构造或初始化它们。

下面的工作示例。再次感谢您的帮助!

// Init
google.charts.load('current', {'packages':['corechart']});

var currentclick = 0;

// My Data
var titles = ['Year1','Year2','Year3','Year4'];
var nums = [ [44,12,33,22], [33,11,7,8], [2,1,65,44] ];
var chartColArray = $.makeArray(); // Global var
var chartRowArray = $.makeArray(); // Global var

$('#addSeries').click(function() {

   if (currentclick < 3) {
       draw(nums, currentclick);
   }
   currentclick++;
});

function draw(arr, seriesnum) {
	var chartData = new google.visualization.DataTable();
	
	// For initial Column, push 'Year'; otherwise, push 'Value'
	if (chartColArray.length == 0) {
		chartColArray.push('Year');
	}
	chartColArray.push('Value');
	// addColumn() has to be 1-by-1-by-1, there is no addColumns(colarray)
	$.each(chartColArray, function(index, item) {
		(index == 0 ? chartData.addColumn('string', item) : chartData.addColumn('number', item));
	});
	
	for (var i = 0; i < 4; i++) {
        // For initial Row subarray, create subarray and push 'Year Series'; 
        // otherwise, push actual 'Series' value
		if (chartRowArray[i] == undefined) {
			chartRowArray[i] = $.makeArray();
			chartRowArray[i].push(titles[seriesnum]);
		}
		chartRowArray[i].push(nums[seriesnum][i]);
	}
	chartData.addRows(chartRowArray);

	// Instantiate and draw the chart.
	var chart = new google.visualization.LineChart(document.getElementById('chartarea'));
	chart.draw(chartData, null);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="addSeries">Add Next Series</button>

<div id="chartarea">
</div>