Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colored bar chart #105

Open
risinghero opened this issue Feb 23, 2016 · 2 comments
Open

Colored bar chart #105

risinghero opened this issue Feb 23, 2016 · 2 comments
Labels

Comments

@risinghero
Copy link

Is there any possibility to draw colored chart, i.e. to assign each bar in BarChart different color?

@dsyme dsyme added the question label Jun 7, 2017
@FoggyFinder
Copy link

As far I know there is no special method to do this. But GenericChart has function ApplyToChart. It allows you to apply whatever you want directly to inner chart.

So you can create simple function like this:

let difColors (colors:Color[]) (chart:Chart) = 
    let count = colors.Length
    for (series:Series) in chart.Series do
        if count = series.Points.Count then
            for i in 0..count - 1 do
                series.Points.[i].Color <- colors.[i]

and get colored chart

sample

Full code:

open System.Windows.Forms.DataVisualization.Charting
open System.Drawing

let countryData = 
    [ "Africa", 1033043; 
      "Asia", 4166741; 
      "Europe", 732759; 
      "South America", 588649; 
      "North America", 351659; 
      "Oceania", 35838  ]

let difColors (colors:Color[]) (chart:Chart) = 
    let count = colors.Length
    for (series:Series) in chart.Series do
        if count = series.Points.Count then
            for i in 0..count - 1 do
                series.Points.[i].Color <- colors.[i]

let colors = [| Color.Red; Color.Green; Color.Blue; Color.Orange; Color.DarkGray; Color.Black |]
(countryData |> Chart.Bar).ApplyToChart (difColors colors)

@FoggyFinder
Copy link

It was just a one way. You can apply palette or something if you don't want to enumerate colors manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants