gpt4 book ai didi

javascript - Google Charts - Google Visualization arraytoDataTable 无法识别数值,除非 "treated"

转载 作者:行者123 更新时间:2023-11-29 22:46:57 27 4
gpt4 key购买 nike

看看this JsFiddle .

我正在尝试从表单字段导入一个值,将其分配给一个变量,然后将其传递给 google.visualization.arrayToDataTable

但在这两种情况下,甚至强制将 type: 'number' 分配为 in this answer我总是会遇到某种错误:

案例 1:

<form name="myForm">
<input type="number" name="tot_btc" id="tot_btc" value="250">
var points  = document.forms.myForm.tot_btc.value;

var data = google.visualization.arrayToDataTable([
['Name', {label: 'Donuts eaten', type: 'number'}],
//['Name', 'Donuts eaten'],
['Michael' , points],
['Elisa', 7],
['Robert', 3],
['John', 2],
['Jessica', 6],
['Aaron', 1],
['Margareth', 8]
]);

我遇到这种错误:

enter image description here

如您所见,已确认该值,但未绘制图表:它是空白的。


案例 2:

<form name="myForm">
<input type="number" name="tot_btc" id="tot_btc" value="250">
        var data = google.visualization.arrayToDataTable([
//['Name', {label: 'Donuts eaten', type: 'number'}],
['Name', 'Donuts eaten'],
['Michael' , points],
['Elisa', 7],
['Robert', 3],
['John', 2],
['Jessica', 6],
['Aaron', 1],
['Margareth', 8]
]);

我删除了type: 'number' 赋值,结果甚至没有识别出数字。

enter image description here


案例 3:

现在,看看这个:

但是 如果我从变量中取出数字并执行任何简单的数学运算,瞧!数字被识别并且图表被绘制..即使没有强制类型:'number'识别:

enter image description here

谁能解释一下?

谢谢

最佳答案

虽然你设置了<input>作为型号,
value属性总是会返回一个字符串,这里...

    var points  = document.forms.myForm.tot_btc.value;

来自 MDN - HTMLInputElement - Properties ,可以看到属性type = string...

value string: Returns / Sets the current value of the control.


必须转换为实际数字,
在将值提供给 arrayToDataTable 之前

这就是为什么对变量执行数学运算会导致它起作用的原因。

要更正,将值解析为数字,
使用 parseIntparseFloat ...

    var points  = parseFloat(document.forms.myForm.tot_btc.value);

请参阅以下工作片段...

      google.charts.load('current', {'packages':['corechart', 'controls']});
google.charts.setOnLoadCallback(drawStuff);

function drawStuff() {

var dashboard = new google.visualization.Dashboard(
document.getElementById('programmatic_dashboard_div'));

// We omit "var" so that programmaticSlider is visible to changeRange.
var programmaticSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'programmatic_control_div',
'options': {
'filterColumnLabel': 'Donuts eaten',
'ui': {'labelStacking': 'vertical'}
}
});
// var points = document.forms["myForm"]["points"].value;
var points = parseFloat(document.forms.myForm.tot_btc.value);
var cazzo = 81;

var programmaticChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'programmatic_chart_div',
'options': {
'width': 300,
'height': 300,
'legend': 'none',
'chartArea': {'left': 15, 'top': 15, 'right': 0, 'bottom': 0},
'pieSliceText': 'value'
}
});

var data = google.visualization.arrayToDataTable([
// ['Name', {label: 'Donuts eaten', type: 'number'}],
['Name', 'Donuts eaten'],
['Michael' , points],
['Elisa', 7],
['Robert', 3],
['John', 2],
['Jessica', 6],
['Aaron', 1],
['Margareth', 8]
]);

dashboard.bind(programmaticSlider, programmaticChart);
dashboard.draw(data);

changeRange = function() {
programmaticSlider.setState({'lowValue': 2, 'highValue': 5});
programmaticSlider.draw();
};

changeOptions = function() {
programmaticChart.setOption('is3D', true);
programmaticChart.draw();
};
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="programmatic_dashboard_div" style="border: 1px solid #ccc">
<table class="columns">
<tr>
<td>
<div id="programmatic_control_div" style="padding-left: 2em; min-width: 250px"></div>
<form name="myForm">
<input type="number" name="tot_btc" id="tot_btc" value="2">
<input type="range" onmousedown="return showInput()" onmouseup="return showInput()" name="points" min="0" max="10">
</form>
<div>
<button style="margin: 1em 1em 1em 2em" onclick="changeRange();">
Select range [2, 5]
</button><br />
<button style="margin: 1em 1em 1em 2em" onclick="changeOptions();">
Make the pie chart 3D
</button>
</div>
<script type="text/javascript">
function changeRange() {
programmaticSlider.setState({'lowValue': 2, 'highValue': 5});
programmaticSlider.draw();
}

function changeOptions() {
programmaticChart.setOption('is3D', true);
programmaticChart.draw();
}
</script>
</td>
<td>
<div id="programmatic_chart_div"></div>
</td>
</tr>
</table>
</div>

关于javascript - Google Charts - Google Visualization arraytoDataTable 无法识别数值,除非 "treated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58187040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com