我正在使用ChartJS的窗體。我想要做的是從字段中獲取值來繪製特定的圖表分段。我想使用Knockout數據綁定來獲取圖表以在字段中的數字更改時進行刷新。用Knockout自動更新ChartJS數據綁定
由於某些原因,當我輸入一個數字時,它不會自動刷新。但是,當我輸入一個數字並點擊刷新瀏覽器中的號碼時,
任何人都可以幫助我,使綁定更新實時?
<body>
<form>
<input name="ValueStream" id="ValueStream" data-bind="value: ValueStream, valueUpdate: 'input'"></input>
<input type="text" name="ProductService" id="ProductService" value="200">
<input type="submit">
</form>
<div id="canvas-holder" style="width:30%">
<canvas id="chart-area" width="300" height="300"/>
</div>
<script>
var pAndS = document.getElementById("ProductService").value;
var vS = document.getElementById("ValueStream").value;
var polarData = [
{
value: pAndS,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: vS,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
window.myPolarArea = new Chart(ctx).PolarArea(polarData, {
responsive:true
});
};
</script>
</body>
這裏沒有更新圖表,只在頁面加載時繪製。您可以[訂閱](http://knockoutjs.com/documentation/observables.html#explicitly-subscribeing-to-observables)更改您的視圖模型,或編寫[自定義綁定處理程序](http://knockoutjs.com /documentation/custom-bindings.html) –