Rails and High Charts - can't render the maps at all - ruby-on-rails

I am having problems even getting off the ground with Highcharts maps.
I followed the rails installation.
Gemfile
gem "highcharts-rails", "~> 3.0.0"
application.js
//= require highcharts
//= require highcharts/highcharts-more
//= require highcharts/modules/data
//= require highcharts/modules/map
In my page
<div id="container" style="height: 500px; min-width: 310px; max-width: 600px; margin: 0 auto"></div>
<script>
$(function () {
// Prepare demo data
var data = [{
'hc-key': 'us',
value: 3
}, {
'hc-key': 'ca',
value: 5
}, {
'hc-key': 'mx',
value: 20
}];
// Initiate the chart
$('#container').highcharts('Map', {
title: {
text: 'Showing only non-null areas'
},
subtitle: {
text: 'The <em>allAreas</em> option is false'
},
mapNavigation: {
enabled: true,
buttonOptions: {
verticalAlign: 'bottom'
}
},
colorAxis: {
min: 0
},
series: [{
data: data,
mapData: Highcharts.maps['custom/north-america-no-central'],
joinBy: 'hc-key',
allAreas: false,
name: 'Random data',
states: {
hover: {
color: '#BADA55'
}
},
dataLabels: {
enabled: true,
format: '{point.name}'
}
}]
});
});
</script>
As you can see from the screenshot, there is no map rendered.
I have seen a similar question here:
Highmaps with US States in Rails
but I have tried these permutations and combinations of placing the links to the js files in my application header without success. The simple "your first chart" installation in the docs works fine
http://www.highcharts.com/docs/getting-started/your-first-chart
.....as soon as I try maps, no joy.
EDIT
So,
if i remove the files from application.js and add
<script src="http://code.highcharts.com/maps/highmaps.js"></script>
<script src="http://code.highcharts.com/maps/modules/data.js"></script>
<script src="http://code.highcharts.com/mapdata/custom/world.js"></script>
to my application header....the following works..
<div id="container" style="height: 500px; min-width: 310px; max-width: 600px; margin: 0 auto"></div>
<script>
$(function () {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=world-population-density.json&callback=?', function (data) {
// Add lower case codes to the data set for inclusion in the tooltip.pointFormat
$.each(data, function () {
this.flag = this.code.replace('UK', 'GB').toLowerCase();
});
// Initiate the chart
$('#container').highcharts('Map', {
title: {
text: 'Fixed tooltip with HTML'
},
legend: {
title: {
text: 'Population density per km²',
style: {
color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
}
}
},
mapNavigation: {
enabled: true,
buttonOptions: {
verticalAlign: 'bottom'
}
},
tooltip: {
backgroundColor: 'none',
borderWidth: 0,
shadow: false,
useHTML: true,
padding: 0,
pointFormat: '<span class="f32"><span class="flag {point.flag}"></span></span>'
+ ' {point.name}: <b>{point.value}</b>/km²',
positioner: function () {
return { x: 0, y: 250 };
}
},
colorAxis: {
min: 1,
max: 1000,
type: 'logarithmic'
},
series : [{
data : data,
mapData: Highcharts.maps['custom/world'],
joinBy: ['iso-a2', 'code'],
name: 'Population density',
states: {
hover: {
color: '#BADA55'
}
}
}]
});
});
});
</script>
Surely there has to be a nice "rails" way to get this working without explicitly calling the files in the header?

Related

Why do I get 'Cannot read property 'isValid' of undefined' when using 400 data points?

I am trying to visualise more than 400 data points using highcharts timeline, but I'm getting Cannot read property 'isValid' of undefined
Fiddle: https://jsfiddle.net/0fv2kxqb/
const test = [];
for (let i = 0; i < 400; i++) {
test.push({
x: Date.UTC(1900 + i, 9, 4),
name: 'test'
});
}
Highcharts.chart('container', {
chart: {
zoomType: 'x',
type: 'timeline'
},
xAxis: {
type: 'datetime',
visible: false
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
legend: {
enabled: false
},
title: {
text: 'Timeline of Space Exploration'
},
subtitle: {
text: 'Info source: www.wikipedia.org'
},
tooltip: {
style: {
width: 300
}
},
series: [{
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'{point.x:%d %b %Y}</span><br/>{point.label}'
},
marker: {
symbol: 'circle'
},
data: test
}]
});
#container {
min-width: 400px;
max-width: 600px;
margin: 0 auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/timeline.js"></script>
<div id="container"></div>
It works fine with ~300 data points
Does it not work with large amounts of data points?
That problem is a bug and it is reported here: https://github.com/highcharts/highcharts/issues/11116
Possible workarounds:
set cropThreshold bigger than data length: https://jsfiddle.net/BlackLabel/rtkj5xd7/
enable getExtremesFromAll: https://jsfiddle.net/BlackLabel/rtkj5xd7/2/

Highchart Pie chat not display Codeigniter

I'am trying to use Pie chart of highchart with Codeigniter for the first time. I can't display the graph with my codes below. I can't pass the json to the graph data to display the pie chart.
Any help is greatly appreciated.
Thanks
The query result in json format shows like this :
[{"sexo":"Feminino","total":"4"},{"sexo":"Masculino","total":"2"}]
Controller
public function charCont()
{
$query = $this->model_admin->getPieChat();
$data['query']=json_encode($query);
$this->load->view('chart',$data);
}
View
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Test'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data:<?=$query?>
}]
});
You need preprocess your data to the format required by Highcharts:
var data = [{
"sexo": "Feminino",
"total": "4"
}, {
"sexo": "Masculino",
"total": "2"
}];
data = data.map(function(obj) {
return {
name: obj.sexo,
y: Number(obj.total)
}
});
Highcharts.chart('container', {
...,
series: [{
...
data: data
}]
});
Live demo: http://jsfiddle.net/BlackLabel/yfLovx8b/
API Reference: https://api.highcharts.com/highcharts/series.pie.data

Uncaught TypeError: undefined is not a function while iam Embeding ruby in jquery

i have a table in my main page in which left side i want to show one graph and right side i want to show second one. when iam redering both view from my index.html.erb its showing me above error and just showing me only one graph. if i show only one graph it doesn't give any error.
index.html.erb: here iam integrating my highcharts and rendering views
<head>
<!--Load the AJAX API-->
<%= javascript_include_tag "highcharts"%>
</head>
<tr>
<td width = "490">
<b><%= render :partial => 'new1' %></b>
</td>
<td width = "490" colspan = 2>
<b><%= render :partial => 'new' %></b>
</td>
</tr>
2. new1.html.erb:- its view which has to be showed left side
<head>
<script>
$(function () {
// Radialize the colors
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function (color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
// Build the chart
$('#pie').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: true
},
title: {
text: ''
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
},
connectorColor: 'silver'
}
}
},
series: [{
type: 'pie',
name: 'calculated',
data: <%= raw #result1 %>
}]
});
});
</script>
</head>
<div id="pie" style="width:500px; height:300px; vertical-align: top; display: inline-block; padding: 10px;">
3. new.html.erb:-> its view which has to be showed right side
<head>
<script>
$(function () {
// Radialize the colors
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function (color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
// Build the chart
$('#highpie').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: true
},
title: {
text: ''
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
},
connectorColor: 'silver'
}
}
},
series: [{
type: 'pie',
name: 'calculated',
data: <%= raw #result %>
}]
});
});
</script>
</head>
<div id="highpie" style="width:500px; height:300px; vertical-align: top; display: inline-block; padding: 10px;">
conclusion:-> when iam redering both view from my index.html.erb its showing me above error and just showing me only one graph. if i show only one graph it doesn't give any error
Wow! Your index.html.erb it is not valid, should be in the css folder, js scripts in his. You have all scattered.
in my both view i was using the same code and method of highcharts. i think
it was clashing i changed the highcharts method a little bit it worked fine for me
this is the code:->
<head>
<script>
$(function () {
// Make monochrome colors and set them as default for all pies
Highcharts.getOptions().plotOptions.pie.colors = (function () {
var colors = [],
base = Highcharts.getOptions().colors[0],
i;
for (i = 0; i < 10; i += 1) {
// Start out with a darkened base color (negative brighten), and end
// up with a much brighter color
colors.push(Highcharts.Color(base).brighten((i - 3) / 7).get());
}
return colors;
}());
// Build the chart
$('#highpie').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: ''
},
credits: {
enabled: false
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
type: 'pie',
name: 'calucated',
data: <%= raw #result %>
}]
});
});
</script>
</head>
<div id="highpie" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

How to do realtime highcharts in Ruby on Rails?

I want to build realtime highcharts, and i have user table added sidekiq worker which fetches user_table every 5 minutes and i want it show my users data in real time in my rails application?
Below i have static data, how to retrieve data dynamically from database?
In View:
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script>
$(function () {
$('#container').highcharts({
chart: {
type: 'spline'
},
title: {
text: 'User balance',
x: -20 //center
},
xAxis: {
categories: ['Users']
},
yAxis: {
title: {
text: 'Balance'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '$'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: User'',
data: [<%= #user.sum('equivalent_sum')%>]
}]
});
});
In Controller:
#user = User.where('balance > ?', 0)

High chart problwm with multiple axes with dynamic data

I am trying to generate multi axes graph uding highchart as below.
But its not working, array generation and all are quite fine.
If i hardcode totalNoOfLabels and totalLabelsSize then its working. Please suggest:
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
$(function () {
var result=[{"date":"2013-05-01","propertiesList": {"labelsCount":"14","totalSize":"62.62"}},{"date":"2013-05-04","propertiesList":{"labelsCount":"4","totalSize":"22.43"}},{"date":"2013-05-03","propertiesList":{"labelsCount":"7","totalSize":"34.09"}},{"date":"2013-05-02","propertiesList":{"labelsCount":"13","totalSize":"67.51"}},{"date":"2013-05-05","propertiesList":{"labelsCount":"3","totalSize":"11.65"}}];
var totalNoOfLabels=[];
var totalLabelsSize=[];
var dates=[];
dailyStatList = JSON.stringify(result);
var statsObj = $.parseJSON(dailyStatList);
$.each(statsObj, function() {
dates.push(this.date);
totalNoOfLabels.push(this.propertiesList.labelsCount);
totalLabelsSize.push(this.propertiesList.totalSize);
});
$('#container').highcharts({
chart: {
zoomType: 'xy'
},
title: {
text: 'Info'
},
xAxis: [{
categories: dates
}],
yAxis: [{ // Primary yAxis
labels: {
style: {
color: '#89A54E'
}
},
title: {
text: 'No of labels',
style: {
color: '#89A54E'
}
}
}, { // Secondary yAxis
title: {
text: 'Size ',
style: {
color: '#4572A7'
}
},
labels: {
style: {
color: '#4572A7'
}
},
opposite: true
}],
tooltip: {
shared: true
},
legend: {
layout: 'vertical',
align: 'left',
x: 120,
verticalAlign: 'top',
y: 100,
floating: true,
backgroundColor: '#FFFFFF'
},
series: [{
name: 'Labels',
color: '#4572A7',
type: 'column',
yAxis: 1,
data: totalNoOfLabels
}, {
name: 'Size',
color: '#89A54E',
type: 'spline',
data: totalLabelsSize
}]
});
});
labelsCount and totalSize should be numbers, not strings. Parse that values, or put in JSON as numbers and will work properly.

Resources