我正在研究 jQuery 条形图。
使用日期范围搜索空闲和占用空间,进入和退出总数,
以下快照将简要介绍搜索结果;
Search result http://www.shehary.com/stackimages/search-result.jpg
而 php forearch 是;
<?php if(count($occupied) < 1) return; foreach ($occupied as $key=>$value): ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $total_space; ?></td>
<td><?php echo $real_data[$key] + $dep_data[$key];?></td>
<td><?php echo $total_space - $real_data[$key] - $dep_data[$key]; ?></td>
<td><?php echo (array_key_exists($key, $real_data))?$real_data[$key]:0;?></td>
<td><?php echo (array_key_exists($key, $dep_data))?$dep_data[$key]:0;?></td>
</tr>
<?php endforeach; ?>
条形字符看起来像这样 Search result http://www.shehary.com/stackimages/barchart.jpg
以下是jQuery代码;
$(function(){
$('#graph').graphify({
//options: true,
start: 'bar',
obj: {
id: 'ggg',
width: '100%',
height: 375,
xGrid: true,
legend: true,
title: 'Departure vs Return',
points: [
[7, 26, 33, 74, 12, 49, 33, 33, 74, 12, 49, 33, 178, 160, 33, 74, 12, 49, 33, 178, 160, 178, 160, 33, 74, 12, 49, 33, 178, 160,450],
[32, 46, 75, 38, 62, 20, 52, 75, 38, 62, 20, 52, 134, 145, 52, 75, 38, 62, 20, 52, 134, 145, 145, 52, 75, 38, 62, 20, 52, 134, 145,300]
],
pointRadius: 3,
colors: ['#428bca', '#1caf9a'],
xDist: 40,
dataNames: ['Departure', 'Return'],
xName: 'Day',
tooltipWidth: 15,
animations: true,
pointAnimation: true,
averagePointRadius: 5,
design: {
tooltipColor: '#fff',
gridColor: '#f3f1f1',
tooltipBoxColor: '#d9534f',
averageLineColor: '#d9534f',
pointColor: '#d9534f',
lineStrokeColor: 'grey',
}
}
});
});
Private.defaults = function() {
return {
//Days or Date Range
x: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
y: 20,
attachTo: 'body'
};
};
和 HTML 来显示条形图;
<div id="graph"></div>
我需要帮助如何根据日期范围将值放入数组并加载到条形图中。
<删除> 要点:[ [''], [''] ], X: ['']已编辑和更新的问题
@Marcos Dimitrio 的回答指出我之前使用了错误的数组作为引用;我很抱歉,以下是正确的 depart n 返回数组;
points: [
['<?php echo $real_data[$key];?>'],
['<?php echo $dep_data[$key]; ?>']
],
x: ['<?php echo $key; ?>']//No of Days in X-Axis If no x-axis arrays define, chart will not be loaded.
并且在根据您的说明使用代码作为答案后,我得到了这个,我手动定义了天数(x 轴),例如 [1,2,3,4,5,6,7,8,9,10 ]
No-Bars http://www.shehary.com/stackimages/no-bars.jpg No Data in Table http://www.shehary.com/stackimages/bar-chart-table.jpg
以下是其余的 php 代码;
$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
include_once 'parkingdetail.php'; //This file is doing all the calculation
//Add one day to today
$real_data = array();
$total_space = 0;
$dep_data = array();
$occupied = array();
getParkData($to,$total_space,$real_data,$dep_data,$occupied,$av,$tp,$tpbooking,$from);
ksort($occupied);
//$total_space is fixed 2000
//$real_data is Depart
//$dep_data is Return
//$occupied is total sim of $real_data+$dep_data
删除>
最佳答案
首先,您可以将数据过滤到新数组中:
<?php
$start = "01-June-2015";
$end = "03-June-2015";
$points_departure = array();
$points_return = array();
foreach (array_keys($occupied) as $key) {
if (isWithinRange($key, $start, $end)) {
$points_departure[] = $real_data[$key] + $dep_data[$key];
$points_return[] = $total_space - $real_data[$key] - $dep_data[$key];
}
}
function isWithinRange($key, $start, $end) {
$keyDate = DateTime::createFromFormat ("d-M-Y", $key);
$startDate = DateTime::createFromFormat ("d-M-Y", $start);
$endDate = DateTime::createFromFormat ("d-M-Y", $end);
return $keyDate >= $startDate and $keyDate <= $endDate;
}
之后,您需要将它们发送到 JavaScript。您可以通过 AJAX 来完成,或者您可以使用更简单的方法并将其放在页面顶部:
<script>
var points_departure = <?php echo json_encode($points_departure); ?>;
var points_return = <?php echo json_encode($points_return); ?>;
</script>
这会给你:
<script>
var points_departure = [7,26,33];
var points_return = [32, 46, 75];
</script>
然后简单地用您创建的变量替换点数据:
points: [
points_departure,
points_return
],
关于javascript - 在 jQuery 条形图中加载 php foreach 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30745224/
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
我有一个Highstock图表(带有标记和阴影的线条),并且想以编程方式显示一个highstock工具提示,例如,当我选择某个表上的一行(包含图表数据)我想显示相应的highstock工具提示。这可能吗? 最佳答案 股票图表thissolution不起作用:在thisexample你必须更换这个:chart.tooltip.refresh(chart.series[0].data[i]);为此:chart.tooltip.refresh([chart.series[0].points[i]]);解决方案可用here.
我以为它已经安装了,但在我的gemfile中有gem"jquery-rails"但是在我的asset/javascripts文件夹中accounts.js.coffeeapplication.js都被注释掉了这是我的虚拟railsapplication但是在源代码中没有jQuery并且删除链接不起作用......任何想法都丢失了 最佳答案 看看thisRailscast.您可能需要检查application.js文件并确保它包含以下语句。//=requirejquery//=requirejquery_ujs
我想在页面顶部创建自定义Jquery消息而不是标准RailsFlash消息。我想在我的投票底部附近创建一条即时消息。我的Controller:defvote_up@post=Post.find(params[:id])current_user.up_vote(@post)flash[:message]='Thanksforvoting!'redirect_to(root_path,:notice=>'Takforditindlæg,deternuonline!')rescueMakeVoteable::Exceptions::AlreadyVotedErrorflash[:error]
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文
我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如
我目前从prototype切换到jquery主要是为了支持简单的ajax文件上传。我使用:https://github.com/indirect/jquery-rails95%的javascript代码是由railshelper编写的,例如:-remote_function-render:updatedo|page|-page.replace_html'id',:partial=>'content'-page['form']['name']=something-page.visual_effect:highlight,'head_success'...我知道我必须为Jquery重写5%