How to display content $scope in HighCharts? - angularjs

I'm doing a statistical graph using angularJs and highChartsJS.
Here is the code angularJS:
app.controller("StatController",function($scope,$http,fileUpload,$window, $filter)
{
var ids=location.search; // id ressource
$scope.FindStats=function(){
$http.get(url+"/Stats"+ids)
.success(function(data){
$scope.Stat = data;
console.log(JSON.stringify($scope.Stat));//{"idStat":21,"nbrBoks":7,"nbSection":5,"total":135,"resCon":0.0518519,"resNotCon":0.037037}
}).error(function(err,data){
console.log("error:"
+data);
});
};
$scope.FindStats();
});
Html code:
<div>
{{Stat}}
<!--{"idStat":21,"nbrBoks":7,"nbSection":5,"total":135,"resCon":0.0518519,"resNotCon":0.037037} -->
</div>
<script type="text/javascript">
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.2f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Result of books',
y: '{Stat.resNotCon}', // error is here
color: '#00c853',
},{
name: 'Result of section',
y:'{Stat.resCon}', //error is here
color: '#b71c1c',
}]
}]
});
</script>
After a test of the code, I have a problem :
Uncaught Error: Highcharts error #14: www.highcharts.com/errors/14
at Object.a.error (http://code.highcharts.com/highcharts.js:10:49)
at k.setData (http://code.highcharts.com/highcharts.js:289:213)
at k.init (http://code.highcharts.com/highcharts.js:282:174)
at a.Chart.initSeries (http://code.highcharts.com/highcharts.js:248:70)
at http://code.highcharts.com/highcharts.js:271:370
at Array.forEach (native)
at a.each (http://code.highcharts.com/highcharts.js:27:360)
at a.Chart.firstRender (http://code.highcharts.com/highcharts.js:271:341)
at a.Chart.init (http://code.highcharts.com/highcharts.js:247:444)
at a.Chart.getArgs (http://code.highcharts.com/highcharts.js:246:307)
So the problem is with the format of the data in highCharts.js:
Highcharts Error #14
String value sent to series.data, expected Number
This happens if you pass in a string as a data point, for example in a
setup like this:
series: [{ data: ["3", "5", "1", "6"] }] Highcharts expects the data
values to be numbers. The most common reason for this is that data is
parsed from CSV or from a XML source, and the implementer forgot to
run parseFloat on the parsed value.
For performance reasons internal type casting is not performed, and
only the first value is checked (since 2.3).
Edit1:
data: [{
name: 'Result of books',
color: '#00c853',
y: {Stat.resNotCon} // error is here
},{
name: 'Result of section',
color: '#b71c1c',
y: {Stat.resCon} //error is here
}]
Error of edit1:
Uncaught SyntaxError: Unexpected token. in y: {Stat.resNotCon}
Edit2:
$scope.FindStats=function(){
$http.get(url+"/Stats"+ids)
.success(function(data){
$scope.Stat = data;
console.log(JSON.stringify($scope.Stat));//{"idStat":21,"nbrBoks":7,"nbSection":5,"total":135,"resCon":0.0518519,"resNotCon":0.037037}
}).error(function(err,data){
console.log("error:"
+data);
});
};
$scope.FindStats();
console.log("$scope "+$scope.Stat); //it's empty
var Stat=$scope.Stat;
console.log("after "+Stat); // it's empty
How to format data for highCharts.JS?
Thank you,

The problem is resolved through the following code:
var app = angular.module('myApp',[]);
app.controller("StatController",function($scope,$http,$window, $filter,$RootScope)
{
$RootScope.FindStats = function() {
$scope.Stat = {
"idStat": 21,
"nbrBoks": 7,
"nbSection": 5,
"total": 135,
"resCon": 0.0518519,
"resNotCon": 0.037037
};
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.2f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Result of books',
y: Stat.resNotCon,
color: '#00c853',
},{
name: 'Result of section',
y:Stat.resCon,
color: '#b71c1c',
}]
}]
});
}
$scope.FindStats();
});

You just have to store the value of Stat in variable and not bind it to scope.
var app = angular.module('myApp',[]);
app.controller("StatController",function($scope,$http,$window, $filter)
{
$scope.FindStats = function() {
$scope.Stat = {
"idStat": 21,
"nbrBoks": 7,
"nbSection": 5,
"total": 135,
"resCon": 0.0518519,
"resNotCon": 0.037037
};
}
$scope.FindStats();
var Stat = $scope.Stat;
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares January, 2015 to May, 2015'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.2f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Result of books',
y: Stat.resNotCon, // error is here
color: '#00c853',
},{
name: 'Result of section',
y:Stat.resCon, //error is here
color: '#b71c1c',
}]
}]
});
});
Working Example
http://jsfiddle.net/ADukg/11648/

Related

How Do you convert JQuery HighChart to React

Here is a full link to the project: Electoral Map
I have this Highchart map that I'm trying to convert to React but can't quite figure it out. I tried using React rappers but didn't succeed.
What I have:
JSON data - will be fetched from an API but I have hard-coded them as below.
Jquery functions that maps the data.
Several highcharts imports.
I have not included the path data, too long it wouldnt post.
$(function() {
var json = [{
"name": "Busia",
"registered": "251305",
"UDA": "0",
"Azimio": "0",
"value": "-5"
},{
"name": "Wajir",
"registered": "118091",
"UDA": "8",
"Azimio": "7",
"value": "-2"
}]
function init() {
function pointClick(json) {
var row = this.options.row,
$div = $('<div></div>')
.dialog({
title: ([this.name]),
width: 400,
height: 300
});
window.chart = new Highcharts.Chart({
chart: {
renderTo: $div[0],
type: 'pie',
width: 370,
height: 240
},
title: {
text: null
},
series: [{
name: 'Votes',
data: [{
name: 'Azimio',
color: '#0200D0',
y: Number(this.Azimio)
}, {
name: 'UDA',
color: '#C40401',
y: Number(this.UDA)
}],
dataLabels: {
format: '<b>{point.name}</b> {point.value:.1f}%'
}
}]
});
}
// Initiate the chart
$('#presidential').highcharts('Map', {
title: {
text: 'Presidential Electoral Map <em>(Kenya)</em>'
},
legend: {
title: {
text: 'Political Affiliation'
}
},
credits: {
enabled: false
},
tooltip: {
valueSuffix: 'Margin'
},
mapNavigation: {
enabled: true,
enableButtons: false
},
colorAxis: {
dataClasses: [{
from: 0.0000001,
to: 100,
color: '#C40401',
name: 'UDA'
}, {
from: -100,
to: -0.00000001,
color: '#0200D0',
name: 'Azimio'
}, {
from: 0,
to: 0,
color: '#C0C0C0',
name: 'Battle Ground(s)'
}]
},
series: [{
name: 'By County Difference',
point: {
events: {
click: pointClick
}
},
"type": "map",
"joinBy": ['name', 'name'],
"data": $.each(json, function() {}),
"mapData": [{
"name": "Busia",
"path": "M40,-534,43,-533,46,-532L46,-530L44,-528,44,-525C44,-525,41,-520,41,-520L40,-516,40,-513,41,-511C41,-511,44,-512,43,-509,43,-506,44,-504,44,-504L38,-499,38,-497,44,-495,45,-493,41,-489,41,-486L36,-486L34,-487,30,-488,28,-487,25,-484,22,-484,20,-486,18,-483,16,-481,15,-478,14,-476L14,-473L15,-471,14,-469L12,-469L10,-467,9,-464,10,-459C10,-459,9,-458,7,-457,5,-456,5,-455,5,-455L3,-459,0,-462,0,-465,2,-470,2,-474L2,-478L5,-481,8,-486,10,-491,13,-493L13,-495L12,-499,13,-503,15,-506,15,-510,16,-513C16,-513,19,-516,20,-517,21,-517,24,-519,24,-519L27,-519,28,-519,31,-520L31,-524L32,-526,33,-527,34,-531,35,-532z"
},
}]
}, {
"type": "mapline",
"data": [{
"name": "path5072",
"path": "M443,-449Z"
}]
}]
});
}
init()
});
I've reproduced your example in the working demo that you can find below.
What's important, I didn't use the dialog popup which is a specific jQuery method. Instead, I show a pie chart inside the tooltip, with the use of several point.events such like mouseOver, mouseOut and click as well.
point: {
events: {
//Show the default tooltip
mouseOver: function () {
let point = this;
this.series.chart.update({
tooltip: {
enabled: true,
formatter: function () {
let s = "";
s += `<span style="color:${point.color}">●</span> <span style="font-size: 10px"> ${point.series.name}</span><br/>`;
s += `${point.name}: ${point.value}<br/>`;
return s;
}
}
});
},
//Show the pie chart
click: function () {
let y1 = Number(this.Azimio);
let y2 = Number(this.UDA);
this.series.chart.update({
tooltip: {
useHTML: true,
enabled: true,
formatter: function () {
setTimeout(function () {
Highcharts.chart("chart", {
chart: {
type: "pie"
},
title: {
text: null
},
series: [
{
name: "Votes",
data: [
{
name: "Azimio",
color: "#0200D0",
y: y1
},
{
name: "UDA",
color: "#C40401",
y: y2
}
],
dataLabels: {
format: "<b>{point.name}</b> {point.value:.1f}%"
}
}
]
});
}, 10);
return '<div id="chart" style="width: 300px; height: 150px;"></div>';
}
}
});
},
//Remove the tooltip
mouseOut: function () {
this.series.chart.update({
tooltip: {
enabled: false
}
});
}
}
},
API REference:
https://api.highcharts.com/highmaps/series.map.point.events
Demo:
https://codesandbox.io/s/highcharts-react-demo-forked-44tmqt

Highchart: Break y-axis doest work in react app but is working in js fiddle

using same options in js-fiddle and react app y-axis break is not working in react app but is working fine in js-fiddle.
highchart version i am using is 7.2.1
[enter link description here][1] jsfiddle where break is working
Highcharts.chart('container', {
chart: {
height: 250,
animation: false,
borderColor: '#EFF3F5',
plotBorderColor: '#EFF3F5',
plotBorderWidth: 1,
style: {
},
marginLeft: undefined,
},
legend: {
enabled: false,
},
credits: {
enabled: false,
},
title: {
text: '',
},
subtitle: {
text: '',
},
xAxis: [{
categories: [],
type: 'datetime',
crosshair: {
color: '#96abb6',
width: 1,
snap: false,
},
labels: {
style: {
fontSize: '10px',
color: '#334252',
fontFamily: 'DecimaMono',
textTransform: 'uppercase',
lineHeight: '12px',
whiteSpace: 'nowrap',
},
formatter: function() {
return this.value;
},
},
alternateGridColor: '#F7F9FA',
}, ],
yAxis: [{
tickPositioner: function() {
const positions = [];
let tick = Math.floor(this.dataMin);
const max = Math.min(200, this.dataMax);
const increment = Math.ceil((max - this.dataMin) / 6);
if (this.dataMax !== null && this.dataMin !== null) {
for (tick; tick - increment <= max; tick += increment) {
positions.push(tick);
}
}
if (this.dataMax > 200) {
positions.push(this.dataMax);
}
return positions;
},
title: {
text: null,
},
labels: {
style: {
fontSize: '10px',
textAlign: 'right',
},
},
breaks: [{
from: 200,
to: 1700,
}],
}, ],
series: [{
name: 'Attraction 1',
data: [-0.3543, 5.4184, -31.3792, 95.2435, 135.5852, 104.7914, 84.5844, 8.5129, -38.4724, -54.1816, -13.1134, 677.986, 1763, 1420.0503, 760.9013, 100.8341, 10.4576, 89.8975, 97.4758, 55.4993, 51.4611, 24.1278, 9.9771, 26.9394, 22.042, 32.9894, 145.3526, 88.1315, 135.0617, 119.6472, 29.8568, 43.94, 26.4247, 43.4719, 128.6346, 119.7356, 33.2159, 58.6534, -7.6348, 2.1865, 31.7992],
color: '#e63c45',
lineWidth: 3,
}, ]
});
[enter image description here][2]
[2]: https://i.stack.imgur.com/TIKkO.png screenshot from react app
I reproduce your code in the React environment and everything looks fine, same as in the jsFiddle.
Demo: https://codesandbox.io/s/highcharts-react-demo-83jnr?file=/demo.jsx

How to create overlapping bar charts in angular js?

i have trying to create a chart like
this. I have tried with highchart.js and chart.js but result is not like expected. Is there any plugin to create a chart like this? Or any other way to stack like this in highchart.js or in chart.js? Thank you.
You can use highcharts Fixed placement columns that is inverted. Check the demo posted below.
HTML:
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
JS:
Highcharts.chart('container', {
chart: {
type: 'column',
inverted: true
},
xAxis: {
categories: [
'Seattle HQ',
'San Francisco',
'Tokyo'
]
},
yAxis: [{
min: 0,
title: {
text: 'Employees'
}
}, {
title: {
text: 'Profit (millions)'
},
opposite: true
}],
legend: {
shadow: false
},
tooltip: {
shared: true
},
plotOptions: {
column: {
grouping: false,
shadow: false,
borderWidth: 0
}
},
series: [{
name: 'Employees',
color: 'rgba(165,170,217,1)',
data: [150, 73, 20],
pointPadding: 0,
groupPadding: 0.15,
pointPlacement: 0
}, {
name: 'Employees Optimized',
color: 'rgba(126,86,134,.9)',
data: [140, 90, 40],
pointPadding: 0.2,
pointPlacement: 0
}]
});
Demo:
https://jsfiddle.net/BlackLabel/g0b9uev5/1/

React Highstock Won't Work But Highchart works

I would like to switch from React Highcharts to React Highstock because of the compare option I would like to use in Highstock. Here is my config:
var config = {
credits: {
enabled: false
},
title: {
text: '' // No Title
},
chart: {
height: 200,
type: 'line',
backgroundColor: 'rgba(155, 255, 255, 0.0)' // Always transparent Background
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true
},
yAxis: {
labels: {
format: '$ {value}' // Always Money
},
title: {
text: '' // No Label
}
},
xAxis: {
type: 'datetime',
labels: {
format: '{value:%Y\'%b}'
},
tickInterval: getTickInterval(this.props.interval),
},
plotOptions: {
series: {
color: transformColor(this.props.color),
lineWidth: 1,
compare: 'percent',
showInNavigator: true
},
line: {
marker: {
enabled: false
}
}
},
legend: {
layout:'vertical',
align: getLegendPosition(this.props.legend).align,
verticalAlign: getLegendPosition(this.props.legend).verticalAlign,
x: getLegendPosition(this.props.legend).x,
y: getLegendPosition(this.props.legend).y,
floating: true,
itemStyle: {
font: 'sans-serif',
fontWeight: 'bold',
fontSize: '9px'
},
symbolHeight: 10,
symbolWidth: 10
},
series: this.props.series
};
return (
<div>
<ReactHighcharts config={config}/>
</div>
)
}
But when I switch to Highstock:
return (
<div>
<ReactHighstock config={config}/>
</div>
)
It gives me an error:
InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/Highstock.a88db6d5.jsx') is not a valid name.
Any ideas?
Nevermind, don't actually use the React-Highstock npm package. Just do this:
const ReactHighstock = require('react-highcharts/ReactHighstock');

Highcharts API legend doesnt appear

I want to put a legend, but I don´t know why it doesn´t appear. Also, I want to put the database in the fragments of my pie charts.
myapp.controller('myctrl', function ($scope) {
var data = [
['APP Android', 45.0],
['APP Ios', 26.8],
]
$scope.highchartsNG = {
options: {
chart: { type: 'pie'}
},
legend: {
enabled: true
},
series: [ {
name: 'Avisos',
innerSize: '50%'
},
{
name: 'Plataforma',
size: '80%',
innerSize: '65%',
showInLegend: false,
data: data
}],
loading: false
}
});
This example is public in http://jsfiddle.net/Cp73s/2038/
Check this out
var myapp = angular.module('myapp', ["highcharts-ng"]);
myapp.controller('myctrl', function($scope) {
var data = [
['APP Android', 45.0],
['APP Ios', 26.8],
['Widget Web', 12.8],
['MTC BAckoffice', 8.5],
['Correo electrónico', 6.2],
['Facebook', 6.2],
['Twitter', 6.2],
['Teléfono', 6.2],
['Presencial', 6.2],
]
$scope.highchartsNG = {
options: {
chart: {
type: 'pie'
},
plotOptions: {
pie: {
dataLabels: {
distance: -25 //adjust this value to change label distance
}
}
},
},
title: {
text: '550<br>AVISOS',
align: 'center',
verticalAlign: 'middle'
},
legend: {
enabled: true
},
series: [{
name: 'Avisos',
innerSize: '50%'
}, {
name: 'Plataforma',
size: '80%',
innerSize: '65%',
showInLegend: true,
data: data
}],
loading: false
}
});
Fiddles
fiddle with updated label
Fiddle with updated legend display

Resources