jqPlot stackSeries: true, Uncaught TypeError: Cannot read property '0' of undefined - charts

I want to display 3 data series.
Whenever I set the stackSeries: true option in the I get the
Uncaught TypeError: Cannot read property '0' of undefined
or
Uncaught TypeError: Cannot read property '1' of undefined
when trying to display data.
When I remove the stackSeries: true attribue then the chart is rendered. What's wrong?
Update
$(document).ready(function(){
var bar1 = [
<?php
if(!empty($bar1)){
$i = 0;
foreach($bar1 as $bar){
$i++;
if(!empty($bar['jobs']['date_published'])){
if($i == count($bar1))
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
else
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
}
}
}
?>
];
var bar2 = [
<?php
if(!empty($bar2)){
$i = 0;
foreach($bar2 as $bar){
$i++;
if(!empty($bar['jobs']['date_published'])){
if($i == count($bar2))
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
else
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
}
}
}
?>
];
var bar3 = [
<?php
if(!empty($bar3)){
$i = 0;
foreach($bar3 as $bar){
$i++;
if(!empty($bar['jobs']['date_published'])){
if($i == count($bar3))
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].']';
else
echo '[\''.$bar['jobs']['date_published'].'\', '.$bar[0]['count'].'], ';
}
}
}
?>
];
<?php
if(!empty($bar1) && !empty($bar2) && !empty($bar3))
echo 'var labels = ["Bar 1", "Bar 2", "Bar 3"];';
else if(!empty($bar1) && !empty($bar2))
echo 'var labels = ["Bar 1", "Bar 2"];';
else if(!empty($bar2) && !empty($bar3))
echo 'var labels = ["Bar 2", "Bar 3"];';
else if(!empty($bar1) && !empty($bar3))
echo 'var labels = ["Bar 1", "Bar 3"];';
else if(!empty($bar1))
echo 'var labels = ["Bar 1"];';
else if(!empty($bar2))
echo 'var labels = ["Bar 2"];';
else if(!empty($bar3))
echo 'var labels = ["Bar 3"];';
?>
optionsObj = {
//stackSeries: true,
seriesColors:[<?php if(!empty($bar1)) echo '"#4bb2c5",';
if(!empty($bar2)) echo '"#F78181",';
if(!empty($bar3)) echo '"#9AFE2E",';
echo '"#4bb2c5"'; ?>],
animate:true,
legend:{
show:true,
location:"sw",
rowSpacing:"0px",
labels:labels
},
title:{
text:"Jobs",
fontSize:"12px"
},
seriesDefaults:{
showMarker: true,
pointLabels: { show: true },
renderer: $.jqplot.BarRenderer,
rendererOptions: {
barWidth: 2
}
},
axes:{
xaxis:{
pad:1,
tickOptions:{
fontSize:"10px"
},
renderer:$.jqplot.DateAxisRenderer
},
yaxis:{autoscale: true}
},
highlighter: {show: true},
grid:{drawGridlines:true}
};
<?php $empty = false;
if(!empty($bar1) && !empty($bar2) && !empty($bar3)){ ?>
$.jqplot('chart1', [bar1, bar2, bar3], optionsObj);
<?php } else if(!empty($bar1) && !empty($bar2)){ ?>
$.jqplot('chart1', [bar1, bar2], optionsObj);
<?php } else if(!empty($bar2) && !empty($bar3)){ ?>
$.jqplot('chart1', [bar2, bar3], optionsObj);
<?php } else if(!empty($bar1) && !empty($bar3)){ ?>
$.jqplot('chart1', [bar1, bar3], optionsObj);
<?php } else if(!empty($bar1)){ ?>
$.jqplot('chart1', [bar1], optionsObj);
<?php } else if(!empty($bar2)){ ?>
$.jqplot('chart1', [bar2], optionsObj);
<?php } else if(!empty($bar3)){ ?>
$.jqplot('chart1', [bar3], optionsObj);
<?php } ?>
});

I found that if my dataset has discontinuities in it anywhere, then a stacked graph will throw an error.
For example, this data series will throw an error:
[
[[0,22],[1,22],[2,22]],
[[0,33],[2,33]],
]
this one will not:
[
[[0,22],[1,22],[2,22]],
[[0,33],[1,33],[2,33]],
]
Note the difference between the two. The first one doesn't have a data point at X=1 for the second series. The fix would be to make sure all your series have values at all the possible X values, setting any missing ones to zero:
[
[[0,22],[1,22],[2,22]],
[[0,33],[1,0],[2,33]],
]

Related

Severity: Notice Message: Undefined variable: data Filename: frontend/v_grafik.php Line Number: 17

CodeIgniter, Message: Undefined variable, Passing data
Controller
function v_grafik() {
// error_reporting(0);
$iklan = $this->Tb_iklan_model->get_all();
$berita = $this->Tb_berita_model->get_all();
$x['data'] = $this->M_grafik->get_data_stok();
if ($x) {
$data = array(
'x' => $x,
'berita' => $berita,
'iklan' => $iklan,
'content' => "frontend/v_grafik",
);
// $content = $this->load->view('frontend/v_grafik',$x);
$this->load->view('layout/frontend', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('frontend'));
}
}
View
<?php
foreach($data as $data){
$merk[] = $data->merk;
$stok[] = (float) $data->stok;
}
?>
<canvas id="canvas" width="1000" height="280"></canvas>
<!--Load chart js-->
<script type="text/javascript" src="<?=base_url()?>assets/vendor/chartjs/Chart.js"></script>
<script type="text/javascript" src="<?=base_url()?>assets/vendor/chartjs/Chart.min.js"></script>
<script>
var lineChartData = {
labels : <?php echo json_encode($merk);?>,
datasets : [
{
fillColor: "rgba(60,141,188,0.9)",
strokeColor: "rgba(60,141,188,0.8)",
pointColor: "#3b8bba",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(152,235,239,1)",
data : <?php echo json_encode($stok);?>
}
]
}
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);
</script>
This eror : A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: frontend/v_grafik.php
Line Number: 17
Backtrace:
File: C:\xampp\htdocs\project\application\views\frontend\v_grafik.php
Line: 17
This Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: frontend/v_grafik.php
Line Number: 17
Can help me?
Why would you do... In your view
foreach($data as $data)
You are changing what $data is and bad things will happen. So you need to change one of them. In this "example" I have changed the 2nd occurrence of $data to $info.
foreach($data as $info){
$merk[] = $info->merk;
$stok[] = (float) $info->stok;
}

Bundle products doesn't appear in magento 2

i have create one product with Bundle Product
if i use the theme Bundle Product is not showing in the page and i am getting below error
main.CRITICAL: Notice: Object of class Magento\Framework\Pricing\Amount\Base could not be converted to int in C:\xampp\htdocs\Magento2\app\design\frontend\Megnor\mag110246_5\Magento_Catalog\templates\product\list.phtml on line 83 [] []
if i use the default theme at that time i am getting the Productenter image description here
enter image description here
Please replace below code in list.phtml
File Path:-C:\xampp\htdocs\Magento2\app\design\frontend\Megnor\mag110246_5\Magento_Catalog\templates\product\list.phtml
line 75-101 and 212-238
https://prnt.sc/qmvgmx
https://prnt.sc/qmvgs9
<?php
if ($_product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
$specialprice = $_product->getSpecialPrice();
$specialPriceFromDate = $_product->getSpecialFromDate();
$specialPriceToDate = $_product->getSpecialToDate();
$today = time();
if ($specialprice) {
if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) {
$specialprice = $_product->getSpecialPrice();
$price = $_product->getPrice();
$finalPrice = $_product->getFinalPrice();
if($price >= $finalPrice){
$sale = round( ($price - $specialprice) * 100 / $price);
}
?>
<span class="sale-label">
<?php if($sale) { echo '-'.$sale.'%'; } else {echo __('Sale');} ?>
</span>
<?php }
}
}
?>

Google Charts -- Axis Values overlapping and how to avoid

How do I avoid this axis overlap in google charts? I am working with a large data set and not sure what to do to solve this issue. I am using a large number of dates for the x axis. The options I use for my chart are
var options = {
tooltip: {
trigger: 'both',
},
width: 1900,
height: 400,
vAxis: { 'title': 'Volume' },
crosshair: { trigger: 'both'}
};
EDIT::
PHP creation of the containers
if( isset($db_graph_query)){
while($row = mysqli_fetch_array($db_graph_query)) {
$rowcount2++;
// removed the hard coded column set and made it driven off of the array below
// could have pulled it from the $cols array above, but there might be columns that we don't wish to show
echo " <tr>";
$colindex = 0;
foreach( $cols as $column_name ) {
$style = "";
$val = $row[$column_name];
if ( isset($column_callback)) {
$style=$column_callback($colindex, $val);
}
if ($colindex == 0) {
echo "<td style='text-align: left; width: 1pt;'><a href='#' class='toggle' onClick='drawChart(\"$val\");'>$val</a></td>";
$tempval = $val;
} else {
echo "<td $style>$val</td>";
}
$colindex++;
}
echo "</tr>";
echo "<tr class='tablesorter-childRow'>";
echo "<td colspan='12'>";
echo "<div id='$tempval' style='height: 400px;'></div>";
echo "</td>";
echo "</tr>";
}
}
EDIT::
Draw Chart Script, creates the chart options, grabs data from SQL DB and adds into the data:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
function drawChart(inputname) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'RunDate');
data.addColumn('number', 'Runs');
data.addColumn('number', 'Fails');
data.addRows([
<?php
$dbName = "mydb";
$config = parse_ini_file("configfile.ini",true);
$dbUser = $config["DB"]["db_user"];
$dbServer = $config["DB"]["db_ip"];
$dbPassword = $config["DB"]["db_pass"];
$con = mysql_connect($dbServer, $dbUser, $dbPassword);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbName, $con);
$sql = mysql_query("SELECT * From MyTestTable");
$output = array();
while($row = mysql_fetch_array($sql)) {
// create a temp array to hold the data
$temp = array();
// add the data
$temp[] = '"' . $row['Name'] . '"';
$temp[] = '"' . $row['RunDate'] . '"';
$temp[] = (int) $row['Runs'];
$temp[] = (int) $row['FailCount'];
// implode the temp array into a comma-separated list and add to the output array
$output[] = '[' . implode(', ', $temp) . ']';
}
// implode the output into a comma-newline separated list and echo
echo implode(",\n", $output);
mysql_close($con);
?>
]);
var view = new google.visualization.DataView(data);
view.setRows(data.getFilteredRows([
{column: 0, value: inputname}
]));
view.setColumns([1,2,3]);
var options = {
tooltip: {
trigger: 'both',
},
vAxis: { 'title': 'Volume' },
crosshair: { trigger: 'both'},
width: 1900,
height: 400
};
var chart = new google.visualization.LineChart(document.getElementById(inputname));
chart.draw(view, options);
}
</script>
try using option for slanted text...
hAxis: {slantedText: true}
The horizontal axis ticks should not be overlapping at all, and I believe the only way this could be happening is if your chart is not visible at the time you draw it, in which case the chart thinks the ticks don't take up any space and can be packed in as dense as possible. Slanting or alternating would not necessarily help until you get down to the minimum spacing constraint.
So the only real solution, at this time, is to make sure your chart is visible when you draw it.

How put dates on flot

I've a little problem, I need to do a curve with on y axis numbers and x axis dates.. but I can't display some dates...
My code :
<script type="text/javascript">
$(function () {
<?php
echo " var data = [";
$cpt="0";
include ('../connect.php');
// Requete SQL
$req = 'select "SPP_NB_IND" from "STAT_PERPHY" where "SPP_SAGES" = \''.$sages.'\' AND "SPP_DATE" between \''.$jourtableau.' 00:00:00\' and \''.$jourfinw.' 23:59:59\'';
$res = pg_query($req);
$reqd = 'select "SPP_DATE" from "STAT_PERPHY" where "SPP_SAGES" = \''.$sages.'\' AND "SPP_DATE" between \''.$jourtableau.' 00:00:00\' and \''.$jourfinw.' 23:59:59\' AND "SPP_NB_IND" IS NOT NULL ';
$resd = pg_query($reqd);
// On met les valeurs obtenues dans un tableau
while ( $row = pg_fetch_assoc ($res) )
{
//echo $row['SPP_NB_IND']."<br>";
$var=$row['SPP_NB_IND'];
while ( $roww = pg_fetch_assoc ($resd) )
{
$abscisse=date('d-m', strtotime($roww['SPP_DATE']));
}
echo "[$abscisse, $var],";
$cpt++;
}
echo "];";
?>
var options = {
lines: {
show: true
},
points: { show: true
},
xaxis: {
mode: "time",
timeformat : "%d/%m"
}
};
<?php
echo "$.plot($(\"#graph1\"), [ data ], options);";
?>
});
</script>
[/CODE]
When I put $abscisse, my curve is vertical and if I put $cpt, I have a "normal" curve... but I want to see dates corresponding with numbers..
Freindly,
Tanaes.
See the documentation:
You have to give flot timestamps, not already formated dates. For PHP use something like
$abscisse = strtotime($roww['SPP_DATE']) * 1000;

ZF2 - loop the loop

In my model I have:
public function fetchForParentId($id)
{
$resultSet = $this->tableGateway->select(array('acl_menu_parent_id' => $id));
$resultSet->buffer();
$resultSet->next();
return $resultSet;
}
and controller:
$sm = $this->getServiceLocator();
$this->layout()->acl_menu = $sm->get('AdminSettings\Model\AclMenuTable');
In my View I can use model function:
<?php
$rows = $this->acl_menu->fetchForParentId(0);
foreach ($rows as $item):
echo $item->acl_menu_name.'<br>';
endforeach;
?>
It works, but if I try to run this:
<?php
$rows = $this->acl_menu->fetchForParentId(0);
foreach ($rows as $item):
echo $item->acl_menu_name.'<br>';
$subRows = $this->acl_menu->fetchForParentId($item->acl_menu_id);
foreach ($subRows as $subItem):
echo ' - '.$subItem->acl_menu_name.'<br>';
endforeach;
endforeach;
?>
Second foreach doesn't work, $rows->acl_menu_id exists and in database there are correct rows to show.
I do not know why it happens.
Shouldn't it be $item instead of $rows:
$subRows = $this->acl_menu->fetchForParentId($item->acl_menu_id);