D3.js and Yun

Simple d3.js Graph

PHP, SQLITE3 and Yun

http://forum.arduino.cc/index.php?topic=309101.0

Make Datafeed (data.csv) page

nano /www/sensordata.php
<?php
print "date,close\n";
$db = new SQLite3('/mnt/sda1/sensor.db');
$results = $db->query('SELECT strftime("%H:%M:%S", sqlitetimestamp), temperature from sensor_data ORDER BY id LIMIT 20;');
while ($row = $results->fetchArray()) {
print $row[0].",".$row[1]."\n";
}
$db->close();
?>

Make Sensor page:

nano /www/sensor.html
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

</style>
<body>

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%H:%M:%S").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });
    
// Adds the svg canvas
var svg = d3.select("body")
    .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
    .append("g")
        .attr("transform", 
              "translate(" + margin.left + "," + margin.top + ")");

// Get the data
d3.csv("sensordata.php", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.close; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data));

    // Add the X Axis
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    svg.append("text")
        .attr("transform",
              "translate(" + (width/2) + " ," + 
                             (height+margin.bottom) + ")")
        .style("text-anchor", "middle")
        .text("Date");

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);
    svg.append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("x", margin.top - (height / 2))
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Temperature");
});

</script>
</body>

Download above page:

wget https://www.dropbox.com/s/wdf2jrij3ue6k5y/sensor.html?dl=0 -O /www/sensor.html --no-check-certificate

@sunnyyu
Thanks alot for your useful example.

From my point of view Highcharts is mutch more easy to use and has some very useful features and good designs.
That's why I decided to add an example with Highcharts.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

    <script src="https://code.highcharts.com/stock/highstock.js"></script>
    <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>

    <div id="container" style="width:100%; height:400px;"></div>

    <script type="text/javascript">
 
    //get Data it from a SQLite Database
    var arr =
    <?php
        $db = new PDO('sqlite:/mnt/sda1/sensor.db');
        $sth = $db->query('SELECT (julianday(sqlitetimestamp)- 2440587.5)*86400000 AS sqlitetimestamp, temperature FROM sensor_data ORDER BY id LIMIT 50');
        $sth->execute();
        $results=$sth->fetchAll(PDO::FETCH_ASSOC);
        $json=json_encode($results);

        echo $json 
    ?>;
    
      var arrTemperature = [];
      var test = arr;
      for (k = 0; k < test.length; k++) {
        arrTemperature.push([parseInt(test[k].sqlitetimestamp), parseFloat(test[k].temperature)]);
      }
   
    $(function () {
        $('#container').highcharts({
            title: {
                text: 'Wetterstation',
                x: -20 //center
            },
            subtitle: {
                text: 'Reduit Whg 3 EG rechts',
                x: -20
            },
            xAxis: {
                type: 'datetime'
            },
            yAxis: {
                title: {
                    text: 'Werte'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '°C'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: 'Temperature °C',
                data: arrTemperature
            }]
        });
    });

    </script>

Kind regards
Alain

Screenshot in Attachement.