简体   繁体   中英

Chartkick column charts multiple colors

I'm using chartckick in my RoR project to generate charts, which works quite nice. (along with Google Charts).

I've created a column chart with only 2 bars (male and female).

在此输入图像描述

And now the client wants that each bar have different color? Is that possible?

I've seen this post - How to change the color of a Column-chart created with Chartkick? but it's more then half of year old and I'm hoping that there is a way now to specify more colors to the bars.

Update

My code looks like:

Controller

@followers_gender_count = Project.find(params[:id]).followers.group(:gender).count

View

<%= column_chart parse_gender_data(@followers_gender_count) %>

Helper

def parse_gender_data(data)
  gender_data = Hash.new
  gender_data[:male] = data[1]
  gender_data[:female] = data[2]
  ({ 'Male' => gender_data[:male], 'Female' => gender_data[:female] })
end

Update 2 - Issue on GitHub

The following also works. I didn't need any new files in initializers (no chartkick.rb file), this just worked by adding the following code to my helper file:

  def networth_data
    [
      {name: "Assets", data: {"NetWorth": 6979.53}}, 
      {name: "Liabilities", data: {"NetWorth": 10532.32}}
    ]
  end

And then in my html.erb i specified the colors and other options I wanted:

<%= column_chart networth_data, 
  colors: ["green", "#FF0000"], 
  library: {backgroundColor: "#FFF", height: 265} %>

...renders this chart:

在此输入图像描述

From @buren's comment on GitHub it seems that using a different JSON structure will invoke different colors on the column_cart .


I changed the output in the Helper :

def parse_gender_data(data)
  gender_data = Hash.new
  gender_data[:male] = data[1]
  gender_data[:female] = data[2]
  [{"name" => "Male","data" => {"Gender" => gender_data[:male]}},{"name" => "Female","data" => {"Gender" => gender_data[:female]}}]
end

And created the chartkick.rb config file ( config/initializers/chartkick.rb ), adding some colors :

Chartkick.options = {
  colors: ["#63b598", "#ce7d78", "#ea9e70", "#a48a9e", "#c6e1e8"]
}


Which outputs the desiring result:

在此输入图像描述

Have you seen ColumnStyles

   function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Year', 'Visitations', { role: 'style' } ],
        ['2010', 10, 'color: gray'],
        ['2010', 14, 'color: #76A7FA'],
        ['2020', 16, 'opacity: 0.2'],
        ['2040', 22, 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'],
        ['2040', 28, 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2']
      ]);

but take a look at the link there are some really cool stuff there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM