티스토리 뷰

program

echarts line chart append

littlecarbb 2018. 10. 5. 21:06

출처 : https://stackoverflow.com/questions/42134738/add-values-to-data-under-series-dynamically-echarts

Add Values to data under series dynamically - echarts


출처 : https://github.com/hisune/Echarts-PHP/blob/master/demo/dynamic.php




<?php
/**
* Created by Hisune.
* User: hi@hisune.com
* Date: 2017/8/25
* Time: 09:56
*/
require('../vendor/autoload.php');
use Hisune\EchartsPHP\Config;
use Hisune\EchartsPHP\ECharts;
if(isset($_GET['ajax']) && $_GET['ajax'] == 'true'):
// Your data
echo json_encode(['num' => rand(0,100), 'data' => uniqid()]);
else:
// Your data
$data = [];
$xAxisData = [];
for($i = 0; $i < 50; $i++){
$data[] = rand(30,70);
$xAxisData[] = uniqid();
}
$chart = new ECharts();
$chart->xAxis = [
'type' => 'category',
'data' => $xAxisData,
];
$chart->yAxis[] = [
'type' => 'value',
];
$chart->series[] = [
'type' => 'line',
'data' => $data
];
Config::addExtraScript('jquery.min.js', '//cdn.bootcss.com/jquery/3.3.1');
echo $chart->render('test-id');
?>
<script>
setInterval(function(){
$.ajax({
data: {
'ajax': true
},
dataType: 'json',
success: function(json){
var echart = chart_<?=$chart->getJsVar()?>,
option = echart.getOption();
// shift a item then push a new
option.series[0].data.shift();
option.series[0].data.push(json.num);
option.xAxis[0].data.shift();
option.xAxis[0].data.push(json.data);
echart.setOption({
series: option.series,
xAxis: option.xAxis
});
}
});
}, 1000);
</script>
<?php
endif;


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함