ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Multi Line graph with MSSQL database with PHP
Posted by Johan Valstar on Jun-11-2020 02:27
All,
Who known how to use ChartDirector with PHP code.
The data must come from a MSSQL database and contains 2 lines.

Any idea?

Any help is appreciated.

Johan

  Re: Multi Line graph with MSSQL database with PHP
Posted by Peter Kwan on Jun-12-2020 11:04
Hi Johan,

ChartDirector for PHP expects the data to be in PHP arrays. So you just need to read your data into PHP arrays, and then pass the data to ChartDirector as in the sample code. For example:

// connect to db
mysql_connect(.....);

// Your SQL statement
$SQL = "SELECT AAA, BBB from CCC ORDER BY DDD";

// Query the DB
$result = mysql_db_query("my_db_name", $SQL);

// Read query result to arrays
$labels = array();
$data = array();
while ($row = mysql_fetch_row($result))
{
    $labels[] = $row[0];
    $data[] = $row[1];
}

.... use $labels and $data to plot chart ....


Hope this can help.

Regards
Peter Kwan

  Re: Multi Line graph with MSSQL database with PHP
Posted by J.J. on Jun-12-2020 16:04
Attachments:
I'm still getting errors from PHP, see below:

12-Jun-2020 09:46:54 Europe/Amsterdam] PHP Notice:  Object of class DateTime could not be converted to float in C:inetpubVCSChartDirectorlibphpchartdir.php on line 2189
[12-Jun-2020 09:46:54 Europe/Amsterdam] PHP Notice:  Object of class DateTime could not be converted to float in C:inetpubVCSChartDirectorlibphpchartdir.php on line 2189
[12-Jun-2020 09:46:54 Europe/Amsterdam] PHP Notice:  Object of class DateTime could not be converted to float in C:inetpubVCSChartDirectorlibphpchartdir.php on line 2189
[

The code I used is:

require_once ('ftest.php');
require_once("ChartDirector/lib/phpchartdir.php");
$page = $_SERVER['PHP_SELF'];
$sec = "60";


//Connect to db
$con=GetCon();

// Your SQL statement
$SQL= "select [Time],[BatteryVoltage],[SolarVoltage]
                from [dbo].[tblSolar]
                 order by Time desc";



// Query the DB
$result = sqlsrv_query($con, $SQL);

// Read query result to arrays
$labels = array();
$data1 = array();
$data2 = array();
while ($row = sqlsrv_fetch_array($result))
{
    $labels[] = $row[0];
    $data1[] = $row[1];
$data2[] = $row[2];
}

# Create a XYChart object of size 600 x 400 pixels
$c = new XYChart(600, 400);
# Add a title box using grey (0x555555) 20pt Arial font
$c->addTitle("    Multi-Line Chart Demonstration", "arial.ttf", 20, 0x555555);

# Set the plotarea at (70, 70) and of size 500 x 300 pixels, with transparent background and border
# and light grey (0xcccccc) horizontal grid lines
$c->setPlotArea(70, 70, 500, 300, Transparent, -1, Transparent, 0xcccccc);

# Add a legend box with horizontal layout above the plot area at (70, 35). Use 12pt Arial font,
# transparent background and border, and line style legend icon.
$b = $c->addLegend(70, 35, false, "arial.ttf", 12);
$b->setBackground(Transparent, Transparent);
$b->setLineStyleKey();

# Add 3 data series to the line layer
$layer = $c->addBarLayer2(Stack);
$layer->addDataSet($data1, 0xee9944, "Beta");
$layer->addDataSet($data2, 0x99bb55, "Gamma");

# The x-coordinates for the line layer
$layer->setXData($labels);
$c->xAxis->setLabels($labels);
# Output the chart

# Set axis label font to 12pt Arial
$c->xAxis->setLabelStyle("arial.ttf", 12);
$c->yAxis->setLabelStyle("arial.ttf", 12);

# Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa)
$c->xAxis->setColors(Transparent, TextColor, TextColor, 0xaaaaaa);
$c->yAxis->setColors(Transparent);

# Set the major/minor tick lengths for the x-axis to 10 and 0.
$c->xAxis->setTickLength(10, 0);

# For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis.
$c->xAxis->setTickDensity(80);
$c->yAxis->setTickDensity(40);

# Add a title to the y axis using dark grey (0x555555) 14pt Arial font
$c->yAxis->setTitle("Y-Axis Title Placeholder", "arial.ttf", 14, 0x555555);

# Add a line layer to the chart with 3-pixel line width
$layer = $c->addLineLayer2();
$layer->setLineWidth(3);


header("Content-type: image/png");
print($c->makeChart2(PNG));


What I'm doing wrong?
new.php
<?php
/**
 * Charts 4 PHP
 *
 * @author Shani <support@chartphp.com> - http://www.chartphp.com
 * @version 2.0
 * @license: see license.txt included in package
 */
 
 require_once ('ftest.php');
 require_once("ChartDirector/lib/phpchartdir.php");
$page = $_SERVER['PHP_SELF'];
$sec = "60";

				 
//Connect to db
$con=GetCon();	

// Your SQL statement
$SQL= "select [Time],[BatteryVoltage],[SolarVoltage]
                from [dbo].[tblSolar]
                 order by Time desc"; 



// Query the DB
$result = sqlsrv_query($con, $SQL);

// Read query result to arrays
$labels = array();
$data1 = array();
$data2 = array();
while ($row = sqlsrv_fetch_array($result))
{
    $labels[] = $row[0];
    $data1[] = $row[1];
	$data2[] = $row[2];
}
		 
# Create a XYChart object of size 600 x 400 pixels
$c = new XYChart(600, 400);
# Add a title box using grey (0x555555) 20pt Arial font
$c->addTitle("    Multi-Line Chart Demonstration", "arial.ttf", 20, 0x555555);

# Set the plotarea at (70, 70) and of size 500 x 300 pixels, with transparent background and border
# and light grey (0xcccccc) horizontal grid lines
$c->setPlotArea(70, 70, 500, 300, Transparent, -1, Transparent, 0xcccccc);

# Add a legend box with horizontal layout above the plot area at (70, 35). Use 12pt Arial font,
# transparent background and border, and line style legend icon.
$b = $c->addLegend(70, 35, false, "arial.ttf", 12);
$b->setBackground(Transparent, Transparent);
$b->setLineStyleKey();

# Add 3 data series to the line layer
$layer = $c->addBarLayer2(Stack);
$layer->addDataSet($data1, 0xee9944, "Beta");
$layer->addDataSet($data2, 0x99bb55, "Gamma");

# The x-coordinates for the line layer
$layer->setXData($labels);
$c->xAxis->setLabels($labels);
# Output the chart

# Set axis label font to 12pt Arial
$c->xAxis->setLabelStyle("arial.ttf", 12);
$c->yAxis->setLabelStyle("arial.ttf", 12);

# Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa)
$c->xAxis->setColors(Transparent, TextColor, TextColor, 0xaaaaaa);
$c->yAxis->setColors(Transparent);

# Set the major/minor tick lengths for the x-axis to 10 and 0.
$c->xAxis->setTickLength(10, 0);

# For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis.
$c->xAxis->setTickDensity(80);
$c->yAxis->setTickDensity(40);

# Add a title to the y axis using dark grey (0x555555) 14pt Arial font
$c->yAxis->setTitle("Y-Axis Title Placeholder", "arial.ttf", 14, 0x555555);

# Add a line layer to the chart with 3-pixel line width
$layer = $c->addLineLayer2();
$layer->setLineWidth(3);


header("Content-type: image/png");
print($c->makeChart2(PNG));
?>

  Re: Multi Line graph with MSSQL database with PHP
Posted by J.J. on Jun-12-2020 16:06
Out of SQL is:
Time                               BatteryVoltage SolarVoltage
2020-06-12 10:05:55.000 27.55          106.52
2020-06-12 10:04:54.000 27.57 106.71
2020-06-12 10:03:54.000 27.57 106.84
2020-06-12 10:02:54.000 27.58 107.17

  Re: Multi Line graph with MSSQL database with PHP
Posted by Peter Kwan on Jun-13-2020 04:51
Hi J.J,

First, you need to determine what you want the x-axis to look like. You can use Axis.setLabels or Layer.setXData, but not both at the same time.

If you are plotting a bar chart, the most common style is to have the bars equally spaced and to have a label for each bar. In this case, the API is Axis.setLabels.

Next you would need to determine what are the labels. If the labels are "2020-06-12 10:05:55.000", it is quite long for a bar. Suppose you just need the hours and minutes. In this case, the label is "10:05". So you should obtain this label from your database.

SELECT FORMAT([Time], "hh:mm"), [BatteryVoltage], [SolarVoltage] .....

If you prefer to use SELECT [Time], [BatteryVoltage], [SolarVoltage], it appears with your database schema, database driver and PHP version, the [Time] will become a PHP DateTime data type. This is an opaque PHP data type. You can use the DateTime.format method to convert it to a human readable format:

$labels[] = $row[0]->format("H:i");


Sometimes a chart will use a real date/time x-axis. In this type of chart, the x-coordinates are provided to ChartDirector using Layer.setXData. By default, ChartDirector will then automatically determine the x-axis labels.  An example for this style is at:

https://www.advsofteng.com/doc/cdphp.htm#multiline2.htm

This format is more common for line charts, or charts with a lot of evenly spaced data points. Note that if you use Layer.setXData, you cannot use Axis.setLabels. You can provide the label format and let ChartDirector automatically determine the labels, or you can specify the x-axis scale and labels using Axis.setDataScale.

To use Layer.setXData, the date/time must be in a format supported by ChartDirector. Because database of different schema, brand, driver and PHP version can use different data types, ChartDirector are unable to support all these types. For PHP, the most common method is to use the UNIX timestamp format. In this case, the code would be like:

$xData[] = chartTime2($row[0]->getTimeStamp());

You can then use Layer.setXData, like:

$layer->setXData($xData);

and optionally provide the format:

$layer->setLabelFormat("{value|hh:nn}");

Hope this can help.

Regards
Peter Kwan