Javascript 使用pusher和canvas js的实时图表
Javascript 使用pusher和canvas js的实时图表,javascript,charts,real-time,pusher,Javascript,Charts,Real Time,Pusher,嗨,我在用。在我上面的代码中,我如何访问标签的值。我以JSON.stringifydps[0].name的形式访问y的值-它返回125个值。但我需要实现管理部、生产实验室等标签。。这样我就可以跟踪触发推送器事件时应该更新哪个标签。您必须使用ajax。在updateChart上插入一个ajax,看这里 <script type="text/javascript"> window.onload = function () { // initial values of dataPo
嗨,我在用。在我上面的代码中,我如何访问标签的值。我以JSON.stringifydps[0].name的形式访问y的值-它返回125个值。但我需要实现管理部、生产实验室等标签。。这样我就可以跟踪触发推送器事件时应该更新哪个标签。您必须使用ajax。在updateChart上插入一个ajax,看这里
<script type="text/javascript">
window.onload = function () {
// initial values of dataPoints
var dps = [
{label: "Management Wing", y: 125} ,
{label: "Production Lab", y: 332},
{label: "Cafeteria", y: 55},
{label: "Library", y: 46},
{label: "Recreation Centre", y: 32}
];
var totalEmployees = "total people on campus: 590";
var chart = new CanvasJS.Chart("chartContainer",{
theme: "theme2",
title:{
text: "People On Campus"
},
axisY: {
title: "Number of People"
},
legend:{
verticalAlign: "top",
horizontalAlign: "centre",
fontSize: 18
},
data : [{
type: "column",
showInLegend: true,
legendMarkerType: "none",
legendText: totalEmployees,
indexLabel: "{y}",
dataPoints: dps
}]
});
// renders initial chart
chart.render();
var sum = 590; //initial sum
var updateInterval = 1000; // milliseconds
var updateChart = function () {
// Selecting a random dataPoint
var dataPointIndex = Math.round(Math.random()*4);
// generating random value
var deltaY = Math.round(2 + Math.random() *(-2-2));
// adding random value to random dataPoint
dps[dataPointIndex].y = (dps[dataPointIndex].y + deltaY) > 0 ? dps[dataPointIndex].y + deltaY : 0 ;
// updating legend text.
sum = sum + deltaY;
totalEmployees = "total people on campus: " + sum;
chart.options.data[0].legendText = totalEmployees;
chart.render();
};
// update chart after specified interval
setInterval(function(){updateChart()}, updateInterval);
}
</script>
<script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>
</head>
的URL提供的输出与var dps上的数据类似
别忘了在canvasjs.min.js上加载jquery库。看起来像这里
// global variable label n y
var labelJSON = [];
var yJSON = [];
var updateChart = function () {
$.getJSON("http://www.domain.com/data/json", function(json){
for(var i=0; i<json.length; i++) {
labelJSON[i] = json[i].label
yJSON[i] = json[i].y;
}
});
for (var i = 0; i < dps.length; i++) {
// update chart here
dps[i] = {label: labelJSON[i] , y: yJSON[i]};
};
chart.render();
};
我希望它有用
<script type="text/javascript" src="/assets/script/jquery.min.js"></script
<script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>