如何用csv文件创建条形图

How to create a bar graph with a csv file

本文关键字:条形图 文件创建 csv 何用      更新时间:2023-09-26

对于我的网站,我必须创建一个具有以下数据的条形图:90;99;87;78;89;67;85;67;85;

100;

这些是100分的分数,我想把他们每个人放在一个酒吧里,彼此相邻。你知道我怎么用php(或javascript)吗?

我在graphbar。png下注册了图的条形图我的csv文件名为websitedvpt。csv

我到处找这个,但是我真的不明白它是怎么工作的,有人能帮帮我吗?

D3.js + C3.js

一个简单的方法是使用D3.js和C3.js。C3只是D3初学者的一个简单的可重用模式库。

使用给定数据生成条形图非常简单,如下所示:

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

D3.js + NVD3.js

NVD3.js是另一个D3.js的可重用图表集合,像C3.js。用数据生成一个条形图,如下所示:

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

jQuery Flot

jQuery。flot是一个简单的jQuery图形库,使用HTML Canvas。

这个例子应该这样绘制:

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

如何读取CSV数据

要从文件中读取CSV数据,你可以使用几个库,这取决于你使用的是什么语言。

    对于PHP,使用fgetcsv
  • 对于Ruby,使用CSV类及其方法。

  • 对于JavaScript (node.js),可以使用CSV文件npm

  • 对于JavaScript(静态),您可以使用像CSV.js这样的库

你可以使用D3.JS来完成。完整的教程在这里:http://bl.ocks.org/mbostock/3885304—您可以简单地指向一个CSV文件,然后根据需要对其进行分析/呈现。它最终会生成一个SVG,如果你需要的话,它还会为你提供一些你需要做交互的东西。

来自同一教程的示例代码:

<!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.org/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>

上面的答案似乎很好,但是svg的学习曲线可能有点陡峭。您可能会发现结合使用PHP和PHP的GD图形库(它是PHP标准的一部分)会更容易。这样,您只需使用fgetcsv获取数据—可以在http://php.net/manual/en/function.fgetcsv.php上找到一个非常基本的教程—然后基本上可以用一种语言完成所有工作,这是掌握php或任何其他语言的最佳方式。

这里有一个非常好的,功能齐全的GD教程:http://www.phpbuilder.com/columns/nasser20030219.php3?page=2

希望有帮助。

您可以使用类似的内容(参见url)

var chart = c3.generate({
        bindto: '#chart_c3_donut',
        data: {
          url: 'c3_donut.csv',

我更正了…这段代码运行良好…

<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