开发者

Lazy High Charts: change colors of a pie chart [rails 3]

开发者 https://www.devze.com 2023-03-22 04:23 出处:网络
I\'m working in a rails 3 project, I want to change the pie chart colors that I generate with the LazyHighChart gem and I don;t know how to do that

I'm working in a rails 3 project, I want to change the pie chart colors that I generate with the LazyHighChart gem and I don;t know how to do that

This is my method controller

def set_pie_chart(data)
fixed_data = []
data.each_pair do |key, value|
fixed_data << [key.name, value]
end

  @color = data.keys.map {|e| "#" + e.colour }  # e.colour is like '333333'
  @chart = LazyHighCharts::HighChart.new('pie') do |c|
    c.chart({:defaultSeriesType=>"pie" , :margin=> [0, 0, 0, 0]})
    series = {
      type: 'pie',
      name: 'total expenses',
      data: fixed_data,
      colors: ['green','red'] # intent
    }
    c.series(series)
    c.colors = ['red','blue','black'] # intent
    c.options[:colors] = ['green','blue','yellow'] # intent
    c.options['colors'] = ['red','blue','yellow'] # intent
    c.options[:title][:text] = nil
    c.plot_options(:pie=>{
      cursor: "pointer",
      center: ['50%','37%'],
      color: 'red', #intent
      dataLabels: { enabled: false }
    })
  end
end

this method doesn't leave any error, what is 开发者_StackOverflowthe correct way or its not possible with this gem?

or what other good alternative gems could I use for my project?


I had the same problem and I solved it by putting the color options in my view. This is what I did:

#my_view_helper.rb
def chart_colors
  html =  
     "[{linearGradient: [0, 0, 0, 200], stops: [[0, '#e28b02'],[1, '#f1bc70']]},
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#a5ba57'],[1, '#a8bb51']]}, 
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#1e93d6'],[1, '#35aff6']]}, 
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#c8cf99'],[1, '#cdcfa9']]}, 
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#709ab1'],[1, '#a7c5d0']]}, 
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#c76f4e'],[1, '#fba98e']]},
     {linearGradient: [0, 0, 0, 200], stops: [[0, '#95d6e3'],[1, '#bbe8ed']]}]"
  html
 end

#my_view.html.haml
#chart
  = high_chart("pie_1", @chart) do |c| 
    = "options.colors = #{chart_colors}".html_safe


@chart = LazyHighCharts::HighChart.new('pie') do |c|
    c.colors(["red","green","blue"]);
end
0

精彩评论

暂无评论...
验证码 换一张
取 消