gpt4 book ai didi

f# - F#制图示例

转载 作者:行者123 更新时间:2023-12-04 13:29:54 25 4
gpt4 key购买 nike

我想使用内置功能或免费库在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()

关于f# - F#制图示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1812174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com