F#图表示例 [英] F# charting example

查看:175
本文介绍了F#图表示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在F#中使用内置功能或免费库来做一些基本的图表。
我会非常高兴的一个非常基本的例子,如果可能的饼图。



范例资料:

  [(John,34) ;(Sara,30);(Will,20);(Maria,16)] 


$ b b

其中int是要在饼图中表示的百分比。



我最近安装了VSLab,虽然我发现很多3D示例,我只是寻找一个简单的饼图...


解决方案

p>这里是我使用Google Chart API捣碎的东西,
我希望代码很清楚,没有进一步的解释:

  // Built with F#1.9.7.8 
open System.IO
open System.Net
open Microsoft.FSharp.Control.WebExtensions
//为下面的命名空间添加引用如果你不是在F#交互中运行这个代码
打开System.Drawing
打开System.Windows.Forms

让buildGoogleChartUri input =
让chartWidth,chartHeight = 250,100

let labels,data = List.unzip input
let dataString =
data
|> List.map(box>> string)//以这种方式,数据可以是任何ToString()将其转换为数字
|> List.toArray |> String.concat,

let labelString = labels |> List.toArray |> String.concat|

sprintfhttp://chart.apis.google.com/chart?chs=%dx%d&chd=t:%s&cht=p3&chl=%s
chartWidth chartHeight dataString labelString

//从google图表URI中烘焙一个位图
let buildGoogleChart myData =
async {
let req = HttpWebRequest.Create(buildGoogleChartUri myData )
let! response = req.AsyncGetResponse()
return new Bitmap(response.GetResponseStream())
} |> Async.RunSynchronously

//调用google chart api从数据中构建一个图表,并以一种形式显示图像
//引用斜坡的winforms代码
let test ()=
let myData = [(John,34);(Will,20);(Maria,16)]
$ b b let image = buildGoogleChart myData
let frm = new Form()
frm.BackgroundImage< - image
frm.BackgroundImageLayout< - ImageLayout.Center
frm.ClientSize< - image.Size
frm.Show()


I would like to do some basic charting in F# using build in features or a free library. And I would be very very pleased with a very basic example of it, a pie chart if possible.

Example data :

[("John",34);("Sara",30);("Will",20);("Maria",16)] 

Where the ints are percentages to be represented in the pie.

I have recently installed VSLab and though I find a lot of 3D examples, I am only looking for a simple pie chart...

It is also fine to use excel features by the way, not free, but installed nevertheless..

解决方案

Here's something I smashed together using the Google Chart API, I hope the code is clear enough without further explanation:

//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#图表示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆