我想使用内置功能或免费库在 F# 中做一些基本的图表。
我会对一个非常基本的例子感到非常非常满意,如果可能的话,饼图。
示例数据:
[("John",34);("Sara",30);("Will",20);("Maria",16)]
其中整数是饼图中要表示的百分比。
我最近安装了 VSLab,虽然我找到了很多 3D 示例,但我只是在寻找一个简单的饼图......
顺便说一句,使用 Excel 功能也很好,不是免费的,但还是安装了。
这是我使用 Google Chart API 拼凑而成的东西,
我希望代码足够清楚,无需进一步解释:
//Built with F# 1.9.7.8
open System.IO
open System.Net
open Microsoft.FSharp.Control.WebExtensions
//Add references for the namespaces below if you're not running this code in F# interactive
open System.Drawing
open System.Windows.Forms
let buildGoogleChartUri input =
let chartWidth, chartHeight = 250,100
let labels,data = List.unzip input
let dataString =
data
|> List.map (box>>string) //in this way, data can be anything for which ToString() turns it into a number
|> List.toArray |> String.concat ","
let labelString = labels |> List.toArray |> String.concat "|"
sprintf "http://chart.apis.google.com/chart?chs=%dx%d&chd=t:%s&cht=p3&chl=%s"
chartWidth chartHeight dataString labelString
//Bake a bitmap from the google chart URI
let buildGoogleChart myData =
async {
let req = HttpWebRequest.Create(buildGoogleChartUri myData)
let! response = req.AsyncGetResponse()
return new Bitmap(response.GetResponseStream())
} |> Async.RunSynchronously
//invokes the google chart api to build a chart from the data, and presents the image in a form
//Excuse the sloppy winforms code
let test() =
let myData = [("John",34);("Sara",30);("Will",20);("Maria",16)]
let image = buildGoogleChart myData
let frm = new Form()
frm.BackgroundImage <- image
frm.BackgroundImageLayout <- ImageLayout.Center
frm.ClientSize <- image.Size
frm.Show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)