簡體   English   中英

如何加載dc.js和crossfilter.js?

[英]How to load dc.js and crossfilter.js?

我正在嘗試使用dc.js和crossfilter.js但我總是得到

"Uncaught TypeError: Cannot read property 'format' of undefined dc.js:484
Uncaught TypeError: Cannot read property '1' of null header.js:3 
Uncaught ReferenceError: dc is not defined (index):39 "

我認為加載庫的順序不是問題,但是我無法弄清楚格式是什么以及為什么會引起問題! 我什至沒有使用外部文件加載數據My index.html

    <!DOCTYPE html>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="css/dc.css"/>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/crossfilter.js"  ></script>
    <script type="text/javascript" src="js/d3.js" ></script>
    <script type="text/javascript" src="js/dc.js"></script>
</head>

<body>

<div class="container">
<script type="text/javascript" src="js/header.js"></script>

  <p>This example demonstrates assigning a threshold dynamically to a pie chart by creating a new dimension based on slider input.</p>
    <div id="inputSlider">
        <div><input type="range" id="slideRange" value="0.5" min="0" max="1.0" step="0.1" onchange="updateSlider(this.value)"></div>
        Score threshold: <span id="sliderValue">0.5</label>
    </div>
    <div id="dc-coreAcc-piechart"></div>
    <div id="dc-score-barchart"></div>

    <script type="text/javascript">
    var data = [
                { "book": "A", "scores": 45 },
                { "book": "B", "scores": 34 },
                { "book": "C", "scores": 54 },
                { "book": "D", "scores": 27 },
                { "book": "E", "scores": 70 },
                { "book": "F", "scores": 25 },
                { "book": "G", "scores": 92 },
                { "book": "H", "scores": 22 },
                { "book": "I", "scores": 40 },
                { "book": "J", "scores": 10 },
                { "book": "K", "scores": 40 }
               ];
    //## Create Chart Objects
    var scoreChart = dc.barChart("#dc-score-barchart")
                        .xAxisLabel('book_id')
                        .yAxisLabel('score');
    var goodYesNoPieChart = dc.pieChart('#dc-coreAcc-piechart');
    //### Create Crossfilter Dimensions and Groups
    var ndx = crossfilter(data);
    var all = ndx.groupAll();
    var bookDimension = ndx.dimension(function (d) {return d.book;}),
        bookscoresGroup = bookDimension.group().reduceSum(function(d) {return d.scores;});
    //## score bar chart
    scoreChart.width(320)
        .height(320)
        .dimension(bookDimension)
        .group(bookscoresGroup)
        .elasticY(true)
        .x(d3.scale.ordinal())
        .xUnits(dc.units.ordinal)
        .colors(["orange"])
        .yAxis().ticks(5);
    //## pie chart
    // reusable function to create threshold dimension
    function coreCount_from_threshold() {
        var scoreThreshold=document.getElementById('slideRange').value;
        scoreThreshold=parseFloat(scoreThreshold);
        if (isNaN(scoreThreshold)) {
            scoreThreshold=0.5
        }
        return ndx.dimension(function (d) {
            var maxNumber=80;
            if (d.scores >maxNumber*scoreThreshold) {
                return 'High';
            } else {
                return 'Low';
            }
        });
    }
    var coreCount = coreCount_from_threshold();
    var coreCountGroup = coreCount.group();
    goodYesNoPieChart
        .width(320)
        .height(320)
        .radius(120)
        .innerRadius(40)
        .dimension(coreCount)
        .title(function(d){return d.value;})
        .group(coreCountGroup)
        .label(function (d) {
            if (goodYesNoPieChart.hasFilter() && !goodYesNoPieChart.hasFilter(d.key)) {
                return d.key + '(0%)';
            }
            var label = d.key;
            if (all.value()) {
                label += '(' + Math.floor(d.value / all.value() * 100) + '%)';
            }
            return label;
        })
    dc.renderAll();
    //## change slider score value to re-assign the data in pie chart
    function updateSlider(slideValue) {
        var sliderDiv = document.getElementById("sliderValue");
        sliderDiv.innerHTML = slideValue;
        coreCount.dispose();
        coreCount = coreCount_from_threshold();
        coreCountGroup = coreCount.group();
        goodYesNoPieChart
            .dimension(coreCount)
            .group(coreCountGroup);
        dc.redrawAll();
    }
    </script>
</div>
</body>
</html>

我在這里做錯什么了嗎? 有人可以幫我嗎

Dc.js尚不支持D3版本4。 切換到D3 v3.x,您的問題應已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM