Fsharpchart is a type of F # Friendly Packaging of system. Windows. Forms. datavisualization. charting to enable interaction of data in F # interactive. The latest version of fsharpchart is fsharpchart0.6, which is provided by Microsoft's Carl Nolan's. In the chart controls section of msdn, you can view more information about fchart controls. Of course, for more information about fsharpchart, you can also visit the blog of Carl Nolan's in person.
The latest version provides the following new features:
- Save clipboard as a function
- Attribute change event
- Supports 3D charts
- Data naming of boxplot charts
Before that, create an Application Project F # And add references to the following dll:
- System. Windows. Forms. dll
- System. Drawing. dll
- System. Windows. Forms. datavisualization. dll
- Windowsformsintegration. dll
Since I want to use Windows Presentation Foundation (WPF) to host charts, I need to reference the following DLL (my other article has a detailed process description ):
- System. Core. dll
- System. XAML. DL
- Presentationcore. dll
- Presentationframework. dll
Fsharpchart is essentially a winform control, so it must be carried by windowsformshost in WPF.
I. Basic functions
Fsharpchart supports system. Windows. Forms. datavisualization. Charting naming controls for all your types. You can use the static method of the fsharpchart type to create all chart types and then intelligently perceive browsing, as shown in:
These methods make it easier to display data on charts.
For example:
let line = let line = [for x in 0.0 .. 0.2 .. 10.0 –> sin x ]|> FSharpChart.Line new ChartControl(line)
Wrap it in shape as a chartcontrol control:
Each chart control has a context menu. You can easily copy the chart to the clipboard, save it as a file, and view (or modify) The detailed chart attributes.
The combine method can be used to draw two curves in One chart (when combining other graphs ):
let lines = let line1 = [for x in 0.0 .. 0.02 .. 10.0 -> sin x ]|> FSharpChart.Line let line2 = [for x in 0.0 .. 0.02 .. 10.0 -> cos x ]|> FSharpChart.Line new ChartControl( FSharpChart.Combine [line1;line2] )
For example:
The reload version of the chart creation method. The running data is specified by different methods. Basically, most charts allow you to specify the chart data source through the following mechanism:
- A queue containing only the Y axis data (the data on the X axis is automatically specified from 1, just like the method of creating the chart above)
- Sequence)
- (X, y) vector queue
- Y axis or (x, y) system. iobservable type, allows you to update the chart in time when adding data
In addition, there is no limit that the X or Y-axis data must be a floating point. Only the system. iconvertible. iconvertible interface type is required for this data. This means that it is easy to create a chart named by category:
let pie = let c = [(“A”,1); (“B”,3); (“C”,2)] |> FSharpChart.Pie new ChartControl(c,Dock=DockStyle.Fill)
Of course, it is very easy to create a plot chart by time or date:
let column = let r = Random() let c m= [for d in -30 .. m –>; (DateTime.Today.AddDays(float d) , r.Next(0 , 10))] |> FSharpChart.Spline let c = FSharpChart.Combine [c 10; c 12] new ChartControl(c)
2. New Features
1. Copy to clipboard and save other functions
In the latest version, you can copy an image to the clipboard and save the chart as an image. You can copy the image to the clipboard and enhanced Windows Metafile format.
You can also save images in different formats:
Support for code programming is also added for copying images and saving them as files. Copy the chart to the clipboard. You only need to call the copytoclipboard method:
[ for f in 1.0 .. 0.1 .. 10.0 -> sin (f * 2.0) + cos f ] |> FSharpChart.Line |> FSharpChart.WithCreate |> FSharpChart.CopyToClipboard
Similarly, calling save is similar to copytoclipboard.
2, property changed event
One improvement in the new version is that event events are supported when some genericchart attributes change, including name, title, margin, legend, and all common attributes. These events are also used inside the code to pass the changes of genericchart to the bound chart.
This function supports the following code:
chart.Margin <- (2.0f, 12.0f, 2.0f, 2.0f) chart.Title <- Helper.CreateTitle(“Chart Sin/Cosine”, Font = new Font(“Arial”, 12.0f, FontStyle.Bold)) chart.Legend <- Helper.CreateLegend(InsideArea = false, Font = new Font(“Arial”, 8.0f), Alignment = StringAlignment.Center, Docking = Docking.Top)
3, 3D chart
As you know, Windows Forms charting supports 3D rendering and now supports the genericchart 3D attributes. As an example, the piechart pie chart enables 3D effects:
let chart = let c = [(“A”,1); (“B”,3); (“C”,2)] |> FSharpChart.Pie |> FSharpChart.WithArea.Area3DStyle(Enable3D=true,Inclination=70) new ChartControl(c,Dock=DockStyle.Fill)
This feature supports all features, including: inclination, isclustered, isrightangleaxes, lightstyle, perspective, pointdepth, pointgapdepth, rotation, and wallwidth. To use 3 dchart, you only need to set
"Enable3d = true.
4, boxplot data series
As you are familiar with, boxplot charts are a good way to display several series of data. However, in the previous implementation, when presenting several series of data, each axis is always marked as 1, 2 by default, 3. As expected, this new version supports custom tags for each data series. Therefore, you can use the following code to create a boxplot chart:
let boxplot = let plot = FSharpChart.BoxPlot ( [ "Eggs", [| for i in 0 .. 20 -> float (rnd.Next 20) |] "Bacon", [| for i in 0 .. 20 -> float (rnd.Next 15 + 2) |] "Sausage";, [| for i in 0 .. 20 -> float (rnd.Next 5 + 5) |] "Beans";, [| for i in 0 .. 20 –> float (rnd.Next 10 + 3) |] "Mushroom";, [| for i in 0 .. 20 –> float (rnd.Next 15 + 5) |] ], Name = "Breakfast Food BoxPlot") |> FSharpChart.WithMargin(1.0f, 5.0f, 1.0f, 1.0f) |> FSharpChart.WithSeries.Style(Color = Color.SeaGreen) |> FSharpChart.WithLegend(InsideArea = false, Font = new Font(“Arial”, 8.0f), Alignment = StringAlignment.Center, Docking = Docking.Top) new ChartControl (plot)
,
5, Stacked chart
In earlier fsharpchart versions, creating a stacked chart is not intuitive. One thing you have to do is define a combinedchart and combine multiple charts with the same stackedgroupname attribute values:
let rnd = new System.Random ()let datax() = [ for f in 1 .. 10 –> rnd.NextDouble( ) * 10.0]let starckbar = let bar = FSharpChart.Combine [ FSharpChart.StackedBar100(datax(),StackedGroupName=”g1”) FSharpChart.StackedBar100(datax(),StackedGroupName=”g1”) FSharpChart.StackedBar100(datax(),StackedGroupName=”g1”) ] new ChartControl(bar)
This method also allows you to easily combine multiple stacked charts by using different stackedgroupname attribute values:
FSharpChart.Combine [ FSharpChart.StackedBar(dataX(), StackedGroupName = “g1”) FSharpChart.StackedBar(dataX(), StackedGroupName = “g1”) FSharpChart.StackedBar(dataX(), StackedGroupName = “g1”) FSharpChart.StackedBar(dataX(), StackedGroupName = “g2”) FSharpChart.StackedBar(dataX(), StackedGroupName = “g2”) ]
Of course, for stacked charts, a more intuitive method is to specify only a list of data columns. In this new version, the following format can be used:
List <list <'ty>
List <list <'tx * 'ty>
This definition is now allowed, such as the next stackedcolumn chart, using a much simpler expression:
let rndStacked = new System.Random()let dataXY() = [ for f in 1 .. 10 –> (f, round (rndStacked.NextDouble() * 100.0))][dataXY(); dataXY(); dataXY()]|> FSharpChart.StackedColumn|> FSharpChart.WithSeries.DataPoint(Label=”#VAL”)
6. Add a tag to the data column.
As shown in the following code, you can add a tag point to the data column.
let markLine = let rnd = new System.Random() let line = [for i in 1..20..1000 -> i , rnd.NextDouble()] |> FSharpChart.Line |> FSharpChart.WithSeries.Marker(Color = Color.Red ,Style=MarkerStyle.Circle) new ChartControl(line)
You can use the fsharpchart. witharea. axisy or fsharpchart. witharea. axisx function to control the display of horizontal and vertical coordinates. Set this title to use the fsharpchart. withtitle function. As follows:
let markLine = let rnd = new System.Random() let line = [for i in 1..20..1000 –> i , rnd.NextDouble()] |> FSharpChart.Line |> FSharpChart.WithTitle(“Hello”,TextStyle=TextStyle.Frame,Font = new Font(“Arial”, 28.0f)) |> FSharpChart.WithSeries.Marker(Color = Color.Red ,Style=MarkerStyle.Circle) |>FSharpChart.WithArea.AxisX(MajorGrid = Grid(LineColor = Color.White)) new ChartControl(line)
The last code segment provides a more complete point (of course, as described at the beginning of this article to add the corresponding DLL reference) is as follows:
open System open System.Windowsopen System.Windows.Controlsopen System.Windows.Forms.Integrationopen System.Windows.Forms.DataVisualization.Chartingopen System.Windows.Formsopen MSDN.FSharp.Chartingopen System.Drawing // for Colorlet markLine = let rnd = new System.Random() let line = [for i in 1..20..1000 -> i , rnd.NextDouble()] |> FSharpChart.Line |> FSharpChart.WithTitle("Hello",TextStyle=TextStyle.Frame,Font = new Font("Arial", 28.0f)) |> FSharpChart.WithSeries.Marker(Color = Color.Red ,Style=MarkerStyle.Circle) |>FSharpChart.WithArea.AxisX(MajorGrid = Grid(LineColor = Color.White)) new ChartControl(line)let mainNoXaml() = let formsHost =new WindowsFormsHost(Child = markLine)//(Child=starckbar) let window = Window() window.Content <- formsHost let app =new System.Windows.Application() in app.Run(window) |> ignore[<STAThread>]do mainNoXaml()
Summary: thanks to the hard work of Carl Nolan's. For more information, see the blog of Carl Nolans.
Download more Sample Code related to this article: demo.rar