如何将 javascript 数组添加到 RGraph 的数据部分
How to add a javascript array to data part of RGraph
我想添加数组值作为 RGraph 的数据和标签部分。现在它是硬编码的,如下所示。我想将几个月的数组 (ChartData1) 作为 "Label" 传递给图表,其余部分作为 "data" 部分
<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' 数组中提取标签和数据?
您可以创建一个数组变量并推送月份列表并替换图表数据中的变量
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 });
}
这只是一个操作数组并创建几个 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>
我想添加数组值作为 RGraph 的数据和标签部分。现在它是硬编码的,如下所示。我想将几个月的数组 (ChartData1) 作为 "Label" 传递给图表,其余部分作为 "data" 部分
<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' 数组中提取标签和数据?
您可以创建一个数组变量并推送月份列表并替换图表数据中的变量
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 });
}
这只是一个操作数组并创建几个 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>