2016-04-26 3 views
1

私は、ユーザーが州全体の平均と比較するオプションを選択できるようにするチャートを用意しています。 stringifyメソッドを使用して注釈を作成するまでは、グラフは正常に機能します。列の数は選択に応じて異なります。この機能を保持する方法についてのアドバイスはありますが、まだデータラベルがありますか?ここで設定した列のないGoogleグラフにデータラベルを追加する

<html> 
<head> 
<script type='text/javascript' src='https://www.google.com/jsapi'></script> 
<script type='text/javascript'> 
google.load('visualization', '1', {packages:['table', 'corechart']}); 
</script> 

<script type='text/javascript'> 
google.setOnLoadCallback(initialize); 
function initialize() {drawVisualizations(); 
function drawVisualizations() {drawChart(); drawTable();} //drawC();} 

//main chart 
function drawChart() { 
var dataCap = document.getElementById("selected2").value; 
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A1:A5,E1:E5,' + dataCap); query.send(handleQueryResponse); 
} 

function handleQueryResponse(response) { 
    var data = response.getDataTable(); 
    var options = { 
    'title':'College Readiness', 
    'subTitle':'Test', 
    'titleTextStyle': {fontSize: '24', color: 'teal', fontName: '"Arial"', isHTML: true}, 
    hAxis: {'title': 'Percent of Students Ready', 'textStyle': {color: 'gray', fontSize: '9'}, 'minValue': '0', 'maxValue':'1','format': 'percent'}, 
    vAxis: {'title': 'Academic Year', 'textStyle': {bold: true, fontSize: '16'}, 'minValue': '0', 'maxValue':'1'}, 
    legend: {'position': 'top', 'maxLines': 5, 'textStyle': {bold: true, fontSize: '16', fontName: "'Arial'"}}, 
    tooltip: { 
    textStyle: {fontName: "'Arial'"}}, 
     series: { 
     0: {pointsVisible: true, color: '#003366'}, 
     1: {pointsVisible: true, color: '#cc0000'} 
    }, 
    annotations: { 
    textStyle: {bold: true,color: '#000000', fontName: "'Arial'"}, 
    stem:{color: 'none'} 
    }}; 


function handleQueryResponse(response) {var data = response.getDataTable()}; 
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));chart.draw(data, options);} 

//current stats sidebar 
function drawTable() { 
var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Sheet2&range=A:E'); 
    query2.send(handleQueryResponse2); 
} 

function handleQueryResponse2(response) { 
var data2 = response.getDataTable(); 
var options2 = {'sort': 'disable'}; 
var chart2 = new google.visualization.Table(document.getElementById('data_div')); 
    chart2.draw(data2, options2);} 
} 

</script> 

<style type="text/css"> 
body {font-family: Arial, Helvetica, sans-serif;} 
html, body {height: 100%;} 
body {text-align: center;} 
#chart_div {width: 900px; margin: 20px auto; height: 600px;} 
</style> 
</head> 
<body> 
<label for="selected2">You are viewing:</label> 
<select id="selected2" onChange="initialize()"> 
     <option selected value="">Statewide average only</option> 
     <option value="B1:B5">Fake University 1 compared to statewide average</option> 
     <option value="C1:C5">Fake University 2 compared to statewide average</option> 
     <option value="D1:D5">Fake University 3 compared to statewide average</option> 
    </select> 
<input onClick="window.open('datatest21-data.html')" type="button" value="Download the complete data set" /> 
<div id="chart_div"></div> 
<div id="data_div"></div> 
</body> 
</html> 

は私が使っていた文字列化する方法であるが、それは上記のコードを台無しに:

// 
var view = new google.visualization.DataView(data); 
view.setColumns([0, 1, { calc: "stringify", 
sourceColumn: 1, 
type: "string", 
role: "annotation"}, 
2,{ calc: "stringify", 
sourceColumn: 2, 
type: "string", 
role: "annotation" }]); 
// 

それは私がこのすべて間違ってやっているかもしれません - 助言、感謝を。このタイプのコーディングは私には新しいものです。

+0

アノテーションを必要とする列を知っていますか? 'data.getNumberOfColumns()'に基づいて 'view.setColumns'を動的に構築することができます... – WhiteHat

+0

私はあなたが言及した方法を使用しているが、対話性を失ってしまっています。以下の記事を参照してください。 – commcordinator

+0

初期化コードに問題があります。私がdrawChart関数を取り出すと、それは機能します。ご協力いただきありがとうございます。 – commcordinator

答えて

0

設定済みの列を使用していますが、値を変更するためのドロップダウンが機能しなくなりました。私の推測では、グラフがDataTableではなくDataViewに移動すると、グラフは「読み取り専用」になり、対話性が失われることになります。 (この例では別のグラフを使用しています)。別のオプションが選択されている場合は、テーブルを再描画する方法が必要です。 [コード]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 


<script type='text/javascript' src='https://www.google.com/jsapi'></script> 
<script type='text/javascript'> 
google.load('visualization', '1', {packages:['table', 'corechart']}); 
</script> 

<script type='text/javascript'> 
google.setOnLoadCallback(initialize); 
function initialize() {drawVisualizations(); function drawVisualizations() {drawChart(); drawTable();} //drawC();} 

//main chart 
function drawChart() { 
var dataCap = document.getElementById("selected2").value; 
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?sheet=Enrollment&range=' + dataCap); query.send(handleQueryResponse); 
} 

function handleQueryResponse(response) { 
var data = response.getDataTable(); 
    // 
var view = new google.visualization.DataView(data); 
view.setColumns([0,1,2,3, 
         {calc: "stringify", 
         sourceColumn: 4, 
         type: "string", 
         role: "annotation"} 
         ]); 
        // 
    var options = { 
    height: 500, 
    chartArea: {'left': '5%'}, 
    isStacked: true, 
    hAxis: {'textStyle': {'fontSize':11}}, 
    vAxis: {'textStyle': {'fontSize':11}, 'maxValue':110000}, 
    legend: {'position': 'top', 'maxLines': 5, 'textStyle': {bold: true, fontSize: 11, fontName: '"Arial"'}}, 
    tooltip: { 
    textStyle: {fontName: "'Arial'"}}, 
     series: { 
     0: {color: 'navy'}, 
     1: {color: 'teal'} 
    }, 
    annotations: { 
    textStyle: {bold: true,color: '#000000', fontName: "'Arial'"}, 
    alwaysOutside: true, 
    isHTML: true, 
    stemColor: 'none', 
    text: 'test' 
    }}; 

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));chart.draw(view, options);} 


//current stats sidebar 
function drawTable() { 
var dataCap2 = document.getElementById("selected3").value; 
var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1m3ujxzPQJh3haReNDzGGF73Mh6-u6HxyCVPK_5MK2hw/gviz/tq?' + dataCap2); 
    query2.send(handleQueryResponse2); 
} 

function handleQueryResponse2(response) { 
var data2 = response.getDataTable(); 
var options2 = { 
    height: 300, 
    chartArea: {'left': '5%'}, 
    hAxis: {'textStyle': {'fontSize':11}}, 
    vAxis: {'minValue': '0', 'maxValue':'1', 'format':'percent', 'textStyle': {'fontSize':11}}, 
    legend: {'position': 'top', 'maxLines': 5, 'textStyle': {bold: true, fontSize: 11, fontName: '"Arial"'}}, 
    tooltip: { 
    textStyle: {fontName: "'Arial'"}}, 
     series: { 
     //0: {pointsVisible: true, color: '#003366', pointSize: 6}, 
     0: {pointsVisible: true, color: '#cc0000', pointSize: 6} 
    }, 
    annotations: { 
    textStyle: {bold: true,color: '#000000', fontName: "'Arial'"}, 
    stem:{color: 'none'} 
    } 
    }; 
var chart2 = new google.visualization.LineChart(document.getElementById('data_div')); 
    chart2.draw(data2, options2);} 
} 

</script> 

<style type="text/css"> 
body {text-align: center; font-family: Arial, Helvetica, sans-serif;} 
#wrapper {width: 75%; margin: 0 auto; text-align: left;} 
#current {float: right; width: 28%; background-color: #ececec; padding: 0 1em; font-size: 80%; margin-left: 5%;} 
h2 {margin-top: 2em;} 
dt {font-weight: bold; margin-top: .5em;} 
dd {padding-top: .25em; margin-top: .25em;} 
dd.total {border-top: 1px solid #000;} 
#chart_div, #data_div {width: 75%;} 
</style> 
</head> 

<body> 
<div id="wrapper"> 


<h1>Student Enrollment in State Universities and Community Colleges</h1> 


<h3>Trend Data</h3> 
<form> 
<label for="selected2">You are viewing:</label> 
<select id="selected2" onchange="initialize()"> 
     <option selected value="A1:E8">All state universities</option> 
     <option value="F1:I8">Fake University 1</option> 
</select> 
<div id="chart_div"></div> 
<input onClick="window.open('')" type="button" value="Download the complete data set" /> 
</form> 

<h2>Community and Technical Colleges</h2> 

<h3>Trend Data</h3> 
<form> 
<label for="selected3">You are viewing:</label> 
<select id="selected3" onChange="initialize()"> 
<option selected value="sheet=GraduationRates-3Yr&range=A1:B11">3-Year graduation rate</option> 
<option value="sheet=GraduationRates-2Yr&range=A1:B11">2-Year graduation rate</option> 
</select> 
<div id="data_div"></div> 
<input onClick="window.open('')" type="button" value="Download the complete data set" /> 
</form> 

</div> 
</body> 
</html> 

[/コード]

関連する問題