如何将jqplot添加到自定义Joomla(3.3)组件?

时间:2014-06-22 12:09:31

标签: php jquery joomla jqplot

在我开始使用Joomla之前,我开发了一个使用jqplot的html页面,并且页面工作正常。

现在我想在我开发的自定义joomla(3.3)组件中包含jqplot,但是当调用组件时(通过主菜单项),不显示任何图表。

更新DEFAULT.PHP(JOOMLA)代码进一步评论:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();

//add jqplot libraries
JHtml::_('jquery.framework');
$document->addScript(JPATH_ROOT.'/media/system/js/jquery.jqplot.min.js');
$document->addStyleSheet(JPATH_ROOT.'/media/system/js/jquery.jqplot.min.css');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.barRenderer.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.categoryAxisRenderer.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.pointLabels.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.enhancedLegendRenderer.js');
$document->addScript(JPATH_ROOT.'/media/system/js/weqlib.js');

?>

<head>
<script type="text/javascript">

    jQuery(document).ready(function(){
        var plot1 = jQuery.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); //copied from example at http://www.jqplot.com/tests/line-charts.php

    }); //$(document).ready
</script>
</head>

<!--<h1><?php echo $this->msg; ?></h1>-->
<h1>Prosumer Dashboard </h1>

<div id="chart1" style="width:600px; height:250px;"> </div>

我认为我调用libabries的方式是错误的(我知道jqplot函数调用是肯定的,因为我也从旧的html文件中复制了它)。

知道我做错了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您应该通过以下方式将javascript添加到页面的头部...

<?php
$document = JFactory::getDocument();
$document->addScript('/media/system/js/sample.js');
?>

您的脚本目前将尝试加载与网页网址相关的javascript,而不是与您网站的网址相关,因此无法找到它们。

另外,Joomla 3附带了JQuery - 像这样加载它(可能在你的模板而不是你的组件中)......

JHtml::_('jquery.framework');

请注意,这不会在冲突模式下运行,因此您必须更换任何&#39; $&#39;在你的jquery脚本中使用&#39; jQuery&#39;。你也可以让它在冲突模式下运行......

JHtml::_('jquery.framework', false);

但是,如果您的网站有任何其他库正在运行,这可能会导致问题。