I'm using Ruby on Rails and HighCharts:
So here is the print-screen that I see when setting my highchart:
Here is the code:
#chart = LazyHighCharts::HighChart.new('pie') do |f|
f.chart({:defaultSeriesType=>"pie" , :margin=> [50, 200, 60, 170]} )
series = {
:type => 'pie',
:name => 'Browser share',
:data => [['say', 100],['may', 500]]
}
f.series(series)
f.options[:title][:text] = "Total spend on services"
f.legend(:layout=> 'vertical',:style=> {:left=> 'auto', :bottom=> 'auto',:right=> '50px',:top=> '100px'})
f.plot_options(:pie=>{
:allowPointSelect=>true,
:cursor=>"pointer" ,
:dataLabels=>{
:enabled=>true,
:color=>"white",
:style=>{
:font=>"13px Trebuchet MS, Verdana, sans-serif"
}
}
})
end
All is good, apart from I don't see the labels on the pie... It seems there is a small string below the chart, however the label is not initialized.
Anyone know how to solve this issue?
Is it because you have it set to white?
:color=>"white",
Related
I have a conditional statement which determines the color of my highcharts lines. I have multiple charts in which I'd like to repeat this conditional. Currently I'm finding that I have to repeat the code in each chart as I can't seem to do a variable with the conditionals stored. This goes against any DRY method, and will seem troublesome in the future.
My chart (using Lazy highcharts gem)
#chart_1 = LazyHighCharts::HighChart.new('graph') do |f|
#sales.sale_selections.flat_map { |ss|
f.series(animation: false, type: 'spline', name: ss.sale_type.name,
data: ts.sale_methods.map { |sm| { y: sm.sales_total.to_f, x: sm.days }}, color:
if ss.sale_type.name.include? 'soda'
'red'
elsif ss.sale_type.name.include? 'chips'
'blue'
else
'black'
end
)}
end
Is it possible to have the conditional as a variable in which to call upon when needed. For example (which doesn't work, but using as a example):
#colours = if ss.sale_type.name.include? 'soda'
'red'
elsif ss.sale_type.name.include? 'chips'
'blue'
else
'black'
end
Yes. Write a function to return the color you want.
def row_color(sale_type_name)
if sale_type_name.include? 'soda'
'red'
elsif sale_type_name.include? 'chips'
'blue'
else
'black'
end
end
Then call the function to get the color
#colour = row_color(ss.sale_type.name)
I am working on a web app in ruby on rails that should list presentations that a set of users can sign up for. At the end, the admin should be able to send an email to all users with an automatically generated PDF. I am using prawn to create this pdf.
The controller:
def generate_pdf(teac,pres)
Prawn::Document.new(:page_size => "A4", :page_layout => :landscape) do
text "#{teac.name} #{teac.surname}", align: :center
text "Your Presentations"
table([
["Name","Surname","Title","Room","Time","Date"],
[pres.collect{ |r| [r.name] },
pres.collect{ |r| [r.surname] },
pres.collect{ |r| [r.title] },
pres.collect{ |r| [r.room] },
pres.collect{ |r| [Time.at(r.time.to_i).utc.strftime("%H:%M")] },
pres.collect{ |r| [r.date] }]
])
move_down 20
pres.each do |pres|
text "visitors '#{pres.title}':"
text "#{pres.visitors}"
move_down 20
end
end.render
end
The problem occurs when the title is so long that the table expands beyond the landscape A4 page. Is there a way to automatically add a new line when the title becomes to long?
If you are okay with setting static column widths, that should to the trick.
For example:
table(data, column_widths: [100, 100, 100, 100, 100, 100])
I am trying to draw a linear function(like y=ax+b) by using Chartkick gem and google chart on my rails application.
'a' and 'b' will be given as parameters.
The problem is that I am trying to achieve an infinite line graph using google chart out of slope 'a' and 'b' and I don't know how to achieve this in a proper way.
I came up with the solution of drawing a line chart with two points, (-b/a, 0) and (0, b) and give trendlines option onto it.
<!-- y = 0.55x floatify(2,#datas[4][2]) + 1.47 floatify(2,#datas[3][2]) -->
<%= line_chart [
{name: "targets",
data: [
["#{-(#datas[3][2].to_f/#datas[4][2].to_f).round(2)}",0],
[0,"#{#datas[3][2].to_f}"]
]
},
],
library: {
title: "#{#simplereg.variable_name}",
subtitle: "#{#simplereg.data_name}",
legend: "right",
vAxis: {title: 'average'},
trendlines: {
0: {
type: 'exponential',
visibleInLegend: true
}
}
}
%>
However, rails cannot read a '0' value as an option of trendlines unlike what google chart documentation says.
-> https://developers.google.com/chart/interactive/docs/gallery/trendlines
I have no clue how to fix this code to work.
Please help me... I don't mind throwing up this code and writing in completely different way.
Thanks ahead.
Use something like below
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
<%= line_chart [
{name: "targets",
data: [
["-2/3",0],
[0,"2"]
]
},
],
library: {
title: "ABC",
subtitle: "XYZ",
legend: "right",
vAxis: {title: 'average'},
trendlines: {
"0"=> {
type: 'exponential',
visibleInLegend: true
}
}
}
%>
basically instead of symbo 0: use "0" =>
I'm having some issues with parsing some JSON data into high charts using the Lazy Highcharts gem. I'm trying to select only the data from the last 7 days, or 1 week ago. At this stage my application just hangs and doesn't load with the code below.
I am loading the JSON data from a link.I have tried the pointStart option, but it doesn't seem to work.
Any help would be appreciated.
JSON
{"status": "ok", "data": [{"2014-06-16 16:00:00": 24.2},{"2014-06-17 12:00:00": 30.2},{"2014-06-18 17:00:00": 42.9}]} etc
Controller
#data = Oj.load(open(#temperature.url).read)
results = []
#data['data'].each do |data|
results << ((7.day.ago.to_i * 1000)..(Date.today.to_i * 1000)).map { |date| [DateTime.parse(data.keys.first).to_i * 1000 == date, data.values.first] }
end
#graph = LazyHighCharts::HighChart.new('graph') do |f|
f.chart(:height => '400', width: '860')
f.yAxis [:title => {:text => "Temperature, :margin => 20, style: { color: '#333'}}]
f.series(:type => 'line', :name => 'Temperature', pointStart: 7.day.ago.to_i * 1000, data: results, marker: {enabled: false}, :color => '#00463f' )
f.xAxis(:type => 'datetime', tickInterval: 1.day.to_i * 1000, :tickmarkPlacement => 'on', :startOnTick => true )
f.legend({:align => 'center', :verticalAlign => 'top', :y => 0, :borderWidth => 0, style: {color: "#333"}})
end
I have solved this. If any one is interested I added;
min: 1.weeks.ago.at_midnight.to_i * 1000
To the xAxis.
I'm doing a rails app and was able to get chartkick working but customizing it is a bit of a pain in the butt. I'm trying to customize the tooltip with the Google chart API but I can't seem to figure out. I just want to be able to change the text, "Value".
Could someone put me in the right direction.
Here is my code:
<%=
column_chart [
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
],
height: "220px",
library: {
width: 665,
fontName: "Helvetica Neue",
colors: ["#29abe2"],
tooltip: {
textStyle: {
color: "#333333"
}
},
bar: {
groupWidth: "50%"
},
vAxis: {
title: "Everyone",
titleTextStyle: {
italic: false,
color: '#777'
},
gridlines: {
color: "#eeeeee"
},
viewWindow: {
max: m
}
}
}
%>
The chartkick documentation is sparse, but if this system works the way I suspect it does, then you need to add an array of column headers to the start of your data, eg:
[
["Label for column 0", "Label for column 1"],
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
]