Search code examples
f#fsharpchartf#-charting

How to set the quality of grids for xaxis grid in Fsharp.Charting?


I use Yahoo Finance data to compare two company's close price:

#load "C:\Users\Nick\Documents\Visual Studio 2013\packages\FSharp.Charting.0.90.9\FSharp.Charting.fsx"
open FSharp.Data
open FSharp.Charting
open System
open System.Drawing
open System.Windows.Forms.DataVisualization.Charting

type Stocks = CsvProvider<"http://ichart.finance.yahoo.com/table.csv?s=FB">

let plotprice nasdaqcode =
    let url = "http://ichart.finance.yahoo.com/table.csv?s=" + nasdaqcode
    let company = Stocks.Load(url)
    let companyPrices = 
        [ for r in company.Rows do
            if r.Date > DateTime(2010, 1, 1) (* && r.Date < DateTime(2015, 1, 1) *) then
        yield r.Date, r.Close ]

    Chart.Line(companyPrices,Name=nasdaqcode) 
    |> Chart.WithLegend(Enabled=true,Docking=ChartTypes.Docking.Bottom, InsideArea=false)

Chart.Combine ([plotprice "VLKPY";plotprice "TM"])
|> Chart.WithXAxis(LabelStyle = ChartTypes.LabelStyle(Angle = -45),MajorGrid=ChartTypes.Grid(Enabled=true, LineColor=Color.LightGray))
|> Chart.WithYAxis(MajorGrid=ChartTypes.Grid(Enabled=true, LineColor=Color.LightGray))

And here is what I got:

VM and Toyota Close price

Question:

As the picutre shows, there're only two grid lines (year 2012/1/1 and 2014/1/1). I would like to have a finer grid on the X axis: one line for each year from 2010 to 2015. Is it possible to setup the grid this way?


Solution

  • You can use the Interval parameter of the Grid type:

    Chart.Combine ([plotprice "VLKPY";plotprice "TM"])
    |> Chart.WithXAxis
        ( MajorGrid = ChartTypes.Grid( Enabled=true, 
                                       LineColor=Color.LightGray, 
                                       Interval=365.0 ) )
    

    There is also IntervalOffsetType parameter that you could set to DateTimeIntervalType.Years, but this does not seem to be working for me - so it looks like F# Charting currently only works on days.

    It would be great if you could verify this, submit an issue or perhaps even look into contributing to F# Charting to get this fixed :-)