HighCharts - invalid or unexpected token - javascript
I have created a high chart using the code from the following fiddle, but have changed the data to a java string (which pulls in fine) but get the error "uncaught syntax error: invalid or unexpected token"
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/column-rotated-labels/
When i click on the error it loads this console sources and points to the "data" tag:
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Users per lesson'
},
xAxis: {
type: 'category',
labels: {
rotation: -45,
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
},
yAxis: {
min: 0,
title: {
text: 'Users'
}
},
series: [{
name: 'Users',
data: [
"['Snowboarding for dummies', 1],
['Advanced Carving
Techniques', 1],"
],
dataLabels: {
enabled: true,
rotation: -90,
color: '#FFFFFF',
align: 'right',
format: '{point.y:.1f}', // one decimal
y: 10, // 10 pixels down from the top
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
}]
});
What i am actually passing is:
data: [
<%=session.getAttribute("jsTag") %>
],
But this is creating the string with linebreaks in?
If i output the string it outputs as all on one line:
['Snowboarding for dummies', 1], ['Advanced Carving Techniques', 1],
There is an error in the serie data, Highcharts can't understant a Javascript string so you will need to parse the string. It should be like that :
...
data: [
['Snowboarding for dummies', 1],
['Advanced Carving Techniques', 1]
],
...
Fiddle
Based on the HighChart sample code, it seems that you are trying to pass an array ( data) with a single string value.
HighCharts expects a 2D array as the data key. So if you specify:
data:[ ['Shanghai', 24.2],...]
i.e 'Shanghai' is the x-axis value and 24.2 is the y-axis value.
Now if you want to pass the data as a string, you may want to parse the string first.
var data=JSON.parse("[[\"Snowboarding for dummies\", 1], [\"Advanced Carving Techniques\", 1]]");
And then pass this data variable to the HighChart object.
All the best!
Related
Pass data to Stacked Column Chart (Highcharts Framework)
I have a problem when trying to render a stacked column chart with the Highcharts framework. I get an error when rendering on the client: Uncaught TypeError: a.toPrecision is not a function From what I've gathered this is related with stacking. If I set stacking to null it won't give an error, but no graph will show. I'm passing dynamic data to the series. The data is in the form of a name for the legend and an array of values for each xAxis category. Below is an example of one object that is passed as data. This is stored in an array with a more entries. name:"READ_CALENDAR" y: (24) [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] This must be the wrong way, since I can't get it to work, but what then is the correct way? My question is, what am I doing wrong? How should I pass dynamic data to make it work? I need to pass several unique instances of data that need to stack and match the xAxis categories. I will add more details below regarding how I pass data and such. If you want more info comment and I will try to provide as much as I can. Please help! Thanks in advance!!! This is an example of data that is passed to the client: [{"name":"READ_CALENDAR","timesUsed":[1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0]}, {"name":"READ_CONTACTS","timesUsed":[1,0,0,0,0,0,0,0,0,0,0,0,5,8,12,3,2,0,1,0,0,0,0,0]}, {"name":"READ_EXTERNAL_STORAGE","timesUsed":[1,0,0,0,0,0,0,0,0,10,0,1,9,17,13,39,8,1,0,0,0,0,0,2]}, {"name":"WRITE_EXTERNAL_STORAGE","timesUsed":[1,0,0,0,0,0,0,0,0,10,0,2,9,17,13,38,8,1,0,0,0,0,0,2]}, {"name":"WRITE_SMS","timesUsed":[1,0,0,0,0,0,0,0,0,0,0,0,5,7,12,2,2,1,0,0,0,0,0,0]}, {"name":"CAMERA","timesUsed":[0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,2,2,1,0,0,0,0,0,0]}, {"name":"FINE_LOCATION","timesUsed":[0,0,0,0,0,0,0,0,0,15,4,0,0,0,0,2,0,0,0,0,0,0,0,0]}, {"name":"GPS","timesUsed":[0,0,0,0,0,0,0,0,0,3,0,0,0,1,1,2,0,0,0,0,0,0,0,0]}, {"name":"MONITOR_HIGH_POWER_LOCATION","timesUsed":[0,0,0,0,0,0,0,0,0,4,0,0,0,1,1,2,0,0,0,0,0,0,0,0]}, {"name":"MONITOR_LOCATION","timesUsed":[0,0,0,0,0,0,0,0,0,3,0,0,0,1,1,2,0,0,0,0,0,0,0,0]}, {"name":"OP_READ_PHONE_STATE","timesUsed":[0,0,0,0,0,0,0,0,0,2,0,0,3,2,1,3,0,2,0,0,0,0,0,0]}, {"name":"POST_NOTIFICATION","timesUsed":[0,0,0,0,0,0,0,0,0,16,15,7,10,14,26,17,5,2,1,0,0,0,0,0]}, {"name":"TAKE_AUDIO_FOCUS","timesUsed":[0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0]}, {"name":"TOAST_WINDOW","timesUsed":[0,0,0,0,0,0,0,0,0,14,15,7,10,9,13,12,3,1,0,0,0,0,0,0]}, {"name":"WAKE_LOCK","timesUsed":[0,0,0,0,0,0,0,0,0,6,0,0,1,3,2,17,7,2,0,0,0,0,0,1]}, {"name":"WIFI_SCAN","timesUsed":[0,0,0,0,0,0,0,0,0,2,0,0,0,0,2,0,0,0,0,0,0,0,0,0]}, {"name":"BOOT_COMPLETED","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,4,2,0,0,0,0,0,0,0,0,0,0]}, {"name":"GET_ACCOUNTS","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,4,4,0,3,0,0,0,0,0,0,0,0]}, {"name":"WIFI_CHANGE","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,4,2,0,0,0,0,0,0,0,0,0,0]}, {"name":"USE_FINGERPRINT","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]}, {"name":"RECORD_AUDIO","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,0,0,0,0,0,0,0]}, {"name":"VIBRATE","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,0,0,0,0,0,0,0]}, {"name":"WRITE_CALENDAR","timesUsed":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0]}] This data will then be parsed to a JSON Object with: var jsonObj = JSON.parse(obj); (where obj is the data) Then the the data will be pushed to an array: var seriesData = []; for (var i in jsonObj) { seriesData.push({ name: jsonObj[i].name , y: jsonObj[i].timesUsed }); } Then the Hichcharts scripts looks like this: Highcharts.chart('permData', { chart: { type: 'column' }, title: { text: 'Example graph' }, xAxis: { categories: ['00-01','01-02','02-03','03-04','04-05','05-06','06-07','07-08','08-09','09-10','10-11','11-12','12-13','13-14','14-15','15-16','16-17','17-18','18-19','19-20','20-21','21-22','22-23','23-24'] }, yAxis: { min: 0, title: { text: 'Y axis text here' }, stackLabels: { enabled: true, style: { fontWeight: 'bold', color: null /*(Highcarts.theme && Highcharts.theme.textColor) || 'gray'*/ } } }, legend: { align: 'right', x: -30, verticalAlign: 'top', y: 25, floating: true, backgroundColor: null /*(Highcharts.theme && Highcharts.theme.background2) || 'white'*/, borderColor: '#CCC', borderWidth: 1, shadow: false }, tooltop: { headerFormat: '<b>{point.x}</b><br/>', pointFormat: '{series.name}:{point.y}<br/>', }, plotOptions: { column: { stacking: 'percentage', dataLabels: { enabled: true, color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white' } } }, series: [{ type: 'column', name: 'perm', data: seriesData }] });
You were very close, you have set y instead of data in your loop building the dataSeries. It should be like this: for (var i in jsonObj) { seriesData.push({ name: jsonObj[i].name , data: jsonObj[i].timesUsed //data instead of y }); } After changing that, it works in the fiddle. y is a member of data, and is used in case you want to set several properties for each point. Working example: https://jsfiddle.net/ewolden/r5yd56eb/1/ API on series.data: https://api.highcharts.com/highcharts/series.column.data
HIghcharts Heatmap Overload with Too Much Data?
$(function() { $('#container').highcharts({ chart: { type: 'heatmap', marginTop: 40, marginBottom: 80, plotBorderWidth: 0.5 }, title: { text: 'Number' }, xAxis: { categories: [ .......... ] }, yAxis: { categories: [ .......... ], title: null }, colorAxis: { min: 0, minColor: '#FFFFFF', maxColor: Highcharts.getOptions().colors[0] }, legend: { align: 'right', layout: 'vertical', margin: 0, verticalAlign: 'top', y: 25, symbolHeight: 280 }, tooltip: { formatter: function() { return '' + this.series.xAxis.categories[this.point.x] + '<br>' + this.series.yAxis.categories[this.point.y] + '<br>' + this.point.value; } }, series: [{ name: 'Sales per employee', borderWidth: 1, data: [ .......... ], http://jsfiddle.net/Slate_Shannon/0mvgmhLb/6/ This is a heatmap chart. The chart looks okay as-is, but should have more data. However, if I add any additional data, the chart breaks. Note that a large part of the data is commented out. This starts with the data that begins with [50,0,null], [50,1,8380], [50,2,37430], (note that I removed the axis labels and the data in the code shown above.) If you change the position of the beginning of the comment tag to so that the "50" data gets charted, then the chart fails. Is this just too much data, or is there a way to create a heatmap that needs to be approx 20 x 90 cells?
Set turboThreshold to 0, from the Highcharts API: When a series contains a data array that is longer than this, only one dimensional arrays of numbers, or two dimensional arrays with x and y values are allowed. Also, only the first point is tested, and the rest are assumed to be the same format. This saves expensive data checking and indexing in long series. Set it to 0 disable. Defaults to 1000. http://api.highcharts.com/highcharts/plotOptions.heatmap.turboThreshold Example: http://jsfiddle.net/0mvgmhLb/7/ series: [{ name: 'Sales per employee', borderWidth: 1, turboThreshold: 0, ...
Try loading the Highcharts Boost module. The boost.js module is a module to allow quick loading of hundred thousands of data points in Highcharts. Instead of relying solely on SVG to display the graph, this module does the following to boost performance: ... drawing the graph on a canvas, then copying over the contents to an image tag inside the SVG, using a data URL. Link to official blog post. Just load the module after highcharts.js (lib/modules/boost.js).
This can be solved by providing the colorAxis with a min and max value. Highcharts has issues with calculating the max value with extremely large data sets.
How do I remove padding from both sides of Highcharts area category chart?
There's too much padding on either side of the area chart and minPadding/maxPadding doesn't work with categories. I want the area chart to start and end without any padding. My code is below: http://jsfiddle.net/nx4xeb4k/1/ $('#container').highcharts({ chart: { type: 'area', inverted: false }, title: { text: 'Too Much Padding On Either Side' }, plotOptions: { series: { fillOpacity: 0.1 } }, xAxis: { type: 'category' }, yAxis: { title: { text: 'Data Point' } }, legend: { enabled: false }, tooltip: { pointFormat: '<b>{point.y}</b> points' }, series: [{ name: 'Visits', data: [ ["Monday", 58], ["Tuesday", 65], ["Wednesday", 55], ["Thursday", 44], ["Friday", 56], ["Saturday", 65], ["Sunday", 69] ], dataLabels: { enabled: false, rotation: -90, color: '#FFFFFF', align: 'right', format: '{point.y:.1f}', y: 10, style: { fontSize: '14px', fontFamily: 'Verdana, sans-serif' } } }] });
A colleague of mine solved this very situation for some of my charts. Their solution was to remove the type: 'category' from the x-axis (making it a linear type instead) and instead replace the axis labels from an array. Here's what's been changed: First, I added an array of your x-axis labels. var categoryLabels = ["Monday","Tuesday","Wednesday","Thursday","Friday", "Saturday","Sunday"]; Next, I updated your series values to hold only the y-axis values. series: [{ name: 'Visits', data: [58, 65, 55, 44, 56, 65, 69], Then, for your x-axis, I included a formatter function to pull in the labels from the array as substitutes for the default linear values. xAxis: { labels: { formatter: function(){ return categoryLabels[this.value]; } } }, Lastly, I updated the tooltip options to show the values from the labels array. tooltip: { formatter: function () { return categoryLabels[this.x] + ': ' + Highcharts.numberFormat(this.y,0); } }, I updated your fiddle with this tweaks: http://jsfiddle.net/brightmatrix/nx4xeb4k/4/ I hope you'll find this solution as useful as I have!
According to API, the default value of highcharts.xAxis.tickmarkPlacement is between and this is why the point of each category drops between two ticks on xAxis in your chart. By setting highcharts.xAxis.tickmarkPlacement to on and playing around the value of highcharts.xAxis.min and highcharts.xAxis.max like this, you should be able to achieve what you want.
You can declare the min / max values to fix the problem. var categoryLabels = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; //.... xAxis: { min: 0.49, max: categoryLabels.length - 1.49, categories: categoryLabels, type: 'category' }, Example: http://jsfiddle.net/fo04m7k7/
Dates on xaxis with highcharts
I need to have dates on my xaxis on my chart. I collect my data through a range datepicker where the user can enter a range of 14 days. I need the first value on the xaxis to be the start date of the range and the last value to be the end date of the range. i want the the xaxis labels to read something like "27th Jan 2015" , "28th Jan 2015" or something in that direction and each tic to be 1 day. I've read the API-documentation and i've played around alot with the settings of the chart but for some reason i can not get it to work. $('#graphColumn').highcharts({ title: { text: '', x: -20 //center }, xAxis: { categories: [dayArray[0], dayArray[1], dayArray[2], dayArray[3], dayArray[4], dayArray[5], dayArray[6], dayArray[7], dayArray[8], dayArray[9], dayArray[10], dayArray[11], dayArray[12], dayArray[13]] }, yAxis: { title: { text: '' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { valueSuffix: '' }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', borderWidth: 0 }, credits: { enabled: false }, series: [{ showInLegend: false, name: ' ', data: series, color: '#77B7C5', marker: { symbol: 'circle' } }, { showInLegend: false, name: '1 år sedan', data: series1yearago, color: "#71C73E", marker: { symbol: 'circle' } }] }); here is my chart. I know the xaxis has categories set now but thats only for the moment, i know it should be datetime. My series are regular int arrays with one number on each index. I would apreciate any help that could be given here! Edit: Here is a working jsfiddle example: http://jsfiddle.net/g0p00tLv/1
Your data, and how it is formatted, is what we really need to see. You need to do one of two things: 1) pass your data as an array of [x,y] pairs, where x is either the epoch time stamp (in miliseconds), or a date.UTC object 2) pass your data as a single array of y values, and use the pointStart and pointInterval properties to set the date axis properly. reference: http://api.highcharts.com/highcharts#plotOptions.series.pointStart http://api.highcharts.com/highcharts#plotOptions.series.pointInterval If you need more info, set up a working fiddle example and post it here.
Highcharts column fit/adjust on chart
I write because I go back to being stuck in a problem with Highcharts. I have a monthly chart that works fine except for one thing. The zoom level. The X axis is always shown me a value of 0 (today), so that the zoom level is incorrect. I am attaching a picture to try to explain it better. I need this column set in the graph. I appreciate your help! Thank you! The json returned by PHP is (correct results): {"data":[[1401580800000,2],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0],[1400025600000,0]]} And Javascript file: chart = new Highcharts.Chart({ chart: { renderTo: 'divStatsGrupo', plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false }, title: { text: tituloMes }, tooltip: { formatter: function() { return Highcharts.dateFormat('%d/%m/%Y',new Date(this.x)) + '<br/>' +'Alarmas: ' + this.y } }, xAxis: { type: 'datetime', dateTimeLabelFormats : { day: '%e. %b', labels: { style: { width: '200px','min-width': '100px' }, useHTML : true, } } }, yAxis: { title: { text: 'Total alarmas' }, allowDecimals: false, min: 0 }, series : [{ showInLegend: false, name : 'Grafica Mensual', type : 'column', data: data.data, dataLabels: { enabled: true, rotation: 0, color: '#000000', align: 'center', y: 0, style: { fontSize: '14px', fontFamily: 'Verdana, sans-serif', }} }] }); }); ///cierra get EDIT: I need a graphic for month (user select), but, the white area and Xaxis only appers info for the month selected. The PHP file return a correct JSON chain, but highcharts not fit the columns fine. Sorry for my english!
The problem is with your JSON, where you have duplicated values for the same timestamp. Just remove them. Then! You have unsorted data, it should be sorted ascending by timestamp. After fixed, it works fine, see: http://jsfiddle.net/4nCx3/ var data = { "data": [ [1400025600000, 0], [1401580800000, 2] ] };