Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a line graph using Json array using webrequest

Tags:

json

c#

php

mysql

First of all, I'm very new to C# and Json.

I wanted to plot a graph from mysql table data in a C# GUI. I made a PHP file to select the data from mysql database table and echoed the selected contents in json array using echo json_encode(array("result"=>$result))

here is my PHP:

<?php
define('HOST','*********************');
define('USER','*********************');
define('PASS','*********************');
define('DB','***********************');

if($_SERVER['REQUEST_METHOD']=='GET'){

 $start = $_GET['start'];
 $ending  = $_GET['ending'];
}

$con = mysqli_connect(HOST,USER,PASS,DB);


$sql = "SELECT * FROM table WHERE date_time BETWEEN '$start' and '$ending'" ;
$res = mysqli_query($con,$sql);

$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'p_pairs'=>$row[1],'temp1'=>$row[2]  ,'temp2'=>$row[3],'temp3'=>$row[4],'temp4'=>$row[5],'temp5'=>$row[6],'avg_current'=>$row[7],'avg_voltage'=>$row[8],'kw'=>$row[9],'kwh'=>$row[10]));
}

echo json_encode(array($result));

mysqli_close($con);

?>

using the link, I can see the json array clearly between the datetime gap.All I wanted is to plot the graph of temperature values (temp1, temp2,..), p-pairs values and others with the date_time in a line graph to read the historical data.

All I get when I access the PHP page is:

[[{"id":"1","p_pairs":"0000-00-00 00:00:00","temp1":"2","temp2":"100","temp3":"100","temp4":"100","temp5":"100","avg_current":"100","avg_voltage":"300","kw":"300","kwh":"300"},{"id":"2","p_pairs":"0000-00-00 00:00:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"},{"id":"3","p_pairs":"2016-01-07 21:10:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"},{"id":"4","p_pairs":"2016-01-07 21:10:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"}]]

NOTE: some datetime is set as default here. I just wanted to show this array. The correct one will be perfect to plot the graph.

I can can take these array in a string using a web request from C#. using the bellow code:

        System.Net.WebClient wc = new System.Net.WebClient();
        byte[] raw = wc.DownloadData("url to php");

        string webData = System.Text.Encoding.UTF8.GetString(raw);

It'll be a big help If someone can help me to plot this in a line graph as date_time Vs temp1, temp2 or p_pairs and like that in C# GUI...

This is going to be a big help for me. Thanking you in advance.

like image 233
Althaf Avatar asked Jan 08 '16 06:01

Althaf


1 Answers

Firstly get a class to work from. You can dynamically create a class from Json

If you are happy to have a dependency upon the System.Web.Helpers assembly, then you can use the Json class:

dynamic data = Json.Decode(json);

It is included with the MVC framework as an additional download to the .NET 4 framework. Or use the NewtonSoft one.

Next get a chart component: https://msdn.microsoft.com/en-us/library/dd489237.aspx and bind your data to the chart

like image 117
Eminem Avatar answered Oct 18 '22 01:10

Eminem