score:0
to get d3.json to work, i had to call it the following way (my php file generates the json data):
d3.json("inventorydata.php").then(function(data){
console.log(data);
});
score:0
you may use jquery which is more popular as follows:
$.getjson( "ajax/test.json", function( data ) {
console.log(data);
}
score:1
afaik there aren't any libraries that allow javascript to connect to a mysql database directly. instead consider exposing your data via a web service (using any number of server side technologies: asp.net, php, ruby on rails etc) and serialise your data to json in the response
score:1
i have faced problem myself with json_encode, in the end i wrote my own code instead of json_encode. just you need to set the sql and yoru connection info. hope you find it useful.
$conn = new mysqli(db_host, db_user, db_password, db_name) or die("connection failed: " . $conn->connect_error);
$sql = "select aid as id, dedate_value as date, ntertime as counter
from xxxx";
//get the date and store it in array
$records = array();
if ( $result=mysqli_query($conn,$sql) )
while ( $obj=mysqli_fetch_object($result) )
$records[] = $obj;
//echo in json format on screen
echo "[";
$comma_1 = "";
foreach($records as $obj)
{
echo $comma_1."{";
$comma_2 = "";
foreach($obj as $key => $value)
{
echo $comma_2.'"'.$key."\": \"". $value . "\"";
$comma_2 = ", ";
}
echo "}";
$comma_1 = ", \n";
}
score:7
short answer: web service.
basically, you'd want to make a web service that will return json data (for instance) to connect to d3.json()
calls.
i would recommend you to use python to quickly do something extending simplehttpserver
, or go with a web service framework such as web2py.
score:13
the following is a php script that you should be able to save somewhere as a file (let's say you call it 'getdata.php') accessible from your html file with your d3 code in it. when called it will return data from your mysql database in a json format (so long as the database server isn't outside your domain);
<?php
$username = "******";
$password = "******";
$host = "******";
$database="***dbase_name***";
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
$myquery = "
query here
";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
obviously you would need to enter appropriate details for username, password, host and database. you would also need to include an appropriate query for your data so that it returned what you were looking for. something along the lines of (and this is only a guess);
select `datetimetaken`, `reading` from `tablename`
which would return a list of time stamps and values from a table called tablename
with columns called datetimetaken
and reading
.
then when you go to read in your json file you would use the following syntax for the code where you would be reading in your json;
d3.json("getdata.php", function(error, data) {
hopefully that's close to what you're looking for. i've tested it locally and it all seems to work..
i've put together a post to go over local installation of a simple wamp server and setting up a query on the mysql database from d3.js here http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html
this is pretty much the same situation as accessing mysql database in d3 visualization
Source: stackoverflow.com
Related Query
- How to pull data from mysql database and visualize with D3.JS?
- how to visualize data from mysql database in force layout of d3 tool
- how to convert data selected from a postgres database to json or csv to use it with d3js lib?
- Get data from mysql with json and plot with D3
- How do I access data in Firebase and visualize with D3.js?
- How do I remove all children elements from a node and then apply them again with different color and size?
- How to visualize mysql data in d3js using django?
- Date and time transition from data with line
- javascript d3.js: initialize brush with brush.extent and stop data from spilling over margin
- Create data visualizations using Express and get data from a PostgreSQL database
- how to zoom d3.js chart and reload data with greater granularity (in AJAX)
- How to summarize an array with group and rollup from d3-array?
- how to get data with tsv or csv to array in d3.js from a txt file?
- How to create d3 graph using data from Rails database
- How to find top 5 numbers in a data set and data associated with it?
- How to retrieve Vector Tiles from Mapbox with d3.js and convert to geojson?
- How can I append string to y-axis data with tick and d3.format?
- D3 How to update the chart after selection from drop down menu with new data
- Changing time scale from days to weeks, how do I update and sum data values?
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How can i bind an json data with a key and Name for display
- get data from external json with react and d3
- Javascript d3 pie chart doesn't pull data from JSON file with list of dictionaries
- How can I scale my map to fit my svg size with d3 and geojson path data
- How to make xscales and xaxis from this data D3 V4
- How do you get JSON data and create a line chart with d3.js?
- How do I plot data on different x-axes and scales with d3.js? And keep it linked for brushes/tooltips?
- How can I select a dot from a scatter and display data about it?
- How do I configure Ghost in in dev and production to make a json data available via url to load with d3.json?
- How can I use crossfilter to filter graph data and visualize them on a force directed graph?
More Query from same tag
- D3.stratify Uncaught Error: ambiguous: Distributive property
- How to remove parent DOM node after d3 selection().exit()
- d3 vary line/area color along x-axis in line graph
- d3.js: Passing data from parent node to child node onclick event
- Multiple draggable charts d3.js
- Starting a d3 axis timescale from zero
- tickSubdivide not always working?
- D3.js adding a new object with nest and key
- D3, how to finish this graph, data format
- Fill a grid with empty rectangles in D3.js?
- Callback function issue with d3.csv() in d3.js
- d3.js json http authentication issue (no data receive)
- Jumping behavior when dragging groups of elements using d3.drag
- D3, JSON, graph, force layout, data update, not complete redraw, drag
- Normalize numbers to 100 using D3 range?
- D3 tooltip show values from nested dataset
- Multiple Assignments in JavaScript Expression and Syntax Explanation
- Changing opacity for GeoJSON feature using d3 and Leaflet
- D3 click event with multiple svg on one page
- Binding an event during an update() transition
- How to access JSON data with D3.js?
- D3: Select SVG attribute and change it
- D3 updating data on box plot without removing previous SVG first
- Force-Dragging-Graph D3.js error
- d3.js, grouped bar chart with two Y axes, series related to Y1 or Y2
- Storing, editing and retrieving the d3 hierarchy in a MongoDB (keeping realtime reactivity)
- how can set customize color to pie chart using D3.js?
- D3: Highlighting Parts of Path on Mouseover in Line Graph
- d3 linear scale with strange output values
- Getting NaN values in svg path 'd' attr when trying to map geojson data