I'm new to web pages and dynamic content:
I have a server running php and I'm able to read in and display CSV text data - how may I graph this? I was using http://phpmygraph.abisvmm.nl/ but I'm unsure how to parse my CSV data into columns.
In addition, how do I create date-time objects in PHP (is this possible?)
A code example:
<?php
//Set content-type header
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Flow data site#3, John Radcliff Hospital';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Feb' => 25,
'Mar' => 0,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Sep' => 23,
'Oct' => 23,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseVerticalLineGraph($data, $cfg);
?>
This image shows an example of data I can generate on a webpage and the graph I would like to see - this is not a screen shot, only a 'wish list' of what I would like to generate:
if I can recommend, I would adapt your data and would use Google Charts
https://developers.google.com/chart/
Related
I have a table with a json column cast as array. The Schema creation has the column defined like this:
$table->json('row_ids');
In my class:
protected $casts = [
'row_ids' => 'array',
];
I generate the data in this array / column by getting every row id from another table like this:
$id_array = DB::table($table->name)->pluck('api_id')->toArray();
TableChannelRow::create([
'table_api_id' => $table->api_id,
'channel_api_id' => $channel->api_id,
'row_ids' => $id_array,
]);
When I dd a collection record I can see the columns in the target table OK, with one of the columns containing an array as expected:
#attributes: array:4 [▼
"api_id" => 2
"table_api_id" => 1
"channel_api_id" => 6
"row_ids" => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, ▶"
]
When I check in MySQLWorkbench the data looks like this:
In another controller I want to add or remove entries from the array in this column like this:
$table_channel_row = TableChannelRow::where('table_api_id', '=', $rowId)
->where('channel_api_id', '=', $channelId)
->first();
$row_ids = $table_channel_row->row_ids;
if ($is_checked == 'yes') {
// Add value to array if it does not already exist
if (!in_array($row_id, $row_ids)) {
array_push($row_ids, $row_id);
}
} else {
// Remove value from array
$row_id_array[] = $row_id;
$row_ids = array_diff($row_ids, $row_id_array);
}
$table_channel_row->update([
'row_ids' => $row_ids,
]);
Now the data in MySQLWorkbench looks like this:
Why does it get stored looking like a PHP array in the first instance, then later on update it gets stored as a json object?
Additionally the remove PHP code is working, yet the add is not, though it does not trigger an exception (you can see the first value is removed in the second image, but I cannot find it in the object in MySQL when I trigger the code to add it)
What did I miss? Thanks!
The reason why it's saving differently is because array_diff returns an associative array whereas your initial data is an indexed array. Take the following for example:
$ids = [1, 2, 3, 4, 5];
$ids2 = [1, 2, 3];
Then if you perform an array_diff($ids, $ids2), it would return the following:
[
3 => 4,
4 => 5
]
So if you want to save as the same format as your initial one, you have to retrieve the values of the array using array_values:
$row_ids = array_values(array_diff($row_ids, $row_id_array));
I've a PhpSpreadsheet process to export data to XLSX and PDF. I'm using TcPDF as PDF writer. But TcPDF has a problem to convert/translate border style.
Xlsx export
TcPDF export
Any idea to fix this?
I guess you have to use TcPDF's SetLineStyle() (see example 35):
$pdf->SetLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 4, 'color' => array(255, 0, 0)));
$text="DUMMY";
$pdf->Cell(0, 0, $text, 1, 1, 'L', 1, 0);
if you want to create complex border styles for individual cells check this
or see example 48 if you use HTML tables.
Alternatively, try SetLineWidth(1.0).
How do I customize the lines in the line charts on PHPPowerpoint/PHPPresentation? I can't find anything in there documentation or in the samples to figure this out.
Here's my code:
$seriesData = array(
'Monday' => 12,
'Tuesday' => 15,
'Wednesday' => 13,
'Thursday' => 17,
'Friday' => 14,
'Saturday' => 9,
'Sunday' => 7
);
$lineChart = new Line();
$series = new Series('Downloads', $seriesData);
$series->setShowSeriesName(false);
$series->setShowValue(false);
$series->setShowLeaderLines(false);
$lineChart->addSeries($series);
$shape = $currentSlide->createChartShape();
$shape->setResizeProportional(false)->setHeight(convertIn2Px(2.28))->setWidth(convertIn2Px(5.09))->setOffsetX(convertIn2Px(4.75))->setOffsetY(convertIn2Px(3.9));
$shape->getTitle()->setVisible(false);
$shape->getPlotArea()->setType($lineChart);
$shape->getPlotArea()->getAxisY()->setFormatCode('#,##0');
$shape->getLegend()->setVisible(false);
The color of the line graph comes up in blue but I would like to be able to change that color. It also shows the square markers but I would like to make it so that there are no markers on the line.
Thank you in advanced.
It's actually in the develop branch.
But you can do this :
$oOutline = new \PhpOffice\PhpPresentation\Style\Outline();
$oOutline->getFill()->setFillType(Fill::FILL_SOLID);
$oOutline->getFill()->setStartColor(new Color(Color::COLOR_YELLOW));
$oOutline->setWidth(2);
$series->setOutline($oOutline);
In these days i'm trying to draw a graph from a file using PhpMyGraph5.0,
on the autor's site (http://phpmygraph.abisvmm.nl/) there is this example file:
<?php
//Set content-type header
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Feb' => 25,
'Mar' => 0,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Sep' => 23,
'Oct' => 23,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseHorizontalLineGraph($data, $cfg);
?>
Because i need to get the input from a file i've modded the example file changing the $data assignment with:
$data = file("$PATH/$MYFILE");
I've formatted the text inside MYFILE and these are some lines of the file:
'00:00' => 19,
'00:05' => 19,
'00:10' => 21,
...
'17:10' => 21,
'17:15' => 21,
'17:20' => 21,
But when i try to draw the graph i obtain this message instead of the graph:
"exception `Exception` with message `The value of the key %s` is not numeric.`"
I've searched in PhpMyGraph5.0.php and i've found the test that throws the exception:
//Loop
foreach($data as $key => $value) {
//Test
if(!is_numeric($value)) {
throw new Exception('The value of the key "%s" is not numeric.');
}
...
I've tried to substitute the "throw Exception" with this cast:
$value=(int)$value;
but i obtain only an empty graph.
If i manually paste the content of MYFILE inside $data = array(PASTE_HERE); it works, but i can't do it manually.
I think that the problem is about the data type of the value, but i've no ideas on how to solve this problem.
Thanks to everyone and sorry for my bad english.
That exception seems to be badly coded, try changing it to this and it should give you the value of the key where it finds the value is not numeric, that may help identify where the error is :-
throw new Exception(sprintf('The value of the key "%s" is not numeric.',$key));
EDIT
Ok I see the problems, you are not getting what you think you are getting from the $data = file("$PATH/$MYFILE");
if you test using this
$data = file("$PATH/$MYFILE");
print_r($data);
You get the output :
Array
(
[0] => '00:00' => 19,
[1] => '00:05' => 19,
[2] => '00:10' => 21,
[3] => '17:10' => 21,
[4] => '17:15' => 21,
[5] => '17:20' => 21
)
So index [0] is actually an array and not a number, hence the error.
You are going to have to rethink the way you input your data.
Try this for size:
Change you data file to look like this
'00:00',19
'00:05',19
'00:10',21
'17:10',21
'17:15',21
'17:20',21
And your code to do this
$data = array();
$handle = fopen('tst.txt', 'r');
while (!feof($handle)) {
$line = fgets($handle, 8192);
list($time,$count) = explode(',',$line);
$data[$time] = $count;
}
fclose($handle);
print_r($data);
This will generate the following array
Array
(
['00:00'] => 19
['00:05'] => 19
['00:10'] => 21
['17:10'] => 21
['17:15'] => 21
['17:20'] => 21
)
Which I assume is what you wanted in the first place.
EDIT 2
Dont change the package, change what you send it
Replace this line
$data[$time] = $count;
With
$data[$time] = (int)$count;
That should do it.
I have an array which I'd like to show all days of weeks or each month of the year, even if data is 0, is it possible to look inside the array and fill in what's not there?
The data I'm returning from mysql table does not show 0 - details below
Show values even if empty
So, the following shows months of the year and the count, I'd like to fill in the months which aren't there with i.e. 'Feb' => '0' .. 'Sep' => '0' .. 'Dec' => '0'
Array example:
$data = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
Use array_fill_keys to create a "known good starting point" and then array_merge or array addition to incorporate your data.
Example:
$data = array_fill_keys(array('Jan', 'Feb', 'etc'), 0);
$data = array('Feb' => 40) + $data;
Caveat: the result will not end up being ordered by month.
First of all create a blank array with default 0 value than merge this array to your original array.
$data = array(
'Jan' => 0,
'Feb' => 0,
'Mar' => 0,
'Apr' => 0,
'May' => 0,
'Jun' => 0,
'Jul' => 0,
'Aug' => 0,
'Sep' => 0,
'Oct' => 0,
'Nov' => 0,
'Dec' => 0;
);
$data2 = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
$newarray=array_merge($data, $data2);
As already pointed out in the previous question, you need create some kind of "lookup table" for the month names. If you do not do that within the database, you need to do it in PHP. Such a "table" could be an array of month names:
$months = array(
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
);
You can then iterate over the month names an pick the number if it exists in the result or you take the default value:
$default = 0;
foreach ($months as $month) {
$value = isset($data[$month]) ? $data[$month] : $default;
# $value now contains the month value
...
}
This should just work.
Take care that the month names that the database returns need to be the same you use in your $months array.