high chart date format in x-axis - highcharts

I am using column chart in highchart in this i want to show x-axis in date format which i fetch from the google analytics data of my account.and i want to view the date on x-axis and which is not coming in format i want it in formatted way.
it comes like this way "20151001" and i want it to be like this "01-10-2015"
my data is come from google analytic account so i cant use highchart data
this is my code for this
php code from which i get date
$matrics='ga:sessions,ga:pageviews';
$results_top_pages = $analytics->data_ga->get(
'ga:109200082',
date('Y-m-d',strtotime('-6 days')),
date('Y-m-d',strtotime('now')),
$matrics,
array(
'dimensions' => 'ga:date',
// 'sort' => '-ga:pageviews', for acsending and decending page view sorting
'filters'=>'ga:browser%3D~%5E'.$browser.'',
'max-results' => 50
));
if(is_array($results_top_pages->getRows())){
echo '<ol>';
foreach($results_top_pages->getRows() as $top_page){
echo '<li>';
echo $top_page[0];
echo ' - '.$top_page[1].' ';
echo ' - '.$top_page[2].' ';
$data[]=$top_page[2];
$date[]=$top_page[0];
echo '</li>';
}
echo '</ol>';
}
by var_dump of $date i get date in this format:
array(7) {
[0]=>
string(8) "20150930"
[1]=>
string(8) "20151001"
[2]=>
string(8) "20151002"
[3]=>
string(8) "20151003"
[4]=>
string(8) "20151004"
[5]=>
string(8) "20151005"
[6]=>
string(8) "20151006"
}
Edited code from here ::
This is js code for highchart:
$data[]=$top_page[2];
$date[]=date('d-m-y',strtotime($top_page[0]));
// echo '</li>';
}
echo '</ol>';
}
var_dump($date[0]);
$datefirst= date('U', strtotime($date[0])) * 1000;
?>
<script>
$(function () {
$('#container').highcharts({
//alert("call");
chart: {
type: 'column'
},
title: {
text: 'Weekly Traffic'
},
subtitle: {
//text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [<?php echo join($date,','); ?>],
max:6
},
yAxis: {
title: {
text: 'Views'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true,
connectNulls: false
},
enableMouseTracking: false
}
},
series: [{
pointStart: '<?php echo $datefirst; ?>',
pointInterval: 86400000, //one day
pointRange: 86400000, //one day
name: '<?php echo $browser; ?>',
data: [<?php echo join($data, ',') ?>],
//pointStart: Date.UTC(2015, 0, 1),
// pointInterval: 24 * 3600 * 1000
},
]
});
});
</script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

Ok, so, working from your example, you just need to format the dates properly.
Something like this:
foreach($results_top_pages->getRows() as $top_page){
echo '<li>';
echo $top_page[0];
echo ' - '.$top_page[1].' ';
echo ' - '.$top_page[2].' ';
$data[]=date('d-m-Y', strtotime($top_page[2])); //add the date function
$date[]=date('d-m-Y', strtotime($top_page[0])); //add the date function
echo '</li>';
}
echo '</ol>';
}
If you want to use a datetime x axis type, which I would suggest any time you're displaying time series data, you would just change the format to use millisecond epoch time, like this:
date('U', strtotime($top_page[2])) * 1000
But instead of building an array of categories, you would set that value as the x value of your data.
Alternatively, if the data is always going to be a daily value, you can set your pointStart property as the start time, and your pointInterval property as 1 day (86400000 milliseconds)
{{edit ----------
An example of the last method:
http://jsfiddle.net/jlbriggs/xabj5qgp/
You supply:
A start datetime stamp in epoch format
An interval between points (ie one day - in milliseconds)
Your array of data points, one data point per interval (day)
The chart does the rest.
{{Edit for comments:
So, you can put the pointStart and pointInterval properties in one of two places - either in the plotOptions, or in the series properties directly.
pointRange isn't necessarily required, but it is usually a good idea as it fixes some potential spacing issues with columns/bars.
So in your case, take this:
series: [{
name: '<?php echo $browser; ?>',
data: [<?php echo join($data, ',') ?>]
},
And add the properties there:
series: [{
pointStart: __your_start_date__,
pointInterval: 86400000, //one day
pointRange: 86400000, //one day
name: '<?php echo $browser; ?>',
data: [<?php echo join($data, ',') ?>]
},
You will need to retrieve that start date from your data source and format it as epoch time in milliseconds (as demonstrated above)

Related

Highchart Column with Drilldown using MySQL

I want to display vote records per service in my column chart. Then, when i click 1 of the service it will show up the vote records of that service by day.
<?php
$sql = $mysqli->query("SELECT * FROM services");
while ($row0 = $sql0->fetch_assoc()) {
$sid0 = $row0['sid'];
$service[] = $row0['service'];
$sql1 = $mysqli->query("SELECT COUNT(*) as totalVotes FROM entries where sid = '$sid0'");
while ($row01 = $sql01->fetch_assoc()) {
$totalVote01 = $row01['totalVotes'];
$vote01[] = (int)$totalVote01;
}
}
?>
<script>
Highcharts.chart('container', {
chart: {
type: 'column'
},
xAxis: {
categories: ['<?php echo join($service, ', ') ?>']
}
series: [{
name: 'Total Votes per Service',
data: [{
name: '<?php echo join($service, ', ') ?>',
y: '<?php echo join($vote01, ', ') ?>',
drilldown: '<?php echo join($service, ', ') ?>'
}]
}]
})
</script>
Error: Highcharts error #14: www.highcharts.com/errors/14
Please help me with codes, my presentation is tomorrow badly need your help. Just a beginner. Thank you so very much!
Problem is in this line
y: '<?php echo join($vote01, ', ') ?>',
It will return a String as '12'
You need to pass a number here.
You can try withoit quote
y: <?php echo join($vote01, ', ') ?>,
or parse value before setting
y: parseFloat('<?php echo join($vote01, ', ') ?>' ),

Highcharts - Performance issue using chart with x values date and TIME

I am plotting a chart with ‘y’ axis being numeric values and ‘x’ axis being date time values.
I was able to process a JSON object of 3000 items. However, because of our business rules, we also need to display the time: hours + minutes in the chart’s tooltips.
This changed everything!
Now the chart takes about 15 seconds to plot 2000 records. This is unacceptable.
I can see clearly that when I remove the time part of my Date object the charts works perfectly. It is just when added the hours and minutes that performance gets affected.
Trying different things I realized that your charts should support the amount of data I am using since it is not massive.
We love the charts but performance is key for us to continue using your products.
Can you help me with this issue?
Please check this fiddle so you can understand my problem. Feel free to remove the hours and minutes variables from the DateUtc creation:
https://jsfiddle.net/17a3jry9/7/
Thanks in advance!
var pointStart = new Date();
var data = [{"date":"2017-11-08","time":"1712","perc":10},{"date":"2017-11-08","time":"1608","perc":10},{"date":"2017-11-08","time":"1506","perc":10},{"date":"2017-11-08","time":"1408","perc":10},{"date":"2017-11-08","time":"1309","perc":10},{"date":"2017-11-08","time":"1207","perc":10},{"date":"2017-11-08","time":"1110","perc":10},{"date":"2017-11-08","time":"1003","perc":10},{"date":"2017-11-08","time":"0910","perc":10},{"date":"2017-11-08","time":"0810","perc":10},{"date":"2017-11-08","time":"0708","perc":10},{"date":"2017-11-09","time":"1710","perc":10},{"date":"2017-11-09","time":"1604","perc":10},{"date":"2017-11-09","time":"1510","perc":10},{"date":"2017-11-09","time":"1406","perc":10},{"date":"2017-11-09","time":"1310","perc":10},{"date":"2017-11-09","time":"1205","perc":10},{"date":"2017-11-09","time":"1107","perc":10},{"date":"2017-11-09","time":"1010","perc":10},{"date":"2017-11-09","time":"0912","perc":10},{"date":"2017-11-09","time":"0806","perc":10}{"date":"2018-10-25","time":"0709","perc":10},{"date":"2018-10-25","time":"1009","perc":10},{"date":"2018-10-25","time":"1208","perc":10},{"date":"2018-10-25","time":"1309","perc":10},{"date":"2018-10-25","time":"1410","perc":10},{"date":"2018-10-25","time":"1510","perc":10},{"date":"2018-10-25","time":"1702","perc":10},{"date":"2018-10-26","time":"1409","perc":10},{"date":"2018-10-26","time":"0710","perc":10},{"date":"2018-10-26","time":"1505","perc":10},{"date":"2018-10-26","time":"1704","perc":10},{"date":"2018-10-29","time":"0708","perc":10},{"date":"2018-10-29","time":"1007","perc":10},{"date":"2018-10-29","time":"1208","perc":10},{"date":"2018-10-29","time":"1406","perc":10},{"date":"2018-10-29","time":"1509","perc":10},{"date":"2018-10-29","time":"1610","perc":10},{"date":"2018-10-30","time":"0710","perc":10},{"date":"2018-10-30","time":"1010","perc":10},{"date":"2018-10-30","time":"1207","perc":10},{"date":"2018-10-30","time":"1409","perc":10},{"date":"2018-10-30","time":"1510","perc":10},{"date":"2018-10-30","time":"1709","perc":10},{"date":"2018-10-31","time":"0708","perc":10},{"date":"2018-10-31","time":"1009","perc":10},{"date":"2018-10-31","time":"1206","perc":10},{"date":"2018-10-31","time":"1409","perc":10},{"date":"2018-10-31","time":"1509","perc":10},{"date":"2018-10-31","time":"1708","perc":10},{"date":"2018-11-01","time":"0707","perc":10},{"date":"2018-11-01","time":"1007","perc":10},{"date":"2018-11-01","time":"1108","perc":10},{"date":"2018-11-01","time":"1250","perc":10},{"date":"2018-11-01","time":"1509","perc":10},{"date":"2018-11-01","time":"1407","perc":10},{"date":"2018-11-01","time":"1709","perc":10},{"date":"2018-11-02","time":"0708","perc":10},{"date":"2018-11-02","time":"1007","perc":10},{"date":"2018-11-02","time":"1108","perc":10},{"date":"2018-11-02","time":"1210","perc":10},{"date":"2018-11-02","time":"1407","perc":10},{"date":"2018-11-02","time":"1509","perc":10},{"date":"2018-11-02","time":"1608","perc":10},{"date":"2018-11-02","time":"1715","perc":10},{"date":"2018-11-05","time":"0707","perc":10},{"date":"2018-11-05","time":"1007","perc":10},{"date":"2018-11-05","time":"1209","perc":10},{"date":"2018-11-05","time":"1408","perc":10},{"date":"2018-11-05","time":"1509","perc":10},{"date":"2018-11-05","time":"1611","perc":10},{"date":"2018-11-05","time":"1715","perc":10},{"date":"2018-11-06","time":"0708","perc":10},{"date":"2018-11-06","time":"1007","perc":10},{"date":"2018-11-06","time":"1201","perc":10},{"date":"2018-11-06","time":"1410","perc":10},{"date":"2018-11-06","time":"1510","perc":10},{"date":"2018-11-06","time":"1709","perc":10},{"date":"2018-11-07","time":"0708","perc":10},{"date":"2018-11-07","time":"1007","perc":10},{"date":"2018-11-07","time":"1209","perc":10},{"date":"2018-11-07","time":"1307","perc":10},{"date":"2018-11-07","time":"1410","perc":10},{"date":"2018-11-07","time":"1506","perc":10},{"date":"2018-11-07","time":"1708","perc":10},{"date":"2018-11-08","time":"0707","perc":10},{"date":"2018-11-08","time":"1009","perc":10},{"date":"2018-11-08","time":"1207","perc":10},{"date":"2018-11-08","time":"1309","perc":10},{"date":"2018-11-08","time":"1412","perc":10},{"date":"2018-11-08","time":"1508","perc":10}];
var newSeries = data.map(function (key) {
var utilDate = new Date(key.date);
var hours = parseInt(key.time.slice(0, 2));
var minutes = parseInt(key.time.slice(2, 4));
return { x: Date.UTC(utilDate.getFullYear(), utilDate.getMonth(), utilDate.getDate(), hours, minutes), y: key.perc, key: key.id };
});
Highcharts.stockChart('container', {
xAxis: {
type: 'datetime',
title: {
text: 'Date'
}
},
yAxis: {
title: {
text: 'Util (%)'
},
min: 0
},
tooltip: {
headerFormat: '<b>{point.x: %A, %b %e, %I:%M %p}</b><br>'
//pointFormat: '{point.x:%e. %b}: {point.y:.2f} m'
},
colors: ['teal', 'red'],
// Define the data points. All series have a dummy year
// of 1970/71 in order to be compared on the same x axis. Note
// that in JavaScript, months start at 0 for January, 1 for February etc.
series: [{
name: "Util",
data: newSeries
}],
title: {
text: 'Util Movement'
},
subtitle: {
text: ""
},
plotOptions: {
series: {
cursor: 'pointer',
turboThreshold: 10000,
point: {
events: {
}
},
label: {
connectorAllowed: false
},
pointStart: pointStart.getFullYear()
}
},
responsive: {
rules: [{
condition: {
maxWidth: 200
},
chartOptions: {
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
}
}
}]
}
});
Please take a look to the browser console, you have information there about error: Highcharts error #15: www.highcharts.com/errors/15.
This means that you have unsorted data, so you need to sort them:
newSeries.sort(function(a, b){
return a.x - b.x
});
Live demo: https://jsfiddle.net/BlackLabel/onq0Lurx/

Highcharts: set only last chunk of line as a dotted zone

I'm trying to get this chart to only be dotted for the last month:
chart_data = [[1496275200000, 981], [1498867200000, 1089], [1501545600000, 1595], [1504224000000, 1296], [1506816000000, 1678], [1509494400000, 1879], [1512086400000, 2028], [1514764800000, 1885], [1517443200000, 1366], [1519862400000, 1558], [1522540800000, 1636], [1525132800000, 2438], [1527811200000, 2899], [1530403200000, 2521], [1533081600000, 2879], [1535760000000, 1702]]
Highcharts.chart('container', {
title: {
text: 'Zone with dash style'
},
subtitle: {
text: 'Dotted line typically signifies prognosis'
},
xAxis: {
type: 'datetime',
labels: {
formatter: function () {
return Highcharts.dateFormat('%Y-%m', this.value)
}
},
tickInterval: 30 * 24 * 3600 * 1000,
},
series: [{
data: chart_data,
zoneAxis: 'x',
zones: [{
value: chart_data.length - 2
}, {
dashStyle: 'dot'
}]
}]
});
https://jsfiddle.net/aL57381b/
(This highcharts code is basically the same as: http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/series/color-zones-dashstyle-dot/ from the highcharts documentation, just with my time-series data instead.
However, it makes the entire chart turn dotted instead of just the last bit. Any ideas why? I can't seem to fix this.
You need the real value not the starting index like that :
dottedStartIndex = chart_data[chart_data.length - 2][0];
Highcharts.chart('container', {
...
zones: [{
value: dottedStartIndex
}, {
dashStyle: 'dot'
}]
Fiddle

highstock charts - button for date range

in my application I use angular Highstock for charts. I have a lot of data (many millions), and now i want to put the existing buttons for set date range (1week, 1 month, 3 months, 1year, all), but also to click on any button to send a new request to the server to not accumulating data. Example, if pressed button "one week", that only the server retrieves the data for one week, if you click on a button to one month, to take data for a single month. I do not want to call unnecessary data, and that the server slows down, if I'm looking for data for one month, and the server comes back to me all data. Here is the code. Thanks in advance
> https://jsfiddle.net/tf7pr1ft/
Thnx to #Grzegorz Blachliński i fix this. here is code
$scope.chartConfig1 = {
xAxis: {
ordinal: false,
//za dodat date range postavit events i range selectors
events : {
afterSetExtremes: getDateRange //here i add my custom function
}
},
yAxis: {
plotLines: [{
color: '#FF0000',
width: 1,
value: 11.50,
label: {text: '11.50'}
}]
},
options: {
chart: {
zoomType: 'x',
backgroundColor: 'rgba(255, 255, 255, 1)',
polar: true,
type: 'line',
borderRadius: 5
},
legend: {
enabled: true
},
rangeSelector: {
selected : 0, //here i defined default range
enabled: true,
inputStyle: {
color: 'black'
}
},
navigator: {
enabled: true
}
},
series: [],
title: {
text: 'Solar and Battery voltage average'
},
useHighStocks: true
},
$scope.chartConfig1.series.push({
id: 1,
name: "Solar voltage average",
data: $scope.data[0],
tooltip: {
valueDecimals: 2
}
},   {
id: 2,
name: "Battery voltage average",
data: $scope.data[1],
tooltip: {
valueDecimals: 2
}
}
);
and here is my custom function
getDateRange = function (e) {
var date1 = new Date(e.min);
var date2 = new Date(e.max);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
$http.get(serviceBase + 'aaaaa/aaaaaa/' + $stateParams.klupaID + '/aaaaaaa?days=' + diffDays, function(data){
//ukoliko buden dodavat još koji config obavezno dodat broj koliko ih ima na i<=8
for (i=1; i<=8; i++){
$scope.chartConfigString = 'chartConfig' + i;
$scope.chartConfigString.series[0].setData(data);
$scope.chartConfigString.hideLoading();
}
});
};
Now i have another problems, i want to have four selectors (buttons), 1months, 3months, 1year and ALL. I set default range, when open app, on 1month, and here is problem. Buttons for another range is disabled, beacause i get data from url only for one month. I want to have enabled all buttons for select any range. Thnx

HighCharts SyntaxError: missing ] after element list

I am trying to write a script that produces a col chart. The data is from an MySQL query that produces the following array content:
97.2,63.9,33.4,28.9,325.2,114.9,46.1,56.7,104.9,4.5,42.2,56.4,10.5
When I run the script I get an error:
SyntaxError: missing ] after element list
I have tracked down to where I thik the error is but can not see whats wrong.
Inside the jQuery function I have an PHP echo statement which populates the $data variable with the data, $data[] = $row_An['AValue'];.
The values are correct as seen in FireFox dev console. Can anyone see why I am getting the error.
$(function () {
<?php
$data[] = $row_An['AValue'];
?>
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'AN'
},
xAxis: {
categories: ['Inbound', 'Outbound', 'InService']
},
yAxis: {
title: {
text: ''
}
},
series: [{
data: [97.2,63.9,33.4,28.9,325.2,114.9,46.1,56.7,104.9,4.5,42.2,56.4,10.5],
pointStart: 0,
pointInterval
}]
});
});
Many thanks in advance
what's value in pointInterval ? its wrong to leave that without any value
series: [{
data: [97.2,63.9,33.4,28.9,325.2,114.9,46.1,56.7,104.9,4.5,42.2,56.4,10.5],
pointStart: 0,
pointInterval //here is error you should provide value or remove it
}]
Also, these two properties PointInterval and PointStart are part of plotOptions.series
your number of categories don't match with number of data points ,correct them
categories: ['Inbound', 'Outbound', 'InService']

Resources