使用Google Chart API和JSON for DataTable创建折线图(Create line chart using Google Chart API and JSON for DataTable)
我正在尝试使用Google Chart API创建折线图。 我正尝试通过AJAX帖子为数据表使用JSON设置数据。
我有一个为饼图工作的版本,但我无法找到如何为折线图做。
以下是我如何使用ajax创建图表。
function drawLineGraph() { $.post("loadGraph.php", { type: "line" }, function (result) { var data = new google.visualization.DataTable(result); var options = { title: "Line Graph Test" }; var chart = new google.visualization.LineChart(document.getElementById("lineChart")); chart.draw(data, options); }, "json" ); }
下面是loadGraph.php的代码
print json_encode(test()); function test() { $array = array(); if ($_POST['type'] == "line") { $array['cols'][] = array('type' => 'string'); $array['cols'][] = array('type' => 'number'); $temp = array(); $array['row'][] = array('v' => (string) "20-01-13"); $array['row'][] = array('v' => (int) 35); $array['row'][] = array('v' => (string) "21-01-13"); $array['row'][] = array('v' => (int) 30); } }
虽然没有发生错误,但是折线图有点显示,但没有线,就好像数据是0.下面是如何显示图表的屏幕截图
以下是JSON内容的输出。
{"cols":[{"type":"string"},{"type":"number"}],"row":[{"c":[{"v":"20-01-13"},{"v":22}]},{"c":[{"v":"21-01-13"},{"v":24}]},{"c":[{"v":"22-01-13"},{"v":27}]}]}
感谢您的任何帮助,您可以提供。
更新我试图做@davidkonrad建议但我现在遇到了另一个问题。 我已经将行的定义更改为PHP数组的行,如下所示:
$array['cols'][] = array('type' => 'string'); $array['cols'][] = array('type' => 'number'); $array['rows'][] = array('c' => "20-01-13"); $array['rows'][] = array('v' => 35); $array['rows'][] = array('c' => "21-01-13"); $array['rows'][] = array('v' => 30);
但是现在当图形加载时,我得到了
Cannot read property '0' of undefined
其中应该显示图形。下面是如何生成JSON
{"cols":[{"type":"string"},{"type":"number"}],"rows":[{"c":"20-01-13"},{"v":35},{"c":"21-01-13"},{"v":30}]}
我看不到如何更改数组,使其与davidkonrad提供的JSON相匹配
javaI am trying to create a line chart using the Google Chart API. I am trying to set the data using JSON for the datatable via a AJAX post.
I have a version working for the pie chart but I can't find out how to do it for the line chart.
Below is how I am creating the chart with the ajax post.
function drawLineGraph() { $.post("loadGraph.php", { type: "line" }, function (result) { var data = new google.visualization.DataTable(result); var options = { title: "Line Graph Test" }; var chart = new google.visualization.LineChart(document.getElementById("lineChart")); chart.draw(data, options); }, "json" ); }
Below is the code for the loadGraph.php
print json_encode(test()); function test() { $array = array(); if ($_POST['type'] == "line") { $array['cols'][] = array('type' => 'string'); $array['cols'][] = array('type' => 'number'); $temp = array(); $array['row'][] = array('v' => (string) "20-01-13"); $array['row'][] = array('v' => (int) 35); $array['row'][] = array('v' => (string) "21-01-13"); $array['row'][] = array('v' => (int) 30); } }
Although no errors occur, the line chart is sort of displayed, but there is no line, as if the data is 0. Below is a screenshot of how the chart is being displayed
Below is the output of the JSON content.
{"cols":[{"type":"string"},{"type":"number"}],"row":[{"c":[{"v":"20-01-13"},{"v":22}]},{"c":[{"v":"21-01-13"},{"v":24}]},{"c":[{"v":"22-01-13"},{"v":27}]}]}
Thanks for any help you can provide.
UPDATE I have tried to do what @davidkonrad suggested but I am now getting a different problem. I have changed the definition of row to rows for the PHP array as follows:
$array['cols'][] = array('type' => 'string'); $array['cols'][] = array('type' => 'number'); $array['rows'][] = array('c' => "20-01-13"); $array['rows'][] = array('v' => 35); $array['rows'][] = array('c' => "21-01-13"); $array['rows'][] = array('v' => 30);
But now when the graph loads I get
Cannot read property '0' of undefined
where the graph should be displayed.Below is how the JSON is now being generated
{"cols":[{"type":"string"},{"type":"number"}],"rows":[{"c":"20-01-13"},{"v":35},{"c":"21-01-13"},{"v":30}]}
I can't see how to change the array to make it match with the JSON that davidkonrad provided
原文:https://stackoverflow.com/questions/17327022
最满意答案
尝试这个:
CsdlProperty property = new CsdlProperty() .setName("test") .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()) .setCollection(true);
Try this:
CsdlProperty property = new CsdlProperty() .setName("test") .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()) .setCollection(true);
相关问答
更多-
我相信你使用某种HttpServlet来公开OData服务。 然后,更改URL就是改变web.xml或任何地方的映射的问题。 这里是一个存储库,我在其中放置了Spring Olingo应用程序示例: https : //github.com/sollersconsulting/confitura2017/tree/master/snapgram 。 映射在主类中完成 。 I believe that you use some kind of HttpServlet for exposing the ODat ...
-
经过更多调查后,我发现我需要odata-server-core-ext软件包,并且可以导入org.apache.olingo.server.core.MetadataParser 。 除此之外,这个类有一个名为buildEdmProvider(Reader)的函数,它为您构建了一个SchemaBasedEdmProvider 。 After more investigation, I found that I needed the odata-server-core-ext package and I co ...
-
事实证明它实际上非常容易。 Olingo将允许您通过实现JPAEdmExtension及其方法extendJPAEdmSchema来扩展架构。 在这里阅读更多相关信息。 这应该会让你前进,但我会尽力在不久的将来提供一个例子。 Turns out it is actually very easy. Olingo will let you extend the schema by implementing JPAEdmExtension and its method extendJPAEdmSchema. Re ...
-
如果您使用Olingo JPA Extension,则当前未实现从一个实体访问单个属性。 如果要支持此行为,可以注册自定义处理器,并仅覆盖“readEntityComplexProperty”和“readEntitySimpleProperty”方法。 在那里,您可以使用自定义代码来专门获取值。 您不会覆盖的每个方法都会导致执行标准的Olingo功能。 这是一个关于如何注册自定义JPA处理器的教程: http : //olingo.apache.org/doc/odata2/tutorials/Custom ...
-
Olingo:EntityContainer和EntitySets的区别(Olingo: EntityContainer and EntitySets differences)[2023-04-23]
根据OData文档 ,第4.1节: EDM的核心概念是实体和协会。 实体是实体类型的实例 (例如,Customer,Employee等),它们是由命名和类型属性以及密钥组成的结构化记录。 复杂类型是结构类型,也由一系列属性组成,但没有键,因此只能作为包含实体的属性或作为临时值存在。 实体密钥由实体类型的属性子集形成。 实体密钥(例如,CustomerId或OrderId)是唯一标识实体类型实例并允许实体类型实例参与关系的基本概念。 实体在实体集中分组(例如,客户是一组客户实体类型实例)。 关联定义两个或更多 ... -
正如您所说,您可以使用过滤器,但是您真的与Olingo使用的URI模式相结合。 此外,当您有多个相关的实体集时,事情会变得复杂(因为您可以从一个实体集导航到另一个,从而使URI更复杂)。 根据您想要实现的目标,您可以做两件事: 如果要对允许或不允许的操作进行精细控制,可以为ODataSingleProcesor创建一个包装器,并将ODataExceptions抛出到要禁止操作的位置。 您可以始终抛出异常(即完全禁用操作类型),也可以使用URI info参数获取目标实体集,并决定是应抛出异常还是调用标准单处理 ...
-
尝试这个: CsdlProperty property = new CsdlProperty() .setName("test") .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()) .setCollection(true); Try this: CsdlProperty property = new CsdlProperty() .setName("test") .setType(EdmPrimitiveTypeKind.String. ...
-
使用2.0.0 [ http://olingo.apache.org/download.html]版本的Olingo V2 lib,并不是必须使用name和referencedColumnName来注释关系属性。 有关更多详细信息,请参阅JIRA问题 - https://issues.apache.org/jira/browse/OLINGO-127 。 关心Chandan With 2.0.0 [http://olingo.apache.org/download.html] release of Olin ...
-
我认为属性名称区分大小写,因此请尝试使用UserName而不是username。 此外,您可以使用Fiddler来调试您的问题。 这是您的用例的错误: HTTP/1.1 500 Internal Server Error - {"error":{"code":"InternalServerError","message":"The property 'UserName[Nullable=False]' of type 'Edm.String' has a null value I think that p ...
-
如果您只想实现以下过滤器:services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq'Redmond' 请查看http://aspnet.codeplex.com/SourceControl/changeset/view/903afc4e11df#Samples%2fNet4%2fCS%2fWebApi%2fODataServiceSample%2fReadMe.txt上的示例。 它有一个具有可查询属性的供应商和地址模型。 它应该 ...