gpt4 book ai didi

javascript - Chart.js - 在一页上创建多个图表

转载 作者:搜寻专家 更新时间:2023-10-31 21:09:20 24 4
gpt4 key购买 nike

我想使用 chart.js在我的 joomla 组件中称为“bestia”。为此,我将 chart.js 作为媒体元素导入到 manifest.xml 中。

我通过执行以下操作调用 js:

JHtml::script(Juri::root() . 'media/com_hostinghelden/chartjs/Chart.js');

现在我想用一个助手来缩短一些事情。我的助手应该画一个图...

abstract class JHtmlGraphs 
{
public function draw($title = NULL, $labelset = NULL, $data = NULL, $data2 = NULL)
{
$script = "<script> var barChartData_$title = { labels : [ {$labelset} ], datasets : [ { fillColor : 'rgba(220,220,220,0.5)', strokeColor : 'rgba(220,220,220,0.8)', highlightFill: 'rgba(220,220,220,0.75)', highlightStroke: 'rgba(220,220,220,1)', data : [ $data ] }, { fillColor : 'rgba(151,187,205,0.5)', strokeColor : 'rgba(151,187,205,0.8)', highlightFill : 'rgba(151,187,205,0.75)', highlightStroke : 'rgba(151,187,205,1)', data : [ $data2 ] } ] }; window.onload = function(){ var ctx_$title = document.getElementById('canvas_$title').getContext('2d'); window.myBar = new Chart(ctx_$title).Bar(barChartData_$title, { responsive : true }); } </script>";
$canvas = "<div style='width: 100%'><canvas id='canvas_$title' height='20' width='20'></canvas></div>";
return $script . $canvas;
}
}

好吧,如果我调用这个 Jhtml-Graphs,图形就会正确呈现:

<?php echo JHtml::_('graphs.draw', "Titel", $labelset, $data, $data2);  ?>

但是如果我想在我的页面中添加第二个图表,第一个图表将不再显示:

<?php echo JHtml::_('graphs.draw', "Titel2", $labelset, $data, $data2); ?>

我能做什么?

最佳答案

您正在重新声明 window.onload 函数。这就是为什么您将只绘制一个(最后一个)图表。

window.onload = function(){ 
var ctx_$title = document.getElementById('canvas_$title').getContext('2d');
window.myBar = new Chart(ctx_$title).Bar(barChartData_$title, { responsive : true });

}

您可以做些什么来解决这个问题。我建议更改 JHtmlGraphs 类如下:

    abstract class JHtmlGraphs 
{
private $titles = array();

public function draw($title = NULL, $labelset = NULL, $data = NULL, $data2 = NULL)
{
$script = "<script> var barChartData_$title = { labels : [ {$labelset} ], datasets : [ { fillColor : 'rgba(220,220,220,0.5)', strokeColor : 'rgba(220,220,220,0.8)', highlightFill: 'rgba(220,220,220,0.75)', highlightStroke: 'rgba(220,220,220,1)', data : [ $data ] }, { fillColor : 'rgba(151,187,205,0.5)', strokeColor : 'rgba(151,187,205,0.8)', highlightFill : 'rgba(151,187,205,0.75)', highlightStroke : 'rgba(151,187,205,1)', data : [ $data2 ] } ] };</script>";
$canvas = "<div style='width: 100%'><canvas id='canvas_$title' height='20' width='20'></canvas></div>";

$this->titles[] = $title; //storing titles
return $script . $canvas;
}

public function finishDrawing() {
$launchCharts = '';
foreach($this->titles as $title) {
$launchCharts .= "var ctx_$title = document.getElementById('canvas_$title').getContext('2d'); window.myBar_$title = new Chart(ctx_$title).Bar(barChartData_$title, { responsive : true });";
}
$this->titles = array();

$script = "<script>window.onload = function(){ $launchCharts }</script>";
return $script;
}
}

我修改了你的脚本。现在您的 onload 函数将生成一次,来自 finishDrawing 方法:

<?php echo JHtml::_('graphs.draw', "Titel", $labelset, $data, $data2);  ?>
<?php echo JHtml::_('graphs.draw', "Titel2", $labelset, $data, $data2); ?>

在底部的某处你必须调用新函数:

<?php echo JHtml::finishDrawing(); //you better know than me how to call this function?>

关于javascript - Chart.js - 在一页上创建多个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757569/

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