我正在使用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
这个方法不会留下任何错误,这个宝石的正确方法是什么?或者不可能?
或者我可以将哪些其他好的替代宝石用于我的项目?
答案 0 :(得分: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
答案 1 :(得分:0)
@chart = LazyHighCharts::HighChart.new('pie') do |c|
c.colors(["red","green","blue"]);
end