プログラマでありたい

おっさんになっても、プログラマでありつづけたい

Google Chart APIで2次関数のグラフを描く

 グラフを書きたくて調べていたのですが、Google Chart APIはグラフの数値だけではなく数式を渡すことが出来るようです。これで二次関数を渡して、二次曲線を描くことが出来ます。知らなかったけど、便利そうです。


Line Charts - Google Chart Tools: Image Charts - Google Code

A simple sine wave. Some things to notice:

chd=t:-1 - We use a dummy variable for the chart data, because our data is declared in the chfd parameter.
chco=FF0000 - Red is specified for the first series. Even though we don't use the data from chd, the corresponding color for that series will be used for that plotted function.
chfd=0,x,0,11,0.1,sin(x)*50%2B50 - We declare one variable, called x, with values 0―11, incremented by 0.1. It is assigned to the first series, which means it will get the first series color (FF0000). The function applied to x is sin(x) * 50 + 50. Note how we must encode the + in that function.

こんな感じになります。
Sine wave specified by chfd

https://chart.googleapis.com/chart?cht=lc&chd=t:-1&chs=250x150&chco=FF0000&chfd=0,x,0,11,0.1,sin(x)*50%2b50&chxt=x,y


chfdの部分で関数の定義と値を渡しています。
もう少し調べてみて、まとめてみます。