使用按钮更新图表和文本 (D3.js)

我想制作 3 个图表,当用户单击按钮时可以更新(一一显示)。像这样:

https://img.mukewang.com/64febf190001bb0e06120411.jpg

但我也想在图表下方添加一个文本(h3 元素)。这是我的代码: HTML:


<!DOCTYPE html>

<html>


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>


<body>

    <button onclick="update(pageViews)">Page Views</button>

    <button onclick="update(users)">Reader</button>

    <button onclick="update(BounceRate)">Bounce Rate</button>


    <div id="myChart"></div>

    <h3>

        Total of Page View on March is xxxx.

    </h3>

    <script src="https://d3js.org/d3.v4.js"></script>

    <script src="stackOverflow.js" charset="utf-8"></script>

</body>


</html>

图表运行良好,但文本不会改变。有人可以帮忙吗?我认为我在 if 语句中犯了一些错误,但我仍然无法弄清楚。这是我的 JSFiddle:https://jsfiddle.net/tw0jbd81/1/


江户川乱折腾
浏览 67回答 1
1回答

慕田峪7331174

因此,您将数据集作为参数传递给全局变量中单击按钮时的更新函数。这部分也没什么用if (update(pageViews)) {&nbsp; &nbsp; tulisanBawah.innerHTML = "Total page views of march";&nbsp; &nbsp; console.log("pageviews");} else if (update(users)) {&nbsp; &nbsp; tulisanBawah.innerHTML = "Total users of march";&nbsp; &nbsp; console.log("users");}因为所做的事情是,它在第一个 IF 条件中调用 update(pageViews),该条件解析为未定义(因为该函数不返回任何内容),然后它再次调用 update(users),该条件再次解析为未定义,因此条件体不会被触发。我会用一种不同的方式来做这件事将函数参数设置为我们要使用的字符串数据集名称,这样我们就可以从 HTML vie update('pageViews') 中调用它调用脚本页脚的 update 方法将标题更新登录移至更新功能内附加按钮回调,例如页面视图为了更新H3元素的内容,使用innerText,而不是innerHTML所以最终的JS会是这样的var pageViews = [&nbsp; &nbsp; { ser1: 1, ser2: 3625 },&nbsp; &nbsp; { ser1: 2, ser2: 2698 },&nbsp; &nbsp; { ser1: 3, ser2: 2441 },&nbsp; &nbsp; { ser1: 4, ser2: 2399 },&nbsp; &nbsp; { ser1: 5, ser2: 2342 },&nbsp; &nbsp; { ser1: 6, ser2: 2279 },&nbsp; &nbsp; { ser1: 7, ser2: 2087 },&nbsp; &nbsp; { ser1: 8, ser2: 2070 },&nbsp; &nbsp; { ser1: 9, ser2: 2490 },&nbsp; &nbsp; { ser1: 10, ser2: 2535 },&nbsp; &nbsp; { ser1: 11, ser2: 3468 },&nbsp; &nbsp; { ser1: 12, ser2: 2526 },&nbsp; &nbsp; { ser1: 13, ser2: 0 },&nbsp; &nbsp; { ser1: 14, ser2: 0 },&nbsp; &nbsp; { ser1: 15, ser2: 0 },&nbsp; &nbsp; { ser1: 16, ser2: 0 },&nbsp; &nbsp; { ser1: 17, ser2: 0 },&nbsp; &nbsp; { ser1: 18, ser2: 0 },&nbsp; &nbsp; { ser1: 19, ser2: 0 },&nbsp; &nbsp; { ser1: 20, ser2: 0 },&nbsp; &nbsp; { ser1: 21, ser2: 0 },&nbsp; &nbsp; { ser1: 22, ser2: 0 },&nbsp; &nbsp; { ser1: 23, ser2: 0 },&nbsp; &nbsp; { ser1: 24, ser2: 0 },&nbsp; &nbsp; { ser1: 25, ser2: 0 },&nbsp; &nbsp; { ser1: 26, ser2: 0 },&nbsp; &nbsp; { ser1: 27, ser2: 0 },&nbsp; &nbsp; { ser1: 28, ser2: 0 },&nbsp; &nbsp; { ser1: 29, ser2: 0 },&nbsp; &nbsp; { ser1: 30, ser2: 0 },&nbsp; &nbsp; { ser1: 31, ser2: 0 }];var users = [&nbsp; &nbsp; { ser1: 1, ser2: 1332 },&nbsp; &nbsp; { ser1: 2, ser2: 956 },&nbsp; &nbsp; { ser1: 3, ser2: 909 },&nbsp; &nbsp; { ser1: 4, ser2: 924 },&nbsp; &nbsp; { ser1: 5, ser2: 864 },&nbsp; &nbsp; { ser1: 6, ser2: 799 },&nbsp; &nbsp; { ser1: 7, ser2: 824 },&nbsp; &nbsp; { ser1: 8, ser2: 812 },&nbsp; &nbsp; { ser1: 9, ser2: 881 },&nbsp; &nbsp; { ser1: 10, ser2: 926 },&nbsp; &nbsp; { ser1: 11, ser2: 1330 },&nbsp; &nbsp; { ser1: 12, ser2: 964 },&nbsp; &nbsp; { ser1: 13, ser2: 0 },&nbsp; &nbsp; { ser1: 14, ser2: 0 },&nbsp; &nbsp; { ser1: 15, ser2: 0 },&nbsp; &nbsp; { ser1: 16, ser2: 0 },&nbsp; &nbsp; { ser1: 17, ser2: 0 },&nbsp; &nbsp; { ser1: 18, ser2: 0 },&nbsp; &nbsp; { ser1: 19, ser2: 0 },&nbsp; &nbsp; { ser1: 20, ser2: 0 },&nbsp; &nbsp; { ser1: 21, ser2: 0 },&nbsp; &nbsp; { ser1: 22, ser2: 0 },&nbsp; &nbsp; { ser1: 23, ser2: 0 },&nbsp; &nbsp; { ser1: 24, ser2: 0 },&nbsp; &nbsp; { ser1: 25, ser2: 0 },&nbsp; &nbsp; { ser1: 26, ser2: 0 },&nbsp; &nbsp; { ser1: 27, ser2: 0 },&nbsp; &nbsp; { ser1: 28, ser2: 0 },&nbsp; &nbsp; { ser1: 29, ser2: 0 },&nbsp; &nbsp; { ser1: 30, ser2: 0 },&nbsp; &nbsp; { ser1: 31, ser2: 0 }];var BounceRate = [&nbsp; &nbsp; { ser1: 1, ser2: 5.68 },&nbsp; &nbsp; { ser1: 2, ser2: 4.49 },&nbsp; &nbsp; { ser1: 3, ser2: 5.29 },&nbsp; &nbsp; { ser1: 4, ser2: 5.74 },&nbsp; &nbsp; { ser1: 5, ser2: 6.14 },&nbsp; &nbsp; { ser1: 6, ser2: 3.95 },&nbsp; &nbsp; { ser1: 7, ser2: 6.03 },&nbsp; &nbsp; { ser1: 8, ser2: 5.08 },&nbsp; &nbsp; { ser1: 9, ser2: 5.1 },&nbsp; &nbsp; { ser1: 10, ser2: 4.78 },&nbsp; &nbsp; { ser1: 11, ser2: 3.84 },&nbsp; &nbsp; { ser1: 12, ser2: 5.75 },&nbsp; &nbsp; { ser1: 13, ser2: 0 },&nbsp; &nbsp; { ser1: 14, ser2: 0 },&nbsp; &nbsp; { ser1: 15, ser2: 0 },&nbsp; &nbsp; { ser1: 16, ser2: 0 },&nbsp; &nbsp; { ser1: 17, ser2: 0 },&nbsp; &nbsp; { ser1: 18, ser2: 0 },&nbsp; &nbsp; { ser1: 19, ser2: 0 },&nbsp; &nbsp; { ser1: 20, ser2: 0 },&nbsp; &nbsp; { ser1: 21, ser2: 0 },&nbsp; &nbsp; { ser1: 22, ser2: 0 },&nbsp; &nbsp; { ser1: 23, ser2: 0 },&nbsp; &nbsp; { ser1: 24, ser2: 0 },&nbsp; &nbsp; { ser1: 25, ser2: 0 },&nbsp; &nbsp; { ser1: 26, ser2: 0 },&nbsp; &nbsp; { ser1: 27, ser2: 0 },&nbsp; &nbsp; { ser1: 28, ser2: 0 },&nbsp; &nbsp; { ser1: 29, ser2: 0 },&nbsp; &nbsp; { ser1: 30, ser2: 0 },&nbsp; &nbsp; { ser1: 31, ser2: 0 }];// set the dimensions and margins of the graphvar margin = { top: 10, right: 30, bottom: 20, left: 50 },&nbsp; &nbsp; width = 650 - margin.left - margin.right,&nbsp; &nbsp; height = 400 - margin.top - margin.bottom;// append the svg object to the body of the pagevar svg = d3&nbsp; &nbsp; .select("#myChart")&nbsp; &nbsp; .append("svg")&nbsp; &nbsp; .attr("width", width + margin.left + margin.right)&nbsp; &nbsp; .attr("height", height + margin.top + margin.bottom)&nbsp; &nbsp; .append("g")&nbsp; &nbsp; .attr("transform", "translate(" + margin.left + "," + margin.top + ")");// Initialise a X axis:var x = d3.scaleLinear().range([0, width]);var xAxis = d3.axisBottom().scale(x);svg&nbsp; &nbsp; .append("g")&nbsp; &nbsp; .attr("transform", "translate(0," + height + ")")&nbsp; &nbsp; .attr("class", "myXaxis");// Initialize an Y axisvar y = d3.scaleLinear().range([height, 0]);var yAxis = d3.axisLeft().scale(y);svg.append("g").attr("class", "myYaxis");var title = document.querySelector("#title");// Create a function that takes a dataset as input and update the plot:function update(dataSetName) {&nbsp; &nbsp; if (dataSetName === "pageViews") {&nbsp; &nbsp; &nbsp; &nbsp; var data = pageViews&nbsp; &nbsp; &nbsp; title.innerText = "Page Views"&nbsp; &nbsp; }&nbsp; &nbsp; else if (dataSetName === "users") {&nbsp; &nbsp; &nbsp; &nbsp; var data = users&nbsp; &nbsp; &nbsp; title.innerText = "Users"&nbsp; &nbsp; }&nbsp; &nbsp; else if (dataSetName === "BounceRate") {&nbsp; &nbsp; &nbsp; &nbsp; var data = BounceRate&nbsp; &nbsp; &nbsp; title.innerText = "Bounce Rate"&nbsp; &nbsp; }&nbsp; &nbsp; // Create the X axis:&nbsp; &nbsp; x.domain([&nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; d3.max(data, function (d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d.ser1;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; ]);&nbsp; &nbsp; svg&nbsp; &nbsp; &nbsp; &nbsp; .selectAll(".myXaxis")&nbsp; &nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; &nbsp; .duration(2500)&nbsp; &nbsp; &nbsp; &nbsp; .call(xAxis);&nbsp; &nbsp; // create the Y axis&nbsp; &nbsp; y.domain([&nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; d3.max(data, function (d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d.ser2;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; ]);&nbsp; &nbsp; svg&nbsp; &nbsp; &nbsp; &nbsp; .selectAll(".myYaxis")&nbsp; &nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; &nbsp; .duration(2500)&nbsp; &nbsp; &nbsp; &nbsp; .call(yAxis);&nbsp; &nbsp; // Create a update selection: bind to the new data&nbsp; &nbsp; var u = svg.selectAll(".lineTest").data([data], function (d) {&nbsp; &nbsp; &nbsp; &nbsp; return d.ser1;&nbsp; &nbsp; });&nbsp; &nbsp; // Update the line&nbsp; &nbsp; u.enter()&nbsp; &nbsp; &nbsp; &nbsp; .append("path")&nbsp; &nbsp; &nbsp; &nbsp; .attr("class", "lineTest")&nbsp; &nbsp; &nbsp; &nbsp; .merge(u)&nbsp; &nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; &nbsp; .duration(2500)&nbsp; &nbsp; &nbsp; &nbsp; .attr(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "d",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .line()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .x(function (d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return x(d.ser1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .y(function (d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return y(d.ser2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; .attr("fill", "none")&nbsp; &nbsp; &nbsp; &nbsp; .attr("stroke", "#ef504d")&nbsp; &nbsp; &nbsp; &nbsp; .attr("stroke-width", 5);&nbsp; &nbsp; // update tulisan bawah}update('pageViews')请参阅此处的 JS 小提琴&nbsp;https://jsfiddle.net/modularcoder/tLv3pusb/21/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5