我必須爲我的學校調查項目實施條形圖,並且我認爲我差不多完成了,但沒有什麼能完美實現,這是我的問題。我通過本教程http://www.rgraph.net/docs/integration-with-server-side-scripting.html#mysql實施了此代碼http://www.rgraph.net/docs/bar.html,但我遇到了一個問題。我需要根據它的高度來繪製圖形條,例如等於3的值是綠色的,小於1.5的值是紅色的。 代碼我使用看起來像這樣條形圖中的Rgraph條形顏色
print('<script src="../libraries/RGraph.bar.js"></script>' . "\n\n");
print('<canvas id="cvs1" width="600" height="200">[No canvas support]</canvas>' . "\n\n");
print('<script>' . "\n");
print('window.onload = function(){'."\n\n");
print('var bar = new RGraph.Bar('.cvs1.', ['.$data_string.'])' . "\n\n");
print('.Set(\'labels\',[\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'10\',\'11\',\'12\',\'13\',\'14\'])'. "\n\n");
print(' .Set(\'colors\', [\'Gradient(#94f776:#50B332:#B1E59F)\', \'Gradient(#94f776:#50B332:#B1E59F)\', \'Gradient(#f2a011:#f2a011:#f2a011:#f2a011)\'])'. "\n\n");
print('.Set(\'hmargin\', 15)'. "\n\n");
print('.Set(\'strokestyle\', \'white\')'. "\n\n");
print('.Set(\'linewidth\', 1)'. "\n\n");
print('.Set(\'shadow\', true)'. "\n\n");
print('.Set(\'shadow.color\', \'#ccc\')'. "\n\n");
print('.Set(\'shadow.offsetx\', 0)'. "\n\n");
print('.Set(\'shadow.offsety\', 0)'. "\n\n");
print('.Set(\'shadow.blur\', 10)'. "\n\n");
print('.Set(\'ymax\', 5)'. "\n\n");
print('.Set(\'colors.sequential\', true)'. "\n\n");
print('.Draw();'. "\n\n");
print('}'. "\n\n");
print('</script>'. "\n\n");
如果這一個 print('var bar = new RGraph.Bar('.cvs1.', ['.$data_string.'])' . "\n\n");
是從數據庫中獲取數據的代碼。我試圖寫簡單的PHP和甚至foreach循環打印顏色,但它沒有奏效。代碼看起來像這樣
foreach($data_string as $param)
if ($param<1){
return(' .Set(\'colors\', [\'Gradient(#FF0000:#FF0000:#FF0000)\'])'. "\n\n");
}
else if($param<=2){
return(' .Set(\'colors\', [\'Gradient(#f2a011:#f2a011:#f2a011:#f2a011)\'])'. "\n\n");
}
else
return(' .Set(\'colors\', [\'Gradient(#94f776:#50B332:#B1E59F)\'])'. "\n\n");
}
我相信爲foreach循環準備數據的代碼是好的,因爲我用它來顯示HTML表格中的數據顏色。請問有什麼解決