问题
This is a example of working multi-line stock chart. Well only one small problem is - the date is moved by -1 day somehow comparing to the mysql result ( but that is not the point now ).
I wanted to create a dynamic stock generator supplied by a form. The form has some checkboxes that allows us to pick what series we want to be generated.
#Questions & Problems #1 PHP side : Ok lets say we picked from checkbox 1 and 3. What we need to do is to post them to the generator file right? Inside the generator file there would be something like
if($_POST['gen_fields'] == 1){ $return['name'] = 'Zwroty';
foreach ($result as $row) {
$date =strtotime($row['Date'])*1000;
$return['data'][] = array($date, (int)$row['totalCOunt']);
// $return[] =
}
}
Well I tried that but seems to not been working somehow. Lets move on to the JS question.
#2 JS side : Javascript wise its also somehow hard to achieve it has to be something like a if statement : if checked checkbox 1 add to series. If no series selected error or add all 3.
Second option I saw some dynamic foreach series scripts on the internet but it seems that they wasn't working with my generator because I had no if $_Get's
<form id="form_gen" method="post" class="bootstrap-frm" style="max-width: 100% !important;">
<label>
<span>Parameters</span>
<input type="checkbox" name="gen_fields" value="1"> Zwroty
<input type="checkbox" name="gen_fields" value="2"> Doręczenia
<input type="checkbox" name="gen_fields" value="3"> W doręczeniu
</label>
<label>
<span> </span>
<input type="button" value="Generate" class="add-button" onclick="form_gen()">
</label>
</form>
JS section
function form_gen() {
$('#form_gen').form('submit', {
url: 'api/stockchart.php',
onSubmit: function() {
},
beforeSend: function() {
},
success: function(result) {
$.getJSON('api/stockchart.php', function(json) {
// Create the chart
$('#as').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'Wpisów dziennie'
},
yAxis: {
title: {
text: 'Ilość'
},
plotLines: [{}]
},
series: [{
name: 'Zwroty',
color: '#4572A7',
type: 'column',
// yAxis: 1,
data: json[1].data
}, {
name: 'Doręczenia',
color: '#45CCA7',
type: 'column',
// yAxis: 1,
data: json[2].data
}, {
name: 'W doręczeniu',
color: '#89A54E',
type: 'spline',
data: json[0].data
}]
});
});
}
});
}
The PHP generator
try {
$sth = $db->prepare("SELECT DATE(date) Date, COUNT(number) totalCOunt FROM numbers GROUP BY DATE(date)");
$sth->execute();
$result = $sth->fetchAll();
$sth = $db->prepare("SELECT DATE(rdate) Date, COUNT(number) totalCOunt FROM numbers Where `return` = 2 AND rdate != '0000-00-00 00:00:00' GROUP BY DATE(rdate)");
$sth->execute();
$returna = $sth->fetchAll();
$sth = $db->prepare("SELECT DATE(rdate) Date, COUNT(number) totalCOunt FROM numbers Where `return` = 1 AND rdate != '0000-00-00 00:00:00' GROUP BY DATE(rdate)");
$sth->execute();
$returnb = $sth->fetchAll();
/* Fetch all of the remaining rows in the result set */
// print("Fetch all of the remaining rows in the result set:\n");
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());}
try{
$return['name'] = 'Zwroty';
foreach ($result as $row) {
$date =strtotime($row['Date'])*1000;
$return['data'][] = array($date, (int)$row['totalCOunt']);
// $return[] =
}
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());}
try{
$return1['name'] = 'W doręczeniu';
foreach ($returna as $row1) {
$date1 =strtotime($row1['Date'])*1000;
$return1['data'][] = array($date1, (int)$row1['totalCOunt']);
// $return[] =
}
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());}
try{
$return2['name'] = 'Doręczony';
foreach ($returnb as $row2) {
$date2 =strtotime($row2['Date'])*1000;
$return2['data'][] = array($date2, (int)$row2['totalCOunt']);
// $return[] =
}
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());}
$result = array();
array_push($result,$return);
array_push($result,$return1);
array_push($result,$return2);
echo json_encode($result,true);
来源:https://stackoverflow.com/questions/26484278/dynamically-generated-stockchart-form-checkboxes-generate-my-chart