duanlijia5864 2014-01-04 02:12
浏览 58

D3JS没有在WAMP上工作

EDIT 2: I found this article and created a .htaccess file in the root dir of my site in wamp with the following two lines:

AddType image/svg+xml svg svgz
AddEncoding gzip svgz

But still no errors and no charts. Could it be a WAMP set up issue? As I said this code works on my online site...


EDIT: I placed an "alert(d.max_energy)" in the code and I can see the JSON values returning to the client page. It just does not get processed into a chart nor complain with an error.


I am sure this is a simple fix but I cannot see it. I have the following code on a hosted site which works OK, and have now replicated in WAMP local server (a different machine in my network from the development PC), but does not execute / cause an error in Console / Inspect.

Any help is appreciated.

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
jQuery( document ).ready(function() {
    CreateBarChart( "http://10.0.0.13/sma/sma-php/inverterdata.php?var=PDAY&id=P100023", "#daychart" );
    CreateBarChart( "http://10.0.0.13/sma/sma-php/inverterdata.php?var=PWEEK&id=P100023", "#weekchart" );
    CreateBarChart( "http://10.0.0.13/sma/sma-php/inverterdata.php?var=PMONTH&id=P100023", "#monthchart" );
    CreateBarChart( "http://10.0.0.13/sma/sma-php/inverterdata.php?var=PYEAR&id=P100023", "#yearchart" );
    CreateBarChart( "http://10.0.0.13/sma/sma-php/inverterdata.php?var=PLIFE&id=P100023", "#lifechart" );
});

function CreateBarChart(url, divid) {   

var margin = {top: 20, right: 0, bottom: 30, left: 30},
    width = 838 - margin.left - margin.right,
    height = 300 - 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(divid)
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.json(url, function(error, data) {
    data.forEach(function(d) {
        d.max_energy = +d.max_energy;
    });

  x.domain(data.map(function(d) { return d.xaxis; }));
  y.domain([0, d3.max(data, function(d) { return d.max_energy ; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .append("text")
      .attr("transform", "rotate(0)")
      .attr("y", 23)
      .attr("x", 340)
      .attr("dy", ".71em")
      .style("text-anchor", "bottom")
      .text("Time / Date / Month / Year");

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(0)")
      .attr("y", -15)
      .attr("x", -25)
      .attr("dy", ".71em")
      .style("text-anchor", "top")
      .text("Energy - KWh");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.xaxis); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.max_energy); })
      .transition().delay(function (d,i){ return i * 10;}).duration(10)
      .attr("height", function(d) { return height - y(d.max_energy); })

  svg.selectAll(".label")
      .data(data)
    .enter().append("svg:text")
      .attr("class", "label")
      .attr("x", function(d) {
          return x(d[d.xaxis]) + x.rangeBand() / 3;
      })
      .attr("y", function(d) {
          return y(d[d.max_energy]) - 5;
      })
      .text(function(d) {
          return y(d[d.max_energy]);
      });;

});
};
</script>

in the inverterdata.php file I have the following code:

<?php
    $param = $_GET["var"];
    $id = $_GET["id"];

    $username = "root"; 
    $password = "";   
    $host = "localhost";
    $database="sma";

    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);

switch ($param) {
    case "CDAY":
    $myquery = "SELECT  poll_hour as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE customer_code = '".$id."' AND poll_day = DAY(DATE(NOW())) AND poll_month = MONTH(DATE(NOW())) AND poll_year = YEAR(DATE(NOW())) GROUP BY xaxis ORDER BY xaxis";
    break;
    case "CWEEK":
    $myquery = "SELECT  CONCAT(LPAD(poll_month, 2, '0'), LPAD(poll_day, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE customer_code = '".$id."' AND poll_year = YEAR(NOW()) AND poll_month = MONTH(NOW()) AND poll_day > DAY(NOW()) - 8 GROUP BY xaxis ORDER BY xaxis";
    break;
    case "CMONTH":
    $myquery = "SELECT  CONCAT(LPAD(poll_month, 2, '0'), LPAD(poll_day, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE customer_code = '".$id."' AND poll_year = YEAR(NOW()) AND poll_month = MONTH(NOW()) GROUP BY xaxis ORDER BY xaxis";
    break;
    case "CYEAR":
    $myquery = "SELECT  CONCAT(poll_year, LPAD(poll_month, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE customer_code = '".$id."' AND poll_year = YEAR(NOW()) GROUP BY xaxis ORDER BY xaxis";
    break;
    case "CLIFE":
    $myquery = "SELECT  poll_year as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE customer_code = '".$id."' GROUP BY xaxis ORDER BY xaxis";
    break;
    case "PDAY":
    $myquery = "SELECT  poll_hour as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE partner_code = '".$id."' AND poll_day = '28' AND poll_month = '12' AND poll_year = '2013' GROUP BY xaxis ORDER BY xaxis";
    break;
    case "PWEEK":
    $myquery = "SELECT  CONCAT(LPAD(poll_month, 2, '0'), LPAD(poll_day, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE partner_code = '".$id."' AND poll_year = '2013' AND poll_month = '12' AND poll_day > '21' GROUP BY xaxis ORDER BY xaxis";
    break;
    case "PMONTH":
    $myquery = "SELECT  CONCAT(LPAD(poll_month, 2, '0'), LPAD(poll_day, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE partner_code = '".$id."' AND poll_year = '2013' AND poll_month = '12' GROUP BY xaxis ORDER BY xaxis";
    break;
    case "PYEAR":
    $myquery = "SELECT  CONCAT(poll_year, LPAD(poll_month, 2, '0')) as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE partner_code = '".$id."' AND poll_year = '2013' GROUP BY xaxis ORDER BY xaxis";
    break;
    case "PLIFE":
    $myquery = "SELECT  poll_year as xaxis, SUM(energy) as max_energy, SUM(efficiency) as max_efficiency FROM sma_inverter_data WHERE partner_code = '".$id."' GROUP BY xaxis ORDER BY xaxis";
    break;
}

    $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_free_result($query);

    mysql_close($server);
?>

When I execute manually via browser address bar one of the PHP script calls I get back valid JSON as follows:

http://10.0.0.13/sma/sma-php/inverterdata.php?var=PLIFE&id=P100023

JSON:

[{"xaxis":"2012","max_energy":"305923.303","max_efficiency":"13019.572"},{"xaxis":"2013","max_energy":"410400.643","max_efficiency":"17466.284"},{"xaxis":"2014","max_energy":"4538.187","max_efficiency":"193.140"}]

My HTML inside the tag is:

<svg id="daychart"></svg>
<svg id="weekchart"></svg>
<svg id="monthchart"></svg>
<svg id="yearchart"></svg>
<svg id="lifechart"></svg>

Thanks

  • 写回答

1条回答 默认 最新

  • doutao5419 2014-01-04 04:35
    关注

    Duh! I knew it was in front of me but could not see it... when I added the tags to the page body in word press I did it in the "visual" box not the "text" box, and this screwed up all the tags...

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?