Graphing Dates with pchart - php

I am trying to use date values for the xaxis but cannot find any documentation on how to do so.
I found this for the old version of pchart but the function calls are not the same in the new version of pchart.
http://pchart.sourceforge.net/documentation.php?topic=advexemple20
For example my Y values are 10, 20, 30 and my X values are 2012-03-31, 2012-04-20, 2012-05-25. I cannot figure out how to add the dates using the addpoints.
I've read that you are to use the unix time stamp but then I get lost with how to format the xaxis in pcharts.

Related

PhpSpreadsheet passing date format to chart

I use PhpSpreadsheet to create XLSX files. The problem is that I set the date inside the cells with a formatCode but when I create a chart based on these cells the formatCode is not applied on them. This leads to having timestamps inside the chart and a proper date format inside the cells. Does anyone know how to pass the right date format into the chart?
Best regards,
laobiz
You may need to apply date formatting to the cell, e.g. :
$worksheet->getStyleByColumnAndRow($column, $row)
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME);
See supported date formats here.
Please note, that MS Excel does not work with unix timestamps like PHP (meaning the number of seconds since 01.01.1970). Instead, Excel counts
the number of days since 01.01.1900 and works with float values when converting to hours/minutes/secs. Therefore you cannot insert into a cell unix timestamp, but convert it into Excel timestamp before. See PHP conversion functions from and into Excel serial date format.

Time in x-axis not showing correctly for graph in flot js

I'm plotting graph using flot js. Now date stored in the database is in this form.
2015-10-29 11:35:33
I convert it to miliseconds, pass to ajax to plot the graph and that graph ought to display date in x-axis not in miliseconds but in readable date.
This is how its shown presently,
If you notice the x-axis, I believe its only showing the time as value for y are all for the same day, same hour but slightly difference in minutes.Please take a look at below table that shows the value (total_bv) and dates (as_of_date).
Problem is, date the date shown in x-axis. what does that 03:3, 03:38 and so forth about. I think its the time which doesn't converted to correct time zone?
Because in the PHP script where I get the data from database, I use timezone asia/kuala lumpur. If I don't use this timezone the time displayed in x-axis starts with 10:36, 10:38 and so forth.. SO can anyone tell me first how to show the correct Date & time in x-axis in readable format?
date_default_timezone_set("Asia/Kuala_Lumpur");
$acceptedUser = new search();
$sales = $acceptedUser->get_sales_graph();
$before = array();
foreach($sales as $k=>$v)
{
$date = strtotime($v['as_of_date']) * 1000;
array_push($before, array("datey" => $date, "bv" => $v['total_bv']));
}
echo json_encode($before);
So it seems you have two issues:
The conversion of date/time strings from your database to JavaScript millisecond timestamps, paying attention to time zones.
Displaying some kind of date/time string for your tick values.
Both of these are addressed by the flot.js documentation.
First of all, flot assumes that timestamps are UTC. If you want to change that, you can do so through setting the axis' timezone property to browser (converts timestamps according to the user's browser), or you can use timezone.js. However most devs just pretend that the timestamp they are using is already in the correct timezone anyway (which is essentially what you're doing and have noticed).
Second, use an explicit format string for the timeformat property with the axis ticks. The documentation has the full list of replaceable format specifiers.

Displaying jquery graph data according to date

I am using jQuery flot for plotting graphs in my PHP page. My page retrieves data from PostgreSQL database and plot the values on the graph having date on x axis and cost on y axis.
Problem Statement: I have data according to julian date in my db but it may not be starting from 1st day of the month. Now my plot shows data for 30 days of the month and I want to plot the values retrieved from db on correct coordinate and not from starting of the x axis coordinate 0. The days previous to the one retieved from db should show 0 or no values.
Requirement: Any logic for displaying this data dynamically on correct coordinate as the plot function exposed by jQuery flot requires manual mapping of coordinates and values.
I'm not sure if I understand your question, does this help?
j$.plot(j$("#placeholder"),[d1],{
xaxis:
{ mode: "time",
min: <?php echo $start_data;?>,
max: 30
}
}

Set correct reading format for a cell with PHPExcel

I'm using PHPExcel to read an Excel 2007 document. In this document there's a column with "Time" formatting and it displays data correctly in Excel. When I try to parse this column I get some weird values. Any idea how can I fix this?
Thank you.
When parsing, you need to specify the format of the column. e.g.
$my_cell = $objWorksheet->getCellByColumnAndRow(1, 1);
$my_cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
print $my_cell_value;
Weird values?!? You mean you get a number instead of a human-readable date string.... it always helps to describe things accurately where possible, and weird isn't really an accurate description.
If you simply get the value from the cell, you'll be reading a raw timestamp value (MS Excel holds date and time values as a timestamp; like a Unix timestamp value in PHP, except that Excel's timestamp is the number of days since 1st January 1900 (or 1st January 1904, depending on which calendar it is configured to use).
MS Excel uses number format masking to display this timestamp as a human-readable date/time string.
You can use the getFormattedValue() method rather than simple getValue() to retrieve this as a formatted date/time (getFormattedValue() applies any number format mask for the cell to the value).
Not ethat if you've loaded the file with readDataOnly set to TRUE, then the number format masks aren't loaded, so PHPExcel cannot identify whetehr a cell contains a date or not.
Or, as James has suggested in his answer, you can convert the raw timestamp to a formatted value manually by applying number formatting with your own format mask
A third alternative is that you can use PHPExcxel's built-in date handling functions to convert this Excel timestamp value to a PHP/unix timestamp or to a PHP DateTime object (PHPExcel_Shared_Date::ExcelToPHP() and PHPExcel_Shared_Date::ExcelToPHPObject() respectively),
that you can then display using PHP's own date handling functions
The value for Time as it is stored in Excel is actually the fraction of the day, so 0.2 is 24 hours (or 1440 minutes or 86400 seconds) times 0.2. You can calculate the time of day based on that information, and then calculating from the beginning of the day. It makes it a little more usable than a formatted time, but a lot less readable.

jpgraph x axis scale

how can i tell jpgraph to show x-axis ticks every month? I have two charts, in one i show results from 1 year and on other i show results from beginning to end. On year graph it shows weekly results and i like it but in case of showing bigger date range (in my case from 1.4.2010. till 10.1.2013. it show tick only every one year so i have tick on 1.4.2010, 1.4.2011, ...
Important part of the code where i define x-axis properties:
$graph->xaxis->SetTickLabels($timestamp);
$graph->xaxis->scale->SetDateAlign(MONTHADJ_1);
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
$graph->xaxis->SetLabelAngle(-45);
$graph->xaxis->SetLabelMargin(2);
$graph->xaxis->SetLabelAlign('left','top');
$graph->xaxis->SetLabelFormatString('d.m.Y',true);
$graph->xaxis->HideFirstLastLabel();
Timestamp array is unix timestamp values converted from mysql dates,and timestamps are correct. I checked them. Also results in the graphs are fine, only i want more ticks on x axis.
To answer my own question,and close it, here is what i added (combined with old xaxis properties):
require_once ('jpgraph/jpgraph_utils.inc.php');
$dateUtils = new DateScaleUtils();
list($tickPos,$minTickPos) = $dateUtils->getTicks($timestamp,DSUTILS_MONTH1);
$graph->xaxis->SetTickPositions($tickPos,$minTickPos);
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
$graph->xaxis->SetLabelAngle(-45);
$graph->xaxis->SetLabelMargin(2);
$graph->xaxis->SetLabelAlign('left','top');
$graph->xaxis->SetLabelFormatString('d.m.Y',true);
$graph->xaxis->HideFirstLastLabel();
Also i changed scale of graph to intlin, it was datlin before.

Categories