首页 \ 问答 \ 使用Google Chart API和JSON for DataTable创建折线图(Create line chart using Google Chart API and JSON for DataTable)

使用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

Line chart not working

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
更新时间:2023-07-23 06:07

最满意答案

尝试这个:
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 ...
  • 根据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上的示例。 它有一个具有可查询属性的供应商和地址模型。 它应该 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)