Fusionchart , XML , render fault - php

I have the following script that creates an XML string for rendering a chart with Fusionchart
<?php
include("includes/fusioncharts.php");
$chart = new SimpleXMLElement("<chart></chart>");
$chart->addAttribute('caption', 'testgrafiek');
$chart->addAttribute('subcaption', 'onderlabel');
$chart->addAttribute('xaxisname', 'naam');
$chart->addAttribute('yaxisname', 'aantal ronden');
$categories = $chart->addChild('categories');
$category[0] = $categories->addChild('category');
$category[1] = $categories->addChild('category');
$category[0] ->addAttribute('label', 'alias1');
$category[1]->addAttribute('label', 'alias2');
$dataset1 = $chart->addChild('dataset');
$dataset1->addAttribute('seriesname', 'Revenues');
$dataset1det[0] = $dataset1->addChild('set');
$dataset1det[0] ->addAttribute('value', '16000');
$dataset1det[1] = $dataset1->addChild('set');
$dataset1det[1] ->addAttribute('value', '18000');
$dataset2 = $chart->addChild('dataset');
$dataset2->addAttribute('seriesname', 'Profits');
$dataset2->addAttribute('renderas', 'line');
$dataset2det[0] = $dataset2->addChild('set');
$dataset2det[0] ->addAttribute('value', '1000');
$dataset2det[1] = $dataset2->addChild('set');
$dataset2det[1] ->addAttribute('value', '8000');
$columnChart = new FusionCharts("column3D", "myFirstChart" , 700, 400, "chart-1", "xml", $chart);
$columnChart->render();
Header('Content-type: text/xml');
echo $chart->asXML();
?>
<div id="chart-1"><!-- Fusion Charts will render here--></div>
If I run the code above, I have an error on the page like
This page contains the following errors:
error on line 7 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
If I run the code without
$columnChart = new FusionCharts("column3D", "myFirstChart" , 700, 400, "chart-1", "xml", $chart);
$columnChart->render();
and
I have no errors on the page and see a correct XML.
Don't find how to solve this.
Someone?

If you want to mix text/html output with XML, then you need to format the XML yourself and HTML encode it so the browser doesn't try to interpret the tags.
Firstly you need to understand that what type of header you are using there so that output should be in that format.
Example :
header("Content-Type: text/xml;"); //gives the output in xml format
header("Content-Type: text/php;"); //gives the output in php format
header("Content-Type: text/html;"); //gives the output in html format
I have used your code and have got rid of the issue you were facing, it was just that to see the output in the html format, you have to use header("Content-Type: text/html;");
The issue has been solved but your data is insufficient to get a chart, so by using a Sufficient data you will be able to see your desired chart.

Related

php returns invalid json

The following php code returns an invalid json error not sure why
<?php
include("dbConnect.php");
$sql = "SELECT QID, Question, Answer,CatID FROM Questions";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('qid'=>$row[0],
'question'=>$row[1],
'answer'=>$row[2],
'catid'=>$row[3]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
when I run the link to the php it returns this data:
{"result":[{"qid":"1","question":"Question 1","answer":"Answer 1","catid":"1"},{"qid":"2","question":"Question 2","answer":"Answer 2","catid":"2"},{"qid":"3","question":"Question 3","answer":"Answer 3","catid":"3"},{"qid":"4","question":"Question 4","answer":"Answer 4","catid":"1"},{"qid":"5","question":"Question 5","answer":"Answer 5","catid":"3"},{"qid":"6","question":"Question 6","answer":"Answer 6","catid":"3"}]}
and I tried running the link to the php that returns the json data using this json formatter website
I get these errors:
Error:Invalid media type, expecting application/json.[Code 28, Structure 0]
Error:Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.[Code 29, Structure 0]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 114]
Error:Invalid characters found.[Code 18, Structure 114]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 116]
Error:Invalid characters found.[Code 18, Structure 116]
If I try copying the resulting json data I get a valid JSON format but when I try running it from the php link that is stored on my server I get the above errors
UPDATE:
link to php
The problem isn't the JSON string, it's the header data.
You must specify the Content-Type header manually, otherwise it will send the output as text/html:
<?php
include("dbConnect.php");
$sql = "SELECT QID, Question, Answer,CatID FROM Questions";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('qid'=>$row[0],
'question'=>$row[1],
'answer'=>$row[2],
'catid'=>$row[3]
));
}
header("Content-Type: application/json; charset=utf-8");
echo json_encode(array("result"=>$result));
mysqli_close($con);
Add header('Content-Type: application/json', true); to your code before echo json_encode () to set the correct mime type.
The other error messages from the validator website are curious; i cannot see invalid characters or fields that are not wrapped in double quotes. Maybe, the validator website has some bugs.
add header in php file..
header("Content-Type: application/json; charset=UTF-8");

phppowerpoint pie chart exploding

I have a project in which data are show in pie chart. It can export the pie chart to powerpoint from php. But the pie chart is show with by exploding. I want to show it without exploding it. Is it possible?
I am using PowerPoint2007. And the code is as following -
<?php
require_once 'iconnect.php';
require_once 'QueryHandler.php';
require_once 'ResultTypes.php';
include_once 'Sample_Header.php';
use PhpOffice\PhpPowerpoint\PhpPowerpoint;
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\Pie3D;
use PhpOffice\PhpPowerpoint\Style\Fill;
use PhpOffice\PhpPowerpoint\Style\Color;
use PhpOffice\PhpPowerpoint\Style\Shadow;
use PhpOffice\PhpPowerpoint\Shape\Chart\Series;
use PhpOffice\PhpPowerpoint\Style\Border;
use PhpOffice\PhpPowerpoint\Style\Alignment;
use PhpOffice\PhpPowerpoint\IOFactory;
Connect(3);
global $linkid;
$queryHandler=new QueryHandler();
$query="SELECT sku,value ".
"FROM ferrero_booker t1,booker_sku t2 ".
"WHERE t1.skuID=t2.skuID ".
"LIMIT 10";
$result = $queryHandler->runQuery($query,$linkid,ResultTypes::$TYPE_ARRAY);
$data=array();
for($i=0;$i<count($result);$i++)
{
$id=$result[$i][0];
$data[$id]=(float)$result[$i][1];
}
$objPHPPowerPoint = new PhpPowerpoint();
$objPHPPowerPoint->getProperties()->setCreator('PHPOffice')
->setLastModifiedBy('PHPPowerPoint Team')
->setTitle('Sample 07 Title')
->setSubject('Sample 07 Subject')
->setDescription('Sample 07 Description')
->setKeywords('office 2007 openxml libreoffice odt php')
->setCategory('Sample Category');
$objPHPPowerPoint->removeSlideByIndex(0);
$currentSlide = createTemplatedSlide($objPHPPowerPoint);
$pie3DChart = new Pie3D();
$series = new Series('', $data);
$series->setShowSeriesName(true);
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4672A8'));
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFAB4744'));
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8AA64F'));
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF725990'));
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4299B0'));
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFDC853E'));
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF93A9CE'));
$pie3DChart->addSeries($series);
$shape = $currentSlide->createChartShape();
$shape->setName('PHPPowerPoint Daily Downloads')
->setResizeProportional(false)
->setHeight(550)
->setWidth(700)
->setOffsetX(120)
->setOffsetY(80);
$shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getTitle()->setText('Booker Sales');
$shape->getTitle()->getFont()->setItalic(true);
$shape->getPlotArea()->setType($pie3DChart);
$shape->getView3D()->setRotationX(90);
$shape->getView3D()->setPerspective(90);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);
$oWriterPPTX = IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$oWriterPPTX->save("sample.pptx");
print_r($data);
?>
I haven't seen a way to do it in the code, but it's easy to modify the source. Whether you should is another matter, but I won't touch that topic here.
The pie chart explosion is controlled by an "explosion" tag written to the xml, and this happens in the writer. Specifically this file (on my installation/version):
PHPPowerPoint/src/PhpPowerpoint/Writer/PowerPoint2007/Chart.php
Within that file, the following lines add the explosion effect:
// c:explosion
$objWriter->startElement('c:explosion');
$objWriter->writeAttribute('val', '20');
$objWriter->endElement();
You can therefore remove the explosion by suppressing the above lines. If you just want this attribute unconditionally gone, comment the above lines out, or delete them. If you want to extend the API, then add the relevant methods to the Pie chart class and check for the values here, conditionally executing the above lines based on those settings.
I hope this helps.

How to parse datetime for use in highchart graph?

Here i got timestamp value
$user_array1[] = $val['timestamp'];
$graph_data1 ='';
if(is_array($user_array1))
{
$graph_data1 = implode(',',$user_array1);
}
Output when i print_r($graph_data1);
I got
,2014-05-26 00:43:45,2014-05-26 00:43:52,2014-05-26 00:43:592014-05-26
00:43:19,2014-05-26 00:43:24,2014-05-26 00:43:27,2014-05-26
00:43:30,2014-05-26 00:43:34,2014-05-26 00:43:37,2014-05-26
00:43:41,2014-05-26 00:43:45,2014-05-26 00:43:52,2014-05-26
00:43:59,2014-05-26 00:44:11
Highchart side :
categories: [<?php echo $graph_data1 ?> ]
it does not show any things.
In your current format of data, your string are considered malformed, and therefore incorrect for input in highcharts. You must format it correctly fist. Consider this example:
$graph_data1 = "'".implode("','", $user_array1)."'";
Should output something like this:
'2014-05-26 00:43:45','2014-05-26 00:43:45','2014-05-26 00:43:45' ... etc ...

Extjs JSON response

I have a form, which sends data to the server. I process the form with a PHP script, which can return 3 differents JSON strings:
One
exit("{\"success\":\"false\", \"msg\":\"Las claves no cohinciden\"}");
Two
exit("{\"success\":\"false\", \"msg\":\"".$failure->getMessage()."\"}");
Three
exit("{\"success\":\"true\",\"msg\":\"El usuario: $nombreUsuario ha sido dado de alta correctamente.\"}");
The first and second strings, are errors to show. The third string is the normal case.
When the script return some of these values, i catch the response with ExtJs do:
var respuesta = Ext.JSON.decode(response.responseText);
But, when do:
console.log(respuesta);
Firebug console says that "respuesta" are undefinied.
Any idea ?
When you are outputting JSON for using in AJAX, it's best to set the headers for JSON and return properly formatted JSON. In my below example, we create a normal PHP array with your response and convert it to JSON with json_encode().
Try it. I've had issues before with Javascript not taking a string in as JSON before without a proper content type and formatting.
<?php
$response = array("success" => "false", "msg" => "Las claves no cohinciden");
header("Content-type: application/json");
exit(json_encode($response));
?>
Optionally, if you have jQuery... You can do $.parseJSON('your string'); to output JSON from a string.
you can use this url for testing your Json code:
http://www.utilities-online.info/xmltojson/#.UNr9FhhQ-gY

Delphi, MSXML2.XMLHTTP, PHP and Win-1250 charset encoding

Hoi!
I trying to make a webservice in Windows.
The client is Delphi 6, with MSXML2.XMLHTTP call, and other side is PHP.
First I tested: can I receive hungarian XML?
The PHP source was UTF-8 encoded file (PSPAD).
$s = 'alma árvíztűrő tükörfúrógép beta';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$m = $doc->createElement('package');
$doc->appendChild($m);
$n = $doc->createElement('Msg');
$m->appendChild($n);
$n->nodeValue = $s;
$xs = $doc->saveXML();
header('Content-Type: text/xml');
echo($xs);
This package I fully got in Delphi side, the accents are ok.
So then I tried to inject data from xml (post xml to php with accents).
global $HTTP_RAW_POST_DATA;
$xmlstr = $HTTP_RAW_POST_DATA;
$xml = new SimpleXMLElement($xmlstr);
$msg = $xml->msg;
#$msg = 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP';
Ok, I got the "msg" field, but nevertheless I trying to convert it, everytime I got some convert notice, and the result is not same as when I set the variable directly...
The debug is:
echo(utfCharToNumber($sinput).'<br>');
echo(utfCharToNumber($sdefined).'<br>');
Input: 195[ ]8286195141908419717682197[ ]328419
Defined: 195[129]8286195141908419717682197[144]328419
Input: 5156751951508270195154821951477119513780<br>
Defined: 5156751951508270195154821951477119513780<br>
As you see that two holes I have in the variable when I converted the input from MSXML2.
I really don't understand this.
I cannot reproduce same XML output from get the data from input XML as when I set directly in PHP code...
Why?
Thanks for your every help, idea, link, document, little example!
dd
Since you haven't included Delphi source, I suspect you're posting data straight from a string as content body of the request, which is encoded in the current ANSI encoding by default in Delphi 6. I advise you either use Utf8Encode on the string before you add this as the request's body data, or add a 'Content-encoding' request header with the name of the ANSI encoding (if I remember correctly GetLocaleInfo could give you this).
The source of the problem was the Delphi code.
Priorly I used AnsiToUTF8 to encode the XML text.
But the COM object is uses UTF16 as I think.
The working code is this:
procedure TForm1.Button4Click(Sender: TObject);
var
mhttp : variant;
ws : WideString;
tosend : OleVariant;
xml : TXMLDocument;
n : IXMLNode;
begin
mhttp := CreateOleObject('MSXML2.XMLHTTP');
mhttp.Open('POST', 'http://127.0.0.1/test_xmlgen.php', False);
xml := CreateANewDocument(Self, '', 'a');
n := xml.DocumentElement.AddChild('msg');
n.NodeValue := 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP';
xml.SaveToXML(ws);
tosend := ws;
mhttp.send(tosend);
Memo1.Lines.Text :=
IntToStr(mhttp.Status) + #13 +
mhttp.responseText + #13;
end;
This can resend the XML I sent - with good accents.
Thanks for your help:
dd

Categories