score:1
could construct the formatted series data to begin with like below:
<?php
/*
get data from the mysql database and return it in json format
*/
//setup global vars
$debug = $_get['debug'];
$format = $_get['format'];
if($format=='json'){
header("content-type: text/json");
}
$db = new mysqli('localhost', root, 'kudanil123', 'pt100', 3306);
if (mysqli_connect_error()) {
die('connect error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
if ($debug == 1) {echo 'success... ' . $db->host_info . "\n";}
// get data
$sql = "select meas_date,ai0_hist_value";
$sql .= " from ai0_hist";
$sql .= " where board_temp_hist_value > 30"; //filter out bad data
$sql .= " group by 1";
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order
$sql .= " limit 5;";
if ($result = $db->query($sql)) {
if ($debug == 1) {echo "fetched data! <br/><br/>";}
while($row = $result->fetch_array()){
$rows[] = $row;
}
foreach($rows as $row){
$seriesdata[] = [ strtotime($row['meas_date'])*1000, (float)$row['ai0_hist_value'] ];
}
echo (json_encode($seriesdata));
$result->close();
} else {
echo "error: " . $sql . "<br>" . $db->error;
}
$db->close();
score:1
this will generate the array you want, there is no need to do all that fiddling with the data from the database
// get data
$sql = "select meas_date,ai0_hist_value";
$sql .= " from ai0_hist";
$sql .= " where board_temp_hist_value > 30"; //filter out bad data
$sql .= " group by 1";
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order
$sql .= " limit 5;";
$rows = array();
if ($result = $db->query($sql)) {
while($row = $result->fetch_array()){
$rows[] = array(strtotime($row['meas_date'])*1000,
$row['ai0_hist_value']
);
}
}
echo json_encode($rows);
now you will need to convert the text to float in the javascript. this is because json is passed as text and not any other data type, so it has to be converted, if necessary in the receiving javascript.
score:2
if you want the code like that, you must fix the code:
while($row = $result->fetch_array()){
$rows[] = $row;
}
foreach($rows as $row){
$text[] = (float)$row['ai0_hist_value'];
$date[] = strtotime($row['meas_date'])*1000;
}
//$data[0] = $names;
$data1 = $date;
$data = $text;
$data2 = array($data1, $data);
//$data[2] = $text;
echo (json_encode($data2));
must be something like this:
while($row = $result->fetch_array()){
$rows[] = array(
(float)$row['ai0_hist_value'],
strtotime($row['meas_date'])*1000);
}
echo (json_encode($rows));
you were saving in $data2 an array with two arrays, the text and the data. you must save a row for each pair of 'text' and 'data'.
Source: stackoverflow.com
Related Query
- Generate JSON From Mysql Using PHP
- JSON data not showing in highstock Candlestick chart using PHP & MYSQL
- How to get data from multiple tables of MySQL DB and manuplate it using PHP to display graphs?
- Highcharts not being drawn in some cases when using data from MySQL (page elements created before database query complete?) PHP / MySQL
- HighCharts pie chart X-axies values are not displayed while trying to get data from MySQL database using PHP
- Highcharts drilldown json from php mysql
- Generate JSON files for Hightcharts with PHP from a TCP string
- Displaying highchart and using php to get data for the chart from mysql database
- Formatting JSON data monthwise for HighCharts using MySQL
- Using PhantomJS to create HighCharts grahps server side for use in PDF creation (PHP) - results in exit code 11 from PHPs exec();
- Passing a JavaScript function from JSON encoded PHP
- Highcharts Mysql Json arrays PHP
- Highcharts Mysql Json arrays PHP
- Highchart - Display JSON Data - MYSQL / PHP
- Highcharts multiple series json from php
- Generate image in a folder from Highchart in PHP
- How to generate highcharts chart from multiple local json files
- passing json values to highcharts from .net code behind
- Charts using Highcharts with multiple series from JSON
- How to send json to highcharts from php
- Generating Highcharts from MySQL data with PHP does not work
- Generating json data for StockChart from PHP
- load data from text file into HIGHCHART graphic using PHP
- I am using click event to trigger a new page to generate the drill down data from highcharts however the entire chart is being generated
- Having trouble JSON encoded data from MySQL through Highcharts
- Different approach to generate PDF of a HTML page which contains 10 highcharts using mPDF in PHP
- Returning MySQL table values from PHP script to Javascript function - Live Graphing
- highchart data from mysql using ajax
- Build a highchart chart using data from an external JSON
- highchart feed data from php json
More Query from same tag
- Sort descendants data in categories in highcharts
- Use classic mode instead of styled mode in Highcharts with Angular
- Angular multi select chart
- Highcharts xAxis labels formatter callback data empty
- Format "data" and "name" highchart mvc asp.net
- Highcharts Bar Graph's Tool Tip Point Format
- How to hide a highchars when there is no data to show?
- How to import Highmaps map collection in Angular 6
- Highcharts - areaspline with negative values, area should be always at the bottom
- Highcharts exporting hide data labels if number doesn't fit in stacked bar
- HighchartsJS getting data from URL and pass it to series data
- Highcharts exporting over HTTPS
- angular multiple same directives in single controller
- How do you generate dynamic <style> tag content in Angular template at runtime?
- High chart - Column Range Chart with Time range. How to get tooltip as Start and End time
- Highcharts not showing graph elements or xAxis
- Wrong x-dots in Highcharts
- Ajax JSON response into HighCharts
- Error on rendering Highstock date
- Format Highcharts xAxis for stock data
- Splitted bar chart for paired data with highcharts
- Highchart flags goes out when range selector is set to entire size
- Highmaps - Printing resizes the map on Chrome
- Javascript data loading issue
- Highcharts visualize, style series
- Extend Highcharts options as plugin
- R highcharter - Two barchart in same plot with different X-axis
- Trellis chart x categories values are not displaying correctly in Highcharts
- How do I dynamically change a data point in Highcharts using JavaScript
- R Highcharter Sankey nodes column property trouble