[英]Google Charts - ScatterChart Tick Marks and Styling
我創建了一個ScatterChart來可視化不同數據字段之間的關系。 可視化數據本質上可以是數字或標稱/序數。 因此,名義/序數值被映射到數值。 一個簡單的例子是“年齡+性別”,其中性別是名義上的,我映射'男性'=> 1,'女性'=> 2以獲得所需的輸出。
到目前為止一直很好,圖表正在運行,但我需要幫助格式化它。
如您所見,第一個性別列顯示在y軸上,第二個性別列顯示在圖表的最右側。 我希望它們在y軸的某個空間中“漂亮”,在右端有一些空間。
另外,我想在y軸上顯示“男性”和“女性”的適當刻度線。
延期:
我還想將數據分成不同的系列,以便能夠着色例如不同顏色的“男性”和“女性”數據點。
我所做的是在我的數據表(年齡,男性,女性)中構建3列,但我無法正確輸出它。 目前,它們都合並為一個顯示的列。
到目前為止,這是我的代碼:
var drawMe = function(){
var columns = 0;
var xColumns = 0;
var yColumns = 0;
var gdata = new google.visualization.DataTable();
/**
* data Object:
* data.xName = Name of x parameter
* data.yName = Name of y parameter
* data.x = data for x paramter
* data.y = data for y parameter
* data.xType = type of x parameter, either num (= number) or other (= string)
* data.yType = type of y parameter, either num (= number) or other (= string)
* data.xChoices = array of strings representing availble choices for x if xType !== num
* daty.yChoices = array of strings representing availble choices for y if yType !== num
*
*/
if(data.xType === 'num'){
gdata.addColumn('number', data.xName);
xColumns++;
columns++;
} else {
for(var i = 0; i < data.xChoices.length; i++){
gdata.addColumn('number', data.xChoices[i]);
xColumns++;
columns++;
}
}
if(data.yType === 'num'){
gdata.addColumn('number', data.yName);
yColumns++;
columns++;
} else {
for(var i = 0; i < data.yChoices.length; i++){
gdata.addColumn('number', data.yChoices[i]);
columns++;
yColumns++;
}
}
var x;
var y;
for(var i = 0; i < count; i++){ // count is set by closure, cause data is paged via ajax
// initialize zero row
var row = [];
for(var j = 0; j < columns; j++){
row[j] = null;
}
if(data.xType === 'num'){
x = parseFloat(data.x[i]);
row[0] = x;
} else {
var index = data.xChoices.indexOf(data.x[i]);
x = {
v: index + 1, // don't start at 0
f: data.xChoices[index],
};
row[index] = x;
}
if(data.yType === 'num'){
y = parseFloat(data.y[i]);
row[xColumns] = y;
} else {
var index = data.yChoices.indexOf(data.y[i]);
y = {
v: index + 1, // don't start at 0
f: data.yChoices[index],
};
row[xColumns + index] = y;
}
gdata.addRow(row);
}
var xTitle = data.xName;
if(data.xUnit){
xTitle += ' [' + data.xUnit + ']';
}
var yTitle = data.yName;
if(data.yUnit){
yTitle += ' [' + data.yUnit + ']';
}
var xGridLines = -1;
var yGridLines = -1;
var xTicks = false;
var yTicks = false;
if(data.xType !== 'num' && data.xChoices){
xGridLines = data.xChoices.length + 2;
xTicks = [{v: 0, f: ''}]; // empty tick at the beginning
for(var i = 0; i < data.xChoices.length; i++){
xTicks.push({v: i+1, f: data.xChoices[i]});
}
xTicks.push({v: 3, f: ''}); // empty tick at the end
}
if(data.yType !== 'num' && data.yChoices){
yGridLines = data.yChoices.length + 2;
yTicks = [{v: 0, f: ''}];
for(var i = 0; i < data.yChoices.length; i++){
yTicks.push({v: i+1, f: data.yChoices[i]});
}
yTicks.push({v: 3, f: ''});
}
var options = {
title: data.xName + ' vs. ' + data.yName,
hAxis: {
title: xTitle,
gridlines: {
count: xGridLines
}
},
vAxis: {
title: yTitle,
gridlines: {
count: yGridLines
}
}
};
if(xTicks !== false){
options.hAxis.ticks = xTicks;
options.hAxis.viewWindowMode = 'pretty';
}
if(yTicks !== false){
options.vAxis.ticks = yTicks;
options.vAxis.viewWindowMode = 'pretty';
}
options.series = {};
for(var i = 0; i < columns; i++){
options.series[i] = {color: atk.COLORS[i], visibleInLegend: true};
}
var chart = new google.visualization.ScatterChart(element);
chart.draw(gdata, options);
};
謝謝你的幫助和時間!
您很幸運,RC版本(1.1)中的API有一個更新,應該能夠完全按照您的需要進行操作。 首先加載RC版本:
google.load('visualization', '1.1', {packages: ['corechart']});
然后像這樣設置hAxis.ticks選項:
hAxis: {
ticks: [{v: 0, f: ''}, {v: 1, f: 'Male'}, {v: 2, f: 'Female'}, {v: 3, f: ''}]
}
試試看,看看它是否有效。
編輯:
要使男性和女性的顏色不同,您需要將數據拆分為兩個單獨的列。 您可以使用DataView完成此操作:
// assumes Male/Female is column 0 and age is column 1 in your DataTable
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'Age',
calc: function (dt, row) {
// return a value only if male
return (dt.getValue(row, 0) == 1) ? dt.getValue(row, 1) : null;
}
}, {
type: 'number',
label: 'Age',
calc: function (dt, row) {
// return a value only if female
return (dt.getValue(row, 0) == 2) ? dt.getValue(row, 1) : null;
}
}]);
使用DataView而不是DataTable繪制圖表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.