2

我正在从事 rails 3 项目,我想更改使用 LazyHighChart gem 生成的饼图颜色,但我不知道该怎么做

这是我的方法控制器

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

这种方法不会留下任何错误,正确的方法是什么,或者这个宝石不可能?

或者我可以为我的项目使用哪些其他好的替代宝石?

4

2 回答 2

2

我遇到了同样的问题,我通过将颜色选项放在我的视图中来解决它。这就是我所做的:

#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
于 2011-09-02T01:10:15.163 回答
0
@chart = LazyHighCharts::HighChart.new('pie') do |c|
    c.colors(["red","green","blue"]);
end
于 2015-04-13T19:37:19.290 回答