2

The issue continues active already! Where are all this smart guys?

Link to download it! example.zip (on the "advanced_8stremas, i took the original and put some steroids, so It can be easier for you to get where is the problem!)

Demo live: Advanced graph on steroids
Demo live: Advanced graph Original
Demo live: Simple graph takes values directly from mysql enter image description here
So the answer relays on the PINK box! xP
enter image description here

This is the code that generates the random data for the advanced graph!

function CAGARRO() { 
  var data1 = []; 
  var data2 = [];
  var data3 = []; 
  var data4 = [];
  var data5 = [];  
  var data6 = [];
  var data7 = [];
  var data8 = [];

// number 8 means 8lines, number 130 means create 130 x values, 
// .1 means the minimum Y value that the random code can give to all his numbers
stream_layers(8,130,.1).map(function(layer, PIXARADA) { 
      layer.forEach(function(TUTANCAMON, i) {
          var object = { x: TUTANCAMON.x };
          object['stream' + (PIXARADA + 1)] = TUTANCAMON.y;
          eval('data' + (PIXARADA + 1)).push(object);
      });
});


var dataA = extend(data1, data2);
var dataB = extend(data3, data4);
var dataC = extend(data5, data6);
var dataD = extend(data7, data8);

var dataY = extend(dataA, dataB);
var dataZ = extend(dataC, dataD);

var RESULTATS = extend(dataY, dataZ);


return RESULTATS;
}

This tutorial is amazing, you can't imagine how simple it is to connect your MySQL database to a D3 (javascript) chart. (15min implementation)

The only "problem" is that the chart on the tutorial is "too simple". Just one single line!

enter image description here

Then we have "really cool stuff" like here: Live Demo of the CoolStuff Chart

and here: Original Source CoolStuff Chart

enter image description here

And when you arrive happily up to this point you find the following differences: - The tutorial source:

d3.json("php/data2.php", function(error, data) {
data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
});

This tutorial source means the data that is receiving from the MySQL database something like this:

[{"date":"1-May-12","close":"58.13"},{"date":"30-Apr-12","close":"53.98"},
{"date":"27-Apr-12","close":"67.00"},{"date":"26-Apr-12","close":"89.70"},
{"date":"25-Apr-12","close":"99.00"},{"date":"24-Apr-12","close":"130.28"},
{"date":"23-Apr-12","close":"166.70"},{"date":"20-Apr-12","close":"234.98"},
{"date":"19-Apr-12","close":"345.44"},{"date":"18-Apr-12","close":"443.34"},
{"date":"17-Apr-12","close":"543.70"},{"date":"16-Apr-12","close":"580.13"},
{"date":"13-Apr-12","close":"605.23"},{"date":"12-Apr-12","close":"622.77"},
{"date":"11-Apr-12","close":"626.20"},{"date":"10-Apr-12","close":"628.44"},
{"date":"9-Apr-12","close":"636.23"},{"date":"5-Apr-12","close":"633.68"},
{"date":"4-Apr-12","close":"624.31"},{"date":"3-Apr-12","close":"629.32"},
{"date":"2-Apr-12","close":"618.63"},{"date":"30-Mar-12","close":"599.55"},
{"date":"29-Mar-12","close":"609.86"},{"date":"28-Mar-12","close":"617.62"},
{"date":"27-Mar-12","close":"614.48"},{"date":"26-Mar-12","close":"606.98"}]

This uggly data looks like this on the MySQL:

enter image description here

Anyway, the super coolStuff chart is that all the data is created with some strange random numbers loop that has been a hell for me to try to plug-in the data I receive from MySQL to the coolStuff code!!

Here a copy of the "CoolStuff" loop:

function testData() {
  var data1 = [];
  var data2 = [];
  var data3 = [];

  stream_layers(3,128,.1).map(function(layer, index) {
    layer.forEach(function(item, i) {
      var object = { x: item.x };
      object['stream' + (index + 1)] = item.y;
      eval('data' + (index + 1)).push(object);
    });
  });

Any ideas?

2 Answers 2

1

Ok, so we have 3 thinks until know.

1. A "MySQL" database

with 3 tables calles "Date1", "Date2" and "Date3" With 2columns each with data :
 _________________________________________________________________________________|
|                    Table: Data1             |                   Table: Data2             |                   Table: Data3              |
 
_________________________________________________________________________________|
|           Date        |         Close        |          Date        |         Close        |          Date        |         Close         |
|
_________________________________________________________________________________|
|       1-May-12    |         58.13         |      1-May-12    |         58.13         |      1-May-12    |         58.13          |
|      30-Apr-12    |         53.98         |     30-Apr-12    |         53.98         |     30-Apr-12    |         53.98         |
|      27-Apr-12    |         67.00         |     27-Apr-12    |         67.00         |     27-Apr-12    |         67.00         |
|_________________________________________________________________________________|

2. A php file/bridge with some JSON data back

________________                                 _______________________
|      Table Data1      |  - Data1.php - >     |     Data translated to JSON    |
________________                                 _______________________

________________                                 _______________________
|      Table Data2      |  - Data2.php - >     |     Data translated to JSON    |
________________                                 _______________________

________________                                 _______________________
|      Table Data3      |    - Data3.php - >    |     Data translated to JSON    |
________________                                 _______________________

(Php complete Source here) that selects the data from our table

$myquery = "SELECT  `date`, `close` FROM  `data1`";<br>

Then we declare the $data variable as an array ($data = array();) and feed the returned information from our query into $data array;

for ($x = 0; $x < mysql_num_rows($query); $x++) {
    $data[] = mysql_fetch_assoc($query);
}

(that's a fancy little piece of code that gets the information row by row and puts it into the array) We then return (echo) the $data array in json format (echo json_encode($data);) into whatever ran the data2.php script

So now when we type "data1.php", "data2.php" or "data3.php" we get back this:

"data1.php"

[{"date":"1-May-12","close":"58.13"},{"date":"30-Apr-12","close":"53.98"},
{"date":"27-Apr-12","close":"67.00"},{"date":"26-Apr-12","close":"89.70"},

"data2.php"

[{"date":"1-May-12","close":"58.13"},{"date":"30-Apr-12","close":"53.98"},
{"date":"27-Apr-12","close":"67.00"},{"date":"26-Apr-12","close":"89.70"},

"data3.php"

[{"date":"1-May-12","close":"58.13"},{"date":"30-Apr-12","close":"53.98"},
{"date":"27-Apr-12","close":"67.00"},{"date":"26-Apr-12","close":"89.70"},

3. The D3.js multichart with 3 linecharts.

Multichart demo but this multichart is creating it self random data. With this code:

function testData() {
  var data1 = [];
  var data2 = [];
  var data3 = [];

  stream_layers(3,128,.1).map(function(layer, index) {
    layer.forEach(function(item, i) {
      var object = { x: item.x };
      object['stream' + (index + 1)] = item.y;
      eval('data' + (index + 1)).push(object);
    });
  });

  var data = extend(data1, data2);
  var result = extend(data, data3);

  return result;
}

So we create under the user's "Xaranqe" advice the following code to plug in our "Data" to this multichart

function testData() {
    var data1 = [];
    var data2 = [];
    var data3 = [];

    d3.json("php/data1.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data1.push(d.close);
        });
    d3.json("php/data2.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data2.push(d.close);
        });
    d3.json("php/data3.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data3.push(d.close);
        });
    // More similar functions here
    });

  var data = extend(data1, data2);
  var result = extend(data, data3);

  return result;
}

But the result we get back is, a BLANK page.. So there is something there we are missing.. mmmm...

Any new ideas?

I leave you with a direct copy of the multichart code! Thanks for help! Am sure the community will benefit a lot from this info!


<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">


<style>
body {
  overflow-y:scroll;
}
text {
  font: 12px sans-serif;
}
svg {
  display: block;
}
#chart1 svg {
  height: 500px;
  min-width: 100px;
  min-height: 100px;
/*
  margin: 50px;
  Minimum height and width is a good idea to prevent negative SVG dimensions...
  For example width should be =< margin.left + margin.right + 1,
  of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
*/
}
</style>


<body>
  <div id="chart">
    <svg style="height: 500px;"></svg>
  </div>
  <div id="stream1" style="float: left; margin-left: 15px;">
    <div><h1>Stream #1</h1></div>
  </div>
  <div id="stream2" style="float: left; margin-left: 15px;">
    <div><h1>Stream #2</h1></div>
  </div>
  <div id="stream3" style="float: left; margin-left: 15px;">
    <div><h1>Stream #3</h1></div>
  </div>


<script src="../lib/d3.v2.js"></script>
<script src="../lib/crossfilter.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/utils.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/axis.js"></script>
<script src="../src/models/scatter.js"></script>
<script src="../src/models/line.js"></script>
<script src="../src/models/lineWithFocusChart.js"></script>
<script src="stream_layers.js"></script>

<script>


extend = function(destination, source) {
    for (var property in source) {
    if (property in destination) { 
      if ( typeof source[property] === "object" &&
        typeof destination[property] === "object") {
          destination[property] = extend(destination[property], source[property]);
      } else {
        continue;
      }
    } else {
      destination[property] = source[property];
    };
    }
    return destination;
};
var rawData = testCrossfilterData();
nv.addGraph(function() {
  var chart = nv.models.lineWithFocusChart();
  chart.xAxis
      .tickFormat(d3.format(',f'));
  chart.x2Axis
      .tickFormat(d3.format(',f'));
  chart.yAxis
      .tickFormat(d3.format(',.2f'));
  chart.y2Axis
      .tickFormat(d3.format(',.2f'));
  chart.dispatch.on('brush', click);
  var data = normalizeData(rawData.datum, 
        [
        {
          name: 'Stream #1',
          key: 'stream1'
        },
        {
          name: 'Stream #2',
          key: 'stream2'
        },
        {
          name: 'Stream #3',
          key: 'stream3'
        }
        ]);
nv.log(data);
  d3.select('#chart svg')
      .datum(data)
    .transition().duration(500)
      .call(chart);
  nv.utils.windowResize(chart.update);
  return chart;
});


function click(e) {
  extent = e.extent;
  rawData.data.filter([extent[0], extent[1]]);
  streams("stream1");
  streams("stream2");
  streams("stream3");
}


function streams(key) {
  var topData = rawData.data.top(5);
  var stream = d3.select("div#" + key).selectAll(".stream-data")
      .data(topData, function(d) {
        return d.key;
      });
  stream
    .html(function(d) {
      return d[key];
    })
  stream.enter().append("div")
      .attr("class", "stream-data")
      .html(function(d) {
        return  d[key]; 
      })
  stream.exit().remove();
  stream.order();
}


function normalizeData(data, series) {
  var sort = crossfilter.quicksort.by(function(d) { return d.key; });
  var result = [];
  for (var i = 0; i < series.length; i++) {
    var seriesData = data.top(Infinity);
    var sorted = sort(seriesData, 0, seriesData.length);
    var values = [];

    seriesData.forEach(function(item, index) {
        values.push({x: item.key,  y: item.value[series[i].key]});
    });
    result.push({key: series[i].name, values: values, color: series[i].color});
  };
  return result;
};


function testCrossfilterData() {
  var data = crossfilter(testData());

  try {
    data.data = data.dimension(function(d) { return d.x; });
    data.datum = data.data.group(function(d) { return d; });
    data.datum.reduce(
      function (p, v) {
        p.count++;
        p.stream1 += v.stream1;
        p.stream2 += v.stream2;
        p.stream3 += v.stream3;
        return p;
      },
      function (p, v) {
        p.count--;
        p.stream1 -= v.stream1;
        p.stream2 -= v.stream2;
        p.stream3 -= v.stream3;
        return p;
      },
      function () {
        return {count: 0, stream1: 0, stream2: 0, stream3: 0};
      });

    data.stream1 = data.dimension(function(d) { return d.stream1; });
    data.stream1datum = data.data.group(function(d) { return d; });
    data.stream1datum.reduce(
      function (p, v) {
        p.count++;
        p.stream1 += v.stream1;
        p.stream2 += v.stream2;
        p.stream3 += v.stream3;
        return p;
      },
      function (p, v) {
        p.count--;
        p.stream1 -= v.stream1;
        p.stream2 -= v.stream2;
        p.stream3 -= v.stream3;
        return p;
      },
      function () {
        return {count: 0, stream1: 0, stream2: 0, stream3: 0};
      }
    );

    data.stream2 = data.dimension(function(d) { return d.stream2; });
    data.stream2datum = data.data.group(function(d) { return d; });
    data.stream2datum.reduce(
      function (p, v) {
        p.count++;
        p.stream1 += v.stream1;
        p.stream2 += v.stream2;
        p.stream3 += v.stream3;
        return p;
      },
      function (p, v) {
        p.count--;
        p.stream1 -= v.stream1;
        p.stream2 -= v.stream2;
        p.stream3 -= v.stream3;
        return p;
      },
      function () {
        return {count: 0, stream1: 0, stream2: 0, stream3: 0};
      }
    );

    data.stream3 = data.dimension(function(d) { return d.stream3; });
    data.stream3datum = data.data.group(function(d) { return d; });
    data.stream3datum.reduce(
      function (p, v) {
        p.count++;
        p.stream1 += v.stream1;
        p.stream2 += v.stream2;
        p.stream3 += v.stream3;
        return p; },
      function (p, v) {
        p.count--;
        p.stream1 -= v.stream1;
        p.stream2 -= v.stream2;
        p.stream3 -= v.stream3;
        return p;
      },
      function () {
        return {count: 0, stream1: 0, stream2: 0, stream3: 0};
      }
    );
  } catch (e) {
    nv.log(e.stack);
  }
  return data;
}


function testData() {
    var data1 = [];
    var data2 = [];
    var data3 = [];
    d3.json("php/data1.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data1.push(d.close);
        });
    d3.json("php/data2.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data2.push(d.close);
        });
    d3.json("php/data3.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data3.push(d.close);
        });
    // More similar functions here
    });
  var data = extend(data1, data2);
  var result = extend(data, data3);
  return result;
}


</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know whether this will work, but here's some pseudocode that (hopefully) points in the right direction:

function testData() {
    var data1 = [];
    var data2 = [];
    var data3 = [];

    d3.json("php/data1.php", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
            data1.push(d.close);
        });

    // More similar functions here
});

1 Comment

Hi Xaranke, and thanks for your answer. This is how it looks like when I just copy and paste your "pseudocode" link kind of "No data available" Message. I gonna edit my "Question" and put the full code of both the "simple code" and the "cool stuff code" and try to comment it as much as possible.. If maybe it pops up some more ideas.. lets see..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.