I'm trying to embed a Highchart Histogram in a Jupyter-notebook. I've simplified the code down to 2 cells and a small dummy series for the actual data to try and limit any surrounding code that could be causing the issue. I am still continually getting an error 17 on the console(that the series type doesn't exist). I'm thinking the problem is with importing the histogram-bellcurve module, but haven't been able to work around this. Any help would be appreciated.
Cell 1:
%%javascript
require.config({
paths: {
highcharts: "http://code.highcharts.com/highcharts",
highcharts_hist: "http://code.highcharts.com/modules/histogram-bellcurve"
},
shim: {
highcharts: {
exports: "Highcharts",
deps: ["jquery"]
},
highcharts_hist: {
exports: "Highcharts",
deps: ["highcharts"]
}
}
});
Cell 2:
%%javascript
$("#container").remove();
element.append('<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>');
require(['highcharts_hist'], function(Highcharts) {
$('#container').highcharts({
'title': {
'text': 'Highcharts Histogram'
},
'xAxis': [{
'title': { 'text': 'Data' },
'alignTicks': false
}, {
'title': { 'text': 'Histogram' },
'alignTicks': false,
'opposite': true
}],
'yAxis': [{
'title': { 'text': 'Data' }
}, {
'title': { 'text': 'Histogram' },
'opposite': true
}],
'series': [{
'name': 'Histogram',
'type': 'histogram',
'xAxis': 1,
'yAxis': 1,
'baseSeries': 's1',
'zIndex': -1
}, {
'name': 'Data',
'type': 'scatter',
'data': [1,1,1,2,2,3,3,3,3,3,4,5,6,7,7,7,7,7,7,7,8,8,9,9,10,11],
'id': 's1',
'marker': {
'radius': 1.5
}
}]
});
});
From Google it would appear that this approach once worked, however your result now seems the default. From the Highcharts documentation, it appears that you need to load the module specifically.
In your case I had success with this approach (changes in cell 2):
%%javascript
$("#container").remove();
element.append('<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>');
require(['highcharts', 'highcharts_hist'], function(Highcharts, histogram) {
histogram(Highcharts); // This is where the magic happens
$('#container').highcharts({
// ... for brevity
});
});
See this gist or this nbviewer to see it in action.
Related
I have generated a bar chart with Highcharts.
On this bar chart, I'm using an annotation to mark the 50% threshold with these words: "conformité partielle".
The problem is that in the data table, I don't want an "annotations" column.
Is there a way to avoid this?
Thanks very much for your help.
The option to exclude from the generated table, as well as other data export formats is includeInDataExport: false.
To exclude a data series it should be added to that series object, at the top level.
To exclude annotation labels, it should be placed in the labelOptions object, inside the annotation object:
document.addEventListener('DOMContentLoaded', function () {
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4],
//includeInDataExport: false
}, {
name: 'John',
data: [5, 7, 3],
}],
annotations: [{
labels: [{
point: { x: 2, y: 5, yAxis: 0 },
text: 'My annotation'
}],
labelOptions: {
includeInDataExport: false
}
}],
exporting: {
showTable: true
}
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="width:100%; height:300px;"></div>
The above is the standard solution for this question. What follows might be relevant for a larger category of related problems that
require the manipulation of the generated table in non-standard ways.
If one has to manipulate the table after its html was generated, one may add a handler for the chart's render event. The annotation can be deleted this way:
document.addEventListener('DOMContentLoaded', function () {
Highcharts.chart('container', {
chart: {
type: 'bar',
events: {
render() {
const tableDiv = this.dataTableDiv,
seriesLength = this.series.length;
if(tableDiv && tableDiv.querySelectorAll('thead th').length === seriesLength+2){
tableDiv.querySelectorAll('th:last-child, td:last-child, tbody>tr:last-child').forEach(el=>el.remove())
}
}
},
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4],
}, {
name: 'John',
data: [5, 7, 3],
}],
annotations: [{
labels: [{
point: { x: 2, y: 5, yAxis: 0 },
text: 'My annotation'
}]
}],
exporting: {
showTable: true
}
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="width:100%; height:300px;"></div>
One has to take into consideration the fact that the handler for render is typically called multiple times, including for instance when the mouse pointer moves inside the chart div - so if the table content is to be modified by addition or deletion one has to make sure that it happens only once. In this case we delete the last column and the last row of the table only when the number of rows is equal to the number of series + 2 -- this assumes that no series has {includeInDataExport: false}. Alternative conditions can be thought of, like searching for the text of the annotation.
Finally, a mention should be made for the exportData
event handler, that might be useful in some cases. The problem in this particular case is that it doesn't offer access to the table heads and indeed the argument doesn't contain anything about the annotations (although it is filled up in the same object after the execution of the handler).
Since at the time when exportData handler is called, the chart's dataTableDiv was created, albeit it is empty, one can exploit it in a rather hacky manner:
document.addEventListener('DOMContentLoaded', function () {
Highcharts.chart('container', {
chart: {
type: 'bar',
events: {
exportData: function() {
const tableDiv = this.dataTableDiv,
seriesLength = this.series.length;
setTimeout(function(){
if(tableDiv && tableDiv.querySelectorAll('thead th').length === seriesLength+2){
tableDiv.querySelectorAll('th:last-child, td:last-child, tbody>tr:last-child').forEach(el=>el.remove())
}
}, 0);
}
}
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4],
}, {
name: 'John',
data: [5, 7, 3],
}],
annotations: [{
labels: [{
point: { x: 2, y: 5, yAxis: 0 },
text: 'My annotation'
}]
}],
exporting: {
showTable: true
}
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="width:100%; height:300px;"></div>
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
I'm going to prepare a stacked column charts report using highcharts. The problem is my data types are different.
Please, view image example
Could you help me create a chart like image. please
You can achieve this using Stacked Group in Highchart by handling data section properly.
Here, I tried to build same chart as you required. You can handle legend and other of the chart.
This should help you. I also created fiddle demo which you can access using below link.
Fiddle Demo: http://jsfiddle.net/t4u8b8co/1/
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: ''
},
legend: {
enabled: false
},
xAxis: {
categories: [
['19603', '19666'], '19603', '19603'
]
},
yAxis: {
title: {
text: 'Good or Bad'
}
},
plotOptions: {
column: {
stacking: 'percent'
}
},
series: [{
name: 'Jane',
data: [244, 23, 4],
stack: '19603'
}, {
name: 'John',
data: [306, 522, 546],
stack: '19603'
}, {
name: 'Jane',
data: [376],
stack: 'female'
}, {
name: 'Janet',
data: [174],
stack: 'female'
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
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?
I have a page with multiple charts. When I scroll down to print a chart, the page returns focus to the top of the page, not the chart I just printed. Haven't found anything on the boards related to this, so hoping someone has encountered this and has a suggestion.
Here is an example: http://jsfiddle.net/ND5xf/
Scroll down to the third chart and choose to print. You can either print it or cancel the window. The page will return focus to the first chart.
HTML:
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container1" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div id="container3" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Javascript:
$(function () {
$('#container1').highcharts({
chart: {
type: 'line'
},
title: {
text: 'Line Chart'
},
xAxis: {
categories: ['Category 1', 'Category 2', 'Category 3']
},
series: [{
data: [7.0, 6.9, 9.5]
}, {
data: [-0.2, 0.8, 5.7]
}]
});
});
$(function () {
$('#container2').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Bar Chart'
},
xAxis: {
categories: ['Category 1', 'Category 2', 'Category 3']
},
series: [{
data: [107, 31, 635]
}, {
data: [133, 156, 947]
}]
});
});
$(function () {
$('#container3').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Column Chart'
},
xAxis: {
categories: ['Category 1', 'Category 2', 'Category 3']
},
series: [{
data: [49.9, 71.5, 106.4]
}, {
data: [83.6, 78.8, 98.5]
}]
});
});
Highcharts prints a single chart by hiding everything else on the page and then printing. It looks like that in this shuffle of DOM elements a potential scroll bar is forgotten. The only fix I can see (without modifying the source) is to take control of the printing yourself and reset the scroll position.
exporting: {
buttons: {
contextButton: {
menuItems: [{
text: 'Print Chart',
onclick: function() {
var scrollPos = $(document).scrollTop();
this.print();
setTimeout(function(){
$(document).scrollTop(scrollPos)
}, 1001); // Highcharts has a 1s delay before display elements again
}
}]
}
}
}
See fiddle here (try the 3rd chart).