For my website I have to create a bar graph with the following data : 90; 99; 87; 78; 89; 67; 85; 67; 85; 100;
Those would be grades on 100 and I would like to put each one of them in a bar, next to each other. Do you know how I could do that with php (or javascript) ?
I have a picture for the bar of my graph registered under graphbar.png and my csv file is called websitedvpt.csv
I've looked for this everywhere but I really don't understand how it works, can someone help me ?
For my website I have to create a bar graph with the following data : 90; 99; 87; 78; 89; 67; 85; 67; 85; 100;
Those would be grades on 100 and I would like to put each one of them in a bar, next to each other. Do you know how I could do that with php (or javascript) ?
I have a picture for the bar of my graph registered under graphbar.png and my csv file is called websitedvpt.csv
I've looked for this everywhere but I really don't understand how it works, can someone help me ?
Share Improve this question edited Mar 15, 2014 at 17:35 The Blue Dog 2,4753 gold badges20 silver badges25 bronze badges asked Mar 15, 2014 at 17:33 user3423788user3423788 411 silver badge2 bronze badges5 Answers
Reset to default 3D3.js + C3.js
A simple way to do this is using D3.js with C3.js. C3 is just a simple reusable patterns library for D3 begginners.
Generating a bar graph with the given data is as simple as:
HTML
<div id="chart"></div>
JS
var chart = c3.generate({
data: {
columns: [
['data1', 90, 99, 87, 78, 89, 67, 85, 67, 85, 100]
],
type: 'bar'
}
});
JSFIDDLE HERE
D3.js + NVD3.js
NVD3.js is another D3.js collection of reusable charts like C3.js. Generating a bar graph with your data would be like this:
HTML
<div id='chart'>
<svg style='height:500px'> </svg>
</div>
JS
nv.addGraph(function() {
var chart = nv.models.discreteBarChart()
.x(function(d) { return d.label }) //Specify the data accessors.
.y(function(d) { return d.value })
.staggerLabels(true) //Too many bars and not enough room? Try staggering labels.
.tooltips(false) //Don't show tooltips
.showValues(true) //...instead, show the bar value right on top of each bar.
.transitionDuration(350)
;
d3.select('#chart svg')
.datum(exampleData())
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
//Each bar represents a single discrete quantity.
function exampleData() {
return [
{
key: "Cumulative Return",
values: [
{
"label" : "A Label" ,
"value" : 90
} ,
{
"label" : "B Label" ,
"value" : 99
} ,
{
"label" : "C Label" ,
"value" : 87
} ,
{
"label" : "D Label" ,
"value" : 78
} ,
{
"label" : "E Label" ,
"value" : 89
} ,
{
"label" : "F Label" ,
"value" : 67
} ,
{
"label" : "G Label" ,
"value" : 85
} ,
{
"label" : "H Label" ,
"value" : 67
},
{
"label" : "I Label" ,
"value" : 85
} ,
{
"label" : "J Label" ,
"value" : 100
}
]
}
]
}
JSFIDDLE HERE
jQuery Flot
jQuery.flot is a simple graph library for jQuery that uses HTML Canvas.
This example would be plotted like this:
JS
var d1 = [[1,90], [2,99], [3,87], [4,78], [5,89], [6,67], [7,85], [8,67], [9,85], [10,100]];
$(document).ready(function () {
$.plot($("#placeholder"), [
{
data: d1,
bars: {
show: true
}
}
]);
});
HTML
<div id="placeholder"></div>
CSS
#placeholder {
width: 450px;
height: 200px;
}
JSFIDDLE HERE
How to read CSV Data
To read CSV data from a file you can use several libraries and depends on what languages you are using.
For PHP, use fgetcsv
For Ruby, use the CSV class and its methods.
For JavaScript (node.js) you can use the CSV npm
For JavaScript (static) you can a library like CSV.js
You can use D3.JS to do this.. A full tutorial is here: http://bl.ocks/mbostock/3885304 -- you can simply point it to a CSV file, then do your analytics/rendering against that as-needed. It will wind up generating an SVG and gives you the bits you need to do interactive stuff as well if you need it..
Sample code from same tutorial:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");
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 + ")");
d3.tsv("data.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); });
});
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
A seemingly good answer above, but svg can be a bit of a steep learning curve. You might well find it easier to use a bination of PHP and php's GD graphics library, which is a part of the php standard. That way, you just grab the data using fgetcsv - a very basic tutorial can be found at http://php/manual/en/function.fgetcsv.php - and then you are basically doing everything in the one language, which is the best way to get to grips with php or any other language.
And here's a very good, full-featured tute for GD: http://www.phpbuilder./columns/nasser20030219.php3?page=2
Hope that helps.
You can use something like that (see url)
var chart = c3.generate({
bindto: '#chart_c3_donut',
data: {
url: 'c3_donut.csv',
I corrected...this code is working well...
<html>
<head>
<link rel="stylesheet" type="text/css" href="c3.css">
</head>
<body>
<div id="chart"></div>
<script src="d3.min.js" charset="utf-8"></script>
<script src="c3.min.js"></script>
<script>
var chart = c3.generate({
bindto: '#chart',
data: {
x: 'Date',
x_Format: '%Y-%m-%d',
url: 'dates.csv',
},
axis: {
x: {
type: 'timeseries',
}
}
});
</script>
</body>
</html>
Date,Count
1996-12-20,1
1997-01-31,2
1997-01-31,3
1997-05-07,4
1997-10-03,5
1997-12-02,6
1997-12-02,7
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313501a4420371.html
评论列表(0条)