How to correct index and current date - php

I have the following error with this highcharts chart library.
I need to update the date to the current one and that the date of the corresponding day appears in the index.
just change from Highcharts.chart to Highcharts.stockChart. (Attached image)
enter image description here
enter image description here
from here the data and graph are brought
function datagrafico(base_url){
$.ajax({
url: base_url + "index.php/Admin/getDataDias",
type:"POST",
dataType:"json",
success:function(data){
var dias = new Array();
var montos = new Array();
$.each(data,function(key, value){
dias.push(value.fecha_actualizacion);
valor = Number(value.monto);
montos.push(valor);
});
graficar(dias,montos);
}
});
}
function graficar(dias, montos){
Highcharts.stockChart('grafico', {
chart: {
renderTo: 'container',
type: 'column'
},
title: {
text: 'Monto acumulado por ventas diarias'
},
subtitle: {
text: ''
},
xAxis: {
categories: dias,
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Monto Acumulado (Colombiano)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">Monto: </td>' +
'<td style="padding:0"><b>{point.y} Colombiano</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 0
},
series:{
dataLabels:{
enabled:true,
formatter:function(){
return Highcharts.numberFormat(this.y)
}
}
}
},
rangeSelector: {
inputPosition: {
align: 'right',
x: 0,
y: 0
},
},
series: [{
name: 'dias',
data: montos
}]
});
controller:
public function getDataDias(){
$resultados = $this->model_venta->montos();
echo json_encode($resultados);
}
and model:
public function montos(){
$this->db->select("fecha_actualizacion, SUM(total) as monto");
$this->db->from("venta");
$this->db->where("pago_id","2");
$this->db->where("estado","1");
$this->db->group_by('DATE(fecha_actualizacion)');
$this->db->order_by('DATE(fecha_actualizacion)');
$resultados = $this->db->get();
return $resultados->result();
}

In your example you change Highcharts to Higcharts Stock, they are similar but building charts from other API. Please have a look at the Stock documentation they, do not have categories options inside xAxis.
The millisecond format of data is standard in the Highcharts Stock axis, you need to change data format to put it in the chart.
series: [{
type: 'column',
data: [
[1592314200000, 165428800],
[1592400600000, 114406400],
[1592487000000, 96820400],
[1592573400000, 264476000],
[1592832600000, 135445200],
[1592919000000, 212155600]
],
}]
Under are Highchart and Higcharts Stock examples to compare of building.
Highchart Demo: https://jsfiddle.net/BlackLabel/sw3qr5p9/
Highchart Stock Demo: https://jsfiddle.net/BlackLabel/drnup1tx/

Related

Set Xaxis from Json data in Highchart

I have json data like this:
and I want to display the above date data as x-axis on my chart:
$(document).ready(function () {
getAjaxData(1);
var val = location.search.split('balai=')[1]
getAjaxData(val);
function getAjaxData(balai){
$.getJSON('src/json/proyek/progress-fisik.php', {balai: balai}, function(progressFisik) {
var chart1 = new Highcharts.Chart('progress_fisik', {
chart: {
renderTo: 'progress_fisik',
type: 'column',
},
title: {
text: ''
},
subtitle: {
text: ''
},
yAxis: {
min: 0,
max: 100,
tickInterval: 20,
allowDecimals: false,
title: {
text: ''
},
},
xAxis: {
type: 'datetime',
labels: {
formatter: function ( ){
return Highcharts.dateFormat('%d-%m-%Y');
},
},
},
tooltip:{
formatter: function() {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.numberFormat(this.y, 2) +' %';
},
},
plotOptions: {
column: {
dataLabels: {
enabled: true,
format: '{point.y:,.2f}'+'%',
}
},
},
series:progressFisik,
});
});
}
});
I've spent a lot of time trying to solve this problem, but haven't gotten any results until now.
I hope someone is kind enough to help me on how I can improve my code above. any help I really appreciate. thanks.
You need to adapt your data to the format required by Highcharts. For example:
const series = progressFisik.map(dataEl => ({
name: dataEl.name,
data: dataEl.data.map((y, index) => [
new Date(dataEl.date[index]).getTime(),
y
])
}));
Live demo: http://jsfiddle.net/BlackLabel/Lhmw8p5f/
API Reference: https://api.highcharts.com/highcharts/series.line.data

Hightchart with ajax request

Good day, i'm trying to create a chart. The chart value is from ajax return.
I got this data
"tgl":["2016-07-12 00:00:00.000","2016-07-01 00:00:00.000"],"total":[1283947,1234618514]}
and here it's my js for handle hightchart
xAxis: {
categories: [
$.each(data.tgl, function(k, v) {
data.tgl
})
]
},
series: [{
name: 'Outlet' +$("#outlet").val(),
data: [
$.each(data.total, function(k, v) {
data.total
})
]
}]
i don't know how to foreach ajax so i follow this link jQuery loop over JSON result from AJAX Success?
and here is my php
function getajax()
{
extract(populateform());
$explode = explode('-',$date_search);
$start_date = inggris_date($explode[0]);
$end_date = inggris_date($explode[1]);
$hasil = $this->modelmodel->showdata("select tanggal,(cash + cc + dc + flash + piutang + reject + disc50)
total from transaksi
where tanggal between '$start_date' and '$end_date' and
outlet = '".$outlets."' order by tanggal desc");
$data = array();
foreach($hasil as $hsl)
{
$data['tgl'][] = $hsl->tanggal;
$data['total'][] = $hsl->total;
}
echo json_encode($data);
}
The result from my script above is like this.
as you can see from my data above. My chart not showing the right value. It should be like this 2016-07-12 00:00:00.000 => 1283947 and 2016-07-01 00:00:00.000 => 1234618514
My lates fiddle https://jsfiddle.net/oyrogu9v/1/
you should have 2 values in the series: time and total.
series: [{
name: 'Winter 2012-2013',
data: [
[Date.UTC(1970, 9, 21), 0],
[Date.UTC(1970, 10, 4), 0.28],
..................
you can see here a sample:
Highchart sample timeserie
please try this code:
$(document).ready(function() {
$("#test").click(function() {
var $btn = $(this);
$btn.button('loading');
$.ajax({
url: '<?=base_url();?>graph/getajax',
data: {outlets:$("#outlet").val(),date_search:$("#reservation").val()},
type: 'POST',
dataType: 'JSON',
success: function(data) {
var res= [];
$.each(data, function(k, v) {
res.push([new Date(data[k].tanggal).getTime(), data[k].total])});
$('#container').highcharts({
title: {
text: '',
x: -20 //center
},
subtitle: {
text: 'Omset ' + $("#outlet").val(),
x: -20
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Rupiah (Rp.)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ' Rupiah'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Outlet '+ $("#outlet").val(),
data:res
}]
});
});
setTimeout(function () {
$btn.button('reset');
}, 1000);
});
});
here you can see the results: https://jsfiddle.net/mb89apxr/

Ajax with HighCharts setData on Symfony 2

I have a problem on my HighCharts graph with JSON data. To explain a litle bit : I have a form, when I submit this form I send my data to my controller with Ajax, I treat them and I return the new array of data for my highcharts graph, but when I use setData function with my new array the graph disappear and the new data aren't displayed.
Here is my ajax function and my test to set the new data into my graph :
$('#gestion .l-gestion-content-form #form-add-informations .btn-validation').click(function(e) {
e.preventDefault();
var chart = $('#hightchart-graf').highcharts();
var dataActionForm = $('#form-add-informations').attr('action').split("/");
var url_ajax = Routing.generate('gestion_view', { dossier: dataActionForm[4], fille: dataActionForm[5] });
var formData = $('#form-add-informations').serializeArray();
$.ajax({
type: "POST",
dataType: 'json',
url: url_ajax,
data: formData,
success: function(msg)
{
console.log(msg['infosArray']);
var test = [ msg['infosArray'].join(',') ];
console.log(test);
console.log('---------------------------------------------');
console.log(msg.infosArray);
var test2 = msg.infosArray.join(',');
var test3 = test2.replace('"', '');
console.log(test2);
chart.series[0].setData( [ msg.infosArray.join(',') ] );
//[ {{ msg.infosArray | join(',') }} ]
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('ERROR');
}
});
});
All my data are stored in the array named msg.infosArray or msg['infosArray'] and the first console.log return an array like this :
["[Date.UTC(2015,11,23),80.00]", "[Date.UTC(2015,11,23),89.00]", "[Date.UTC(2015,11,25),150.00]", "[Date.UTC(2015,11,28),45.00]", "[Date.UTC(2015,11,29),169.00]", "[Date.UTC(2015,11,29),189.00]", "[Date.UTC(2015,11,29),196.00]", "[Date.UTC(2015,11,29),200.00]", "[Date.UTC(2015,11,29),205.00]", "[Date.UTC(2015,11,30),210.00]", "[Date.UTC(2015,11,31),225.00]", "[Date.UTC(2016,0,1),250.00]", "[Date.UTC(2016,0,2),25.00]", "[Date.UTC(2016,0,3),259.00]", "[Date.UTC(2016,0,5),25.00]", "[Date.UTC(2016,0,6),250.00]", "[Date.UTC(2016,0,7),25.00]", "[Date.UTC(2016,0,8),250.00]", "[Date.UTC(2016,0,9),25.00]", "[Date.UTC(2016,0,10),250.00]", "[Date.UTC(2016,0,11),25.00]", "[Date.UTC(2016,0,12),250.00]", "[Date.UTC(2016,0,25),25.00]", "[Date.UTC(2016,0,26),250.00]"]
So I tried to escape " and ' or to join the array but nothing work. Somebody know how can I set the new data, or where I commited a fault ?
Maybe i got a bad response from my Controller ?
Thanks
You need to convert string of javascript code to array of javascript.
I wrote a demo version which converts string to javascript code. and draws chart as expected. Do not focus on the labels it's a dummy data.
JSFiddle Demo
$(function () {
var data = ["[Date.UTC(2015,11,23),80.00]", "[Date.UTC(2015,11,23),89.00]", "[Date.UTC(2015,11,25),150.00]", "[Date.UTC(2015,11,28),45.00]", "[Date.UTC(2015,11,29),169.00]", "[Date.UTC(2015,11,29),189.00]", "[Date.UTC(2015,11,29),196.00]", "[Date.UTC(2015,11,29),200.00]", "[Date.UTC(2015,11,29),205.00]", "[Date.UTC(2015,11,30),210.00]", "[Date.UTC(2015,11,31),225.00]", "[Date.UTC(2016,0,1),250.00]", "[Date.UTC(2016,0,2),25.00]", "[Date.UTC(2016,0,3),259.00]", "[Date.UTC(2016,0,5),25.00]", "[Date.UTC(2016,0,6),250.00]", "[Date.UTC(2016,0,7),25.00]", "[Date.UTC(2016,0,8),250.00]", "[Date.UTC(2016,0,9),25.00]", "[Date.UTC(2016,0,10),250.00]", "[Date.UTC(2016,0,11),25.00]", "[Date.UTC(2016,0,12),250.00]", "[Date.UTC(2016,0,25),25.00]", "[Date.UTC(2016,0,26),250.00]"];
//t = t.map(function(element){return eval(element);});
data = data.map(function (e) {
return eval(e);
});
data = data.map(function (e) {
return [(new Date(e[0])).toLocaleString(), e[1]]
});
var categories = data.map(function (e) {
return (new Date(e[0])).toLocaleString()
});
// t[0] = [date = new Date(t[0])];
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: categories
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: data
}]
});
});

Highchart json from mysql data not able to show up in line graph

I have the following data that I get dynamically from the mysql query via json and ajax. It just cant populate into the graph. I have shown below how my sample data looks likes. I put some sammple data into the data series as below. I also tried this chart.series[0].setData( eval( '(' + data + ')' ) ); not working either
["[1443843286000,0]",
"[1443843286000,100]",
"[1443843356000,200]",
"[1443843356000,0]",
"[1443843356000,100]",
"[1443843359000,0]"," ....
Here is the snippet of mysql how this data is generated.
while($row = $selectQueryResult1->fetch())
{
$count++;
$dateTimer1 =$row['dateTimer']*1000;
$speed=$row['speed'];
$data[] = "[$dateTimer1,$speed]";
}
echo json_encode($data);
Calling ajax and chart initialisation.
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'dashBoardData',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Speed Graph:' ,
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
second: '%Y-%m-%d %H:%M',
minute: '%Y-%m-%d %H:%M',
hour: '%Y-%m-%d %H:%M',
day: '%Y-%m-%d %H:%M',
week: '%Y-%m-%d %H:%M',
month: '%Y-%m-%d %H:%M',
year: '%Y-%m-%d %H:%M'
},
title: {
text: 'Date Time'
},
labels: {
rotation: 0
}
},
yAxis: {
title: {
text: 'Speed (km/h)'
},
min: 0
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 1
},
credits: {
enabled: false
},
series: [{
name: 'Speed Data'
}]
});
$("#SearchDb").click(function(){
var data = $(this).val();
var dateFrom=document.getElementById("beginDateInput").value;
dateTimeFromArray=dateFrom.split(" ");
dateFromArray=dateTimeFromArray[0].split("/");
var dateTo=document.getElementById("endDateInput").value;
dateTimeToArray=dateTo.split(" ");
dateToArray=dateTimeToArray[0].split("/");
mysqlDateFrom=dateFromArray[2]+"-"+dateFromArray[1]+"-"+dateFromArray[0]+" "+dateTimeFromArray[1];
mysqlDateTo=dateToArray[2]+"-"+dateToArray[1]+"-"+dateToArray[0]+" "+dateTimeToArray[1];
$.post('getSpeedGraph.php', {b: mysqlDateFrom,e:mysqlDateTo }, function(data){
chart.series[0].setData(["[1443843286000,0]","[1443843286000,100]","[1443843356000,200]","[1443843356000,0]","[1443843356000,100]","[1443843359000,0]","[1443843359000,0]","[1443843369000,0]","[1443843369000,0]","[1443843380000,0]","[1443843380000,0]","[1443843389000,0]","[1443843389000,0]","[1443843409000,0]","[1443843409000,0]","[1443843419000,0]","[1443843419000,0]","[1443843419000,0]","[1443843482000,0]","[1443843482000,0]","[1443843482000,0]","[1443843545000,17]","[1443843545000,20]","[1443843545000,20]","[1443843576000,0]","[1443843576000,0]","[1443843576000,0]","[1443843639000,7]","[1443843639000,7]","[1443843639000,6]","[1443843667000,0]","[1443843667000,0]","[1443843667000,0]","[1443843732000,0]","[1443843732000,0]","[1443843732000,0]","[1443843796000,0]","[1443843796000,0]","[1443843796000,0]","[1443843859000,0]","[1443843859000,0]","[1443843859000,0]","[1443843927000,0]","[1443843927000,0]","[1443843927000,0]","[1443843990000,0]","[1443843990000,0]","[1443843990000,0]","[1443844056000,0]","[1443844056000,0]","[1443844056000,0]","[1443844119000,0]","[1443844119000,0]","[1443844119000,0]","[1443844182000,0]","[1443844182000,0]","[1443844182000,0]","[1443844236000,0]","[1443844236000,0]","[1443844236000,0]","[1443844300000,0]","[1443844300000,0]","[1443844300000,0]","[1443844363000,0]","[1443844363000,0]","[1443844363000,0]","[1443844428000,0]","[1443844428000,0]","[1443844428000,0]","[1443844491000,0]","[1443844491000,0]","[1443844491000,0]","[1443844554000,0]","[1443844554000,0]","[1443844554000,0]","[1443844617000,0]","[1443844617000,0]","[1443844617000,0]","[1443844755000,0]","[1443844755000,0]","[1443844852000,0]","[1443844852000,0]","[1443844852000,0]","[1443844876000,0]","[1443844876000,0]","[1443844876000,0]","[1443844948000,0]","[1443844948000,0]","[1443844948000,0]","[1443845021000,0]","[1443845021000,0]","[1443845021000,0]","[1443845085000,10]","[1443845085000,10]","[1443845085000,9]","[1443845159000,0]","[1443845159000,0]","[1443845159000,0]","[1443845223000,0]","[1443845223000,0]","[1443845223000,0]","[1443845287000,0]","[1443845287000,0]","[1443845287000,0]","[1443845353000,0]","[1443845353000,0]","[1443845353000,0]","[1443845377000,13]","[1443845377000,12]","[1443845377000,11]","[1443845442000,0]","[1443845442000,0]","[1443845442000,2]","[1443845508000,0]","[1443845508000,0]","[1443845508000,0]","[1443845573000,0]"]);
}
);
}
);
});
});
I found the solution to a simple. Just for the help and reference of other just run this chart.series[0].setData( eval(data) );

Bars does not show up when loading JSON encoded data from PHP in Highcharts

I am loading data stored in a MySQL Database.
The chart doesn't show up on the webpage, but it doesn't show any other warning.
My PHP webpage returns a JSON encoded header with the following information:
["John Doe","2","Jane Doe","3"]
The script that loads the information is the following:
var chart;
function requestData() {
$.ajax({
url:'includes/bin/get_leaders.php',
success: function(point) {
alert(point);
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is longer than 20
chart.series[0].addPoint(point, true, shift);
},
cache: false
});
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'bar',
events: {load: requestData}
},
title: {
text: 'Top Agents'
},
xAxis: {
type: 'int',
title: {text: null}
},
yAxis: {
min: 0,
title: { text: 'Sales this Week', align: 'low'}
},
tooltip: {
formatter: function() {
return ''+ this.series.name +': '+ this.y +' sales';
}
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -100,
y: 100,
floating: true,
borderWidth: 0,
backgroundColor: '#FFFFFF',
shadow: false
},
credits: {
enabled: false
},
series: [{
name: 'Sales' }]
});
});
});
Any clue what's going on?
Thank you!
Looks like you are passing in a ["string", "number", "string", "number"].
What you want is {2, 3} for your series and then for your xAxis to use a category list of {"John Doe", "Jane Doe"}.

Categories